Skip to content

Commit 44e52b2

Browse files
committed
Don't tokenize the token after a class or strict function as strict
Closes #912
1 parent c6f08e8 commit 44e52b2

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

acorn/src/expression.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -838,16 +838,14 @@ pp.parseFunctionBody = function(node, isArrowFunction, isMethod) {
838838
// Add the params to varDeclaredNames to ensure that an error is thrown
839839
// if a let/const declaration in the function clashes with one of the params.
840840
this.checkParams(node, !oldStrict && !useStrict && !isArrowFunction && !isMethod && this.isSimpleParamList(node.params))
841-
node.body = this.parseBlock(false)
841+
// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
842+
if (this.strict && node.id) this.checkLVal(node.id, BIND_OUTSIDE)
843+
node.body = this.parseBlock(false, undefined, useStrict && !oldStrict)
842844
node.expression = false
843845
this.adaptDirectivePrologue(node.body.body)
844846
this.labels = oldLabels
845847
}
846848
this.exitScope()
847-
848-
// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
849-
if (this.strict && node.id) this.checkLVal(node.id, BIND_OUTSIDE)
850-
this.strict = oldStrict
851849
}
852850

853851
pp.isSimpleParamList = function(params) {

acorn/src/statement.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,16 @@ pp.parseExpressionStatement = function(node, expr) {
418418
// strict"` declarations when `allowStrict` is true (used for
419419
// function bodies).
420420

421-
pp.parseBlock = function(createNewLexicalScope = true, node = this.startNode()) {
421+
pp.parseBlock = function(createNewLexicalScope = true, node = this.startNode(), exitStrict) {
422422
node.body = []
423423
this.expect(tt.braceL)
424424
if (createNewLexicalScope) this.enterScope(0)
425-
while (!this.eat(tt.braceR)) {
425+
while (this.type !== tt.braceR) {
426426
let stmt = this.parseStatement(null)
427427
node.body.push(stmt)
428428
}
429+
if (exitStrict) this.strict = false
430+
this.next()
429431
if (createNewLexicalScope) this.exitScope()
430432
return this.finishNode(node, "BlockStatement")
431433
}
@@ -578,7 +580,7 @@ pp.parseClass = function(node, isStatement) {
578580
let hadConstructor = false
579581
classBody.body = []
580582
this.expect(tt.braceL)
581-
while (!this.eat(tt.braceR)) {
583+
while (this.type !== tt.braceR) {
582584
const element = this.parseClassElement(node.superClass !== null)
583585
if (element) {
584586
classBody.body.push(element)
@@ -588,8 +590,9 @@ pp.parseClass = function(node, isStatement) {
588590
}
589591
}
590592
}
591-
node.body = this.finishNode(classBody, "ClassBody")
592593
this.strict = oldStrict
594+
this.next()
595+
node.body = this.finishNode(classBody, "ClassBody")
593596
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
594597
}
595598

test/tests-harmony.js

+4
Original file line numberDiff line numberDiff line change
@@ -16516,3 +16516,7 @@ test("({ a = 42, b: c.d } = e)", {}, {ecmaVersion: 6})
1651616516
testFail("({ a = 42, b: c = d })", "Shorthand property assignments are valid only in destructuring patterns (1:5)", {ecmaVersion: 6})
1651716517

1651816518
test("({ __proto__: x, __proto__: y, __proto__: z }) => {}", {}, {ecmaVersion: 6})
16519+
16520+
// Don't parse first token after a class or strict function as strict
16521+
test("class x {}\n05", {}, {ecmaVersion: 6})
16522+
test("function x() { 'use strict' }\n05", {}, {ecmaVersion: 6})

0 commit comments

Comments
 (0)