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
fix: token after strict mode block is evaluated in strict mode #11186
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we delay throwing octal error when reading a number token. So even in this.nextToken()
the next token is read in an incorrect strict mode, we can still throw later when we are sure this.state.strict
is correct.
return this.parseLiteral(this.state.value, "NumericLiteral"); |
We can delay throwing octal literals error when we finished parsing a numeric literal
const start = this.state.start;
const strict = this.state.strict;
node = this.parseLiteral(this.state.value, "NumericLiteral");
if (strict && this.state.containsOctal) {
this.raise(start, "Legacy octal literals are not allowed in strict mode");
}
this.state.containsOctal = null;
return node;
And in the tokenizer part we can set containsOctal: true
only and allow tokens be generated, so it will be checked later with correct strict
.
if (this.state.strict) { |
I think we should also get rid of
babel/packages/babel-parser/src/tokenizer/index.js
Lines 227 to 228 in d13fd7c
this.state.containsOctal = false; | |
this.state.octalPosition = null; |
as long as we correctly reset these states after parsing literals.
Sidenote: other than tokenizer#readNumber()
, we also risk incorrect state.strict
in tokenizer#readEscapedChar
.
} else if (this.state.strict) { |
But I think it is okay to leave it as-is because this function will never called in the nextToken of a tt.braceR
, which may be a boundary of strict mode. tokenizer#readEscapedChar
will only called after consuming a tt.backslash
token.
@@ -0,0 +1,17 @@ | |||
class A {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test case about function declaration, which is failing on master
function A() { "use strict"; }
05
Thanks for the review! Will take a look later this afternoon. |
Waiting to land #11188 first since the two PRs actually overlap a bit. |
FYI acornjs/acorn@44e52b2 is how acorn fixes this issue. That is a neat fix, maybe we can port it here? |
601b800
to
b808ce2
Compare
b808ce2
to
29b1609
Compare
This is ready for re-review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're currently eating the closing bracket token of the class body before restoring the outer scope's strict mode state. This PR changes it so that we restore the out strict mode state before eating this final token.
Question: Does this seem like the correct way to fix this?