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: improve module block program location tracking #15052
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,10 +222,19 @@ export default abstract class StatementParser extends ExpressionParser { | |
this.raise(Errors.ModuleExportUndefined, { at, localName }); | ||
} | ||
} | ||
let finishedProgram: N.Program; | ||
if (end === tt.eof) { | ||
this.next(); // consume eof for the top level program | ||
// finish at eof for top level program | ||
finishedProgram = this.finishNode(program, "Program"); | ||
} else { | ||
// finish immediately before the end token | ||
finishedProgram = this.finishNodeAt( | ||
program, | ||
"Program", | ||
createPositionWithColumnOffset(this.state.startLoc, -1), | ||
); | ||
} | ||
return this.finishNode(program, "Program"); | ||
return finishedProgram; | ||
} | ||
|
||
// TODO | ||
|
@@ -1124,7 +1133,6 @@ export default abstract class StatementParser extends ExpressionParser { | |
tt.braceR, | ||
afterBlockParse, | ||
); | ||
this.next(); // eat tt.braceR | ||
if (createNewLexicalScope) { | ||
this.scope.exit(); | ||
} | ||
|
@@ -1206,6 +1214,8 @@ export default abstract class StatementParser extends ExpressionParser { | |
if (!oldStrict) { | ||
this.setStrict(false); | ||
} | ||
|
||
this.next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is essentially reverting changes in 101cad5. |
||
} | ||
|
||
// Parse a regular `for` loop. The disambiguation code in | ||
|
@@ -1881,7 +1891,6 @@ export default abstract class StatementParser extends ExpressionParser { | |
this.prodParam.enter(PARAM); | ||
const body: N.Node[] = (member.body = []); | ||
this.parseBlockOrModuleBlockBody(body, undefined, false, tt.braceR); | ||
this.next(); // eat tt.braceR | ||
this.prodParam.exit(); | ||
this.scope.exit(); | ||
this.state.labels = oldLabels; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module { #!/usr/bin/env node }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"throws": "Unexpected token (1:9)" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module {/* comment */} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, | ||
"expression": { | ||
"type": "ModuleExpression", | ||
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, | ||
"body": { | ||
"type": "Program", | ||
"start":8,"end":21,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":21,"index":21}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [], | ||
"directives": [], | ||
"innerComments": [ | ||
{ | ||
"type": "CommentBlock", | ||
"value": " comment ", | ||
"start":8,"end":21,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":21,"index":21}} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
}, | ||
"comments": [ | ||
{ | ||
"type": "CommentBlock", | ||
"value": " comment ", | ||
"start":8,"end":21,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":21,"index":21}} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module { | ||
/* leading */ x /* trailing */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently Babel attaches Now the behaviour is aligned to how we parse plain |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":43}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":43}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":43}}, | ||
"expression": { | ||
"type": "ModuleExpression", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":43}}, | ||
"body": { | ||
"type": "Program", | ||
"start":8,"end":42,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":3,"column":0,"index":42}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start":25,"end":26,"loc":{"start":{"line":2,"column":16,"index":25},"end":{"line":2,"column":17,"index":26}}, | ||
"expression": { | ||
"type": "Identifier", | ||
"start":25,"end":26,"loc":{"start":{"line":2,"column":16,"index":25},"end":{"line":2,"column":17,"index":26},"identifierName":"x"}, | ||
"name": "x" | ||
}, | ||
"trailingComments": [ | ||
{ | ||
"type": "CommentBlock", | ||
"value": " trailing ", | ||
"start":27,"end":41,"loc":{"start":{"line":2,"column":18,"index":27},"end":{"line":2,"column":32,"index":41}} | ||
} | ||
], | ||
"leadingComments": [ | ||
{ | ||
"type": "CommentBlock", | ||
"value": " leading ", | ||
"start":11,"end":24,"loc":{"start":{"line":2,"column":2,"index":11},"end":{"line":2,"column":15,"index":24}} | ||
} | ||
] | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
}, | ||
"comments": [ | ||
{ | ||
"type": "CommentBlock", | ||
"value": " leading ", | ||
"start":11,"end":24,"loc":{"start":{"line":2,"column":2,"index":11},"end":{"line":2,"column":15,"index":24}} | ||
}, | ||
{ | ||
"type": "CommentBlock", | ||
"value": " trailing ", | ||
"start":27,"end":41,"loc":{"start":{"line":2,"column":18,"index":27},"end":{"line":2,"column":32,"index":41}} | ||
} | ||
] | ||
} |
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.
Nit, but we could use
this.state.lastTokEnd
/this.state.lastTokEnd.index
to keep using expect instead of this math-createNode-next dance.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.
They are not equivalent. For example
When we see a left brace
{
,this.state.endLoc
points to the whitespace after{
, whilethis.state.lastTokEnd
after{
is eaten will point to the whitespace immediately after the comment/* 1 */
.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.
👍