Skip to content

Commit 171a5f8

Browse files
committed
correctly parse destructuring in let outside of strict mode
1 parent ecfa19a commit 171a5f8

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/compiler/parser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,6 +2975,11 @@ module ts {
29752975
return !scanner.hasPrecedingLineBreak() && isIdentifier()
29762976
}
29772977

2978+
function netTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() {
2979+
nextToken();
2980+
return !scanner.hasPrecedingLineBreak() && (isIdentifier() || token === SyntaxKind.OpenBraceToken || token === SyntaxKind.OpenBracketToken)
2981+
}
2982+
29782983
function parseYieldExpression(): YieldExpression {
29792984
var node = <YieldExpression>createNode(SyntaxKind.YieldExpression);
29802985

@@ -4873,9 +4878,9 @@ module ts {
48734878
}
48744879

48754880
function isLetDeclaration() {
4876-
// It is let declaration if in strict mode or next token is identifier on same line.
4881+
// It is let declaration if in strict mode or next token is identifier\open brace\open curly on same line.
48774882
// otherwise it needs to be treated like identifier
4878-
return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOnSameLine);
4883+
return inStrictModeContext() || lookAhead(netTokenIsIdentifierOrStartOfDestructuringOnTheSameLine);
48794884
}
48804885

48814886
function isDeclarationStart(): boolean {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [letInNonStrictMode.ts]
2+
let [x] = [1];
3+
let {a: y} = {a: 1};
4+
5+
//// [letInNonStrictMode.js]
6+
var x = ([
7+
1
8+
])[0];
9+
var y = ({
10+
a: 1
11+
}).a;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/letInNonStrictMode.ts ===
2+
let [x] = [1];
3+
>x : number
4+
>[1] : [number]
5+
6+
let {a: y} = {a: 1};
7+
>a : unknown
8+
>y : number
9+
>{a: 1} : { a: number; }
10+
>a : number
11+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let [x] = [1];
2+
let {a: y} = {a: 1};

0 commit comments

Comments
 (0)