Skip to content

correctly parse destructuring in let outside of strict mode #2323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,12 @@ module ts {
return !scanner.hasPrecedingLineBreak() && isIdentifier()
}

function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() {
nextToken();
return !scanner.hasPrecedingLineBreak() &&
(isIdentifier() || token === SyntaxKind.OpenBraceToken || token === SyntaxKind.OpenBracketToken);
}

function parseYieldExpression(): YieldExpression {
var node = <YieldExpression>createNode(SyntaxKind.YieldExpression);

Expand Down Expand Up @@ -4873,9 +4879,9 @@ module ts {
}

function isLetDeclaration() {
// It is let declaration if in strict mode or next token is identifier on same line.
// It is let declaration if in strict mode or next token is identifier\open bracket\open curly on same line.
// otherwise it needs to be treated like identifier
return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOnSameLine);
return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine);
}

function isDeclarationStart(): boolean {
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/letInNonStrictMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [letInNonStrictMode.ts]
let [x] = [1];
let {a: y} = {a: 1};

//// [letInNonStrictMode.js]
var x = ([
1
])[0];
var y = ({
a: 1
}).a;
11 changes: 11 additions & 0 deletions tests/baselines/reference/letInNonStrictMode.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/letInNonStrictMode.ts ===
let [x] = [1];
>x : number
>[1] : [number]

let {a: y} = {a: 1};
>a : unknown
>y : number
>{a: 1} : { a: number; }
>a : number

2 changes: 2 additions & 0 deletions tests/cases/compiler/letInNonStrictMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let [x] = [1];
let {a: y} = {a: 1};