Skip to content

Commit

Permalink
fix: ForInOfLoopInitializer only applies in strict mode
Browse files Browse the repository at this point in the history
closes #116
  • Loading branch information
3cp committed Oct 24, 2020
1 parent dc8bc71 commit 5f6f0d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 53 deletions.
5 changes: 1 addition & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2113,10 +2113,7 @@ function parseVariableDeclaration(
if (
parser.token === Token.OfKeyword ||
(parser.token === Token.InKeyword &&
(token & Token.IsPatternStart ||
(kind & BindingKind.Variable) < 1 ||
(context & Context.OptionsWebCompat) < 1 ||
context & Context.Strict))
(token & Token.IsPatternStart || (kind & BindingKind.Variable) < 1 || context & Context.Strict))
) {
reportMessageAt(
tokenPos,
Expand Down
90 changes: 41 additions & 49 deletions test/parser/statements/for-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,46 @@ describe('Statements - For in', () => {
});
});
}
// ForInOfLoopInitializer only applies in strict mode when webCompat is off
for (const arg of [
'for(var x=1 in [1,2,3]) 0', // Throws with no 'WebCompat'
'for(var x=1 in [1,2,3]) 0',
'for (var x = 1 in y) {}',
'for (var a = 0 in {});',
'for (var a = ++b in c);',
'for (var a = b in c);',
'for (var a = 0 in stored = a, {});',
'for (var a = (++effects, -1) in x);'
]) {
it(`${arg}`, () => {
t.doesNotThrow(() => {
parseSource(`${arg}`, undefined, Context.OptionsWebCompat);
});
});

it(`${arg}`, () => {
t.throws(() => {
parseSource(`${arg}`, undefined, Context.OptionsWebCompat | Context.Strict);
});
});

it(`${arg}`, () => {
t.doesNotThrow(() => {
parseSource(`${arg}`, undefined, Context.None);
});
});

it(`${arg}`, () => {
t.throws(() => {
parseSource(`${arg}`, undefined, Context.Strict);
});
});
}

for (const arg of [
'"use strict";\nfor (var a = 0 in {});',
'for(var [] = 0 in {});',
'for(var [,] = 0 in {});',
'for(var [a] = 0 in {});',
'for (var x = 1 in y) {}',
'for (([x])=y in z);',
'for (a = 0 in {});',
'for ([...x,] in [[]]) ;',
Expand Down Expand Up @@ -464,7 +498,6 @@ describe('Statements - For in', () => {
['for(([0]) in 0);', Context.None],
['for(({a: 0}) in 0);', Context.None],
[`for ((()=>x) in y);`, Context.None],
['for (var a = 0 in {});', Context.None],
['for (const [x,y,z] = 22 in foo);', Context.None],
['for (true ? 0 : 0 in {}; false; ) ;', Context.None],
['for (x = 22 in foo);', Context.None],
Expand All @@ -484,7 +517,6 @@ describe('Statements - For in', () => {
['for (let in o) { }', Context.Strict],
['for(var [...a] = 0 in {});', Context.None],
['for (var a = () => { return "a"} in {});', Context.None],
['for (var a = ++b in c);', Context.None],
['for (const ...x in y){}', Context.None],
['for (...x in y){}', Context.None],
['for (let a = b => b in c; ;);', Context.None],
Expand Down Expand Up @@ -514,7 +546,6 @@ describe('Statements - For in', () => {
['for([a] = 0 in {});', Context.None],
['for([a = 0] = 0 in {});', Context.None],
['for([...a] = 0 in {});', Context.None],
['for (var a = b in c);', Context.None],
['for([...[a]] = 0 in {});', Context.None],
['for({} = 0 in {});', Context.None],
['for({p: x} = 0 in {});', Context.None],
Expand Down Expand Up @@ -551,8 +582,6 @@ describe('Statements - For in', () => {
['"use strict"; for ({ x = yield } in [{}]) ;', Context.None],
['for (let x in {}) label1: label2: function f() {}', Context.None],
['for (x in {}) label1: label2: function f() {}', Context.None],
['for (var a = 0 in stored = a, {});', Context.None],
['for (var a = (++effects, -1) in x);', Context.None],
['for (var [a] = 0 in {});', Context.None],
['"use strict"; for (var i=0 in j);', Context.None],
['for (var {x}=0 in y);', Context.None],
Expand Down Expand Up @@ -779,7 +808,7 @@ describe('Statements - For in', () => {
],
[
'for(var x=1 in [1,2,3]) 0',
Context.OptionsWebCompat,
Context.None,
{
type: 'Program',
sourceType: 'script',
Expand Down Expand Up @@ -1130,7 +1159,7 @@ describe('Statements - For in', () => {
],
[
'for (var a = b in c);',
Context.OptionsWebCompat,
Context.None,
{
type: 'Program',
body: [
Expand Down Expand Up @@ -2879,46 +2908,9 @@ describe('Statements - For in', () => {
]
}
],
[
'for (var a = b in c);',
Context.OptionsWebCompat,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ForInStatement',
body: {
type: 'EmptyStatement'
},
left: {
type: 'VariableDeclaration',
kind: 'var',
declarations: [
{
type: 'VariableDeclarator',
init: {
type: 'Identifier',
name: 'b'
},
id: {
type: 'Identifier',
name: 'a'
}
}
]
},
right: {
type: 'Identifier',
name: 'c'
}
}
]
}
],
[
'for (var a = ++b in c);',
Context.OptionsWebCompat,
Context.None,
{
type: 'Program',
sourceType: 'script',
Expand Down Expand Up @@ -2960,7 +2952,7 @@ describe('Statements - For in', () => {
],
[
'for (var a = 0 in stored = a, {});',
Context.OptionsWebCompat,
Context.None,
{
type: 'Program',
sourceType: 'script',
Expand Down Expand Up @@ -3014,7 +3006,7 @@ describe('Statements - For in', () => {
],
[
'for (var a = (++effects, -1) in x);',
Context.OptionsWebCompat,
Context.None,
{
type: 'Program',
sourceType: 'script',
Expand Down

0 comments on commit 5f6f0d8

Please sign in to comment.