Skip to content
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

[estree] attach comments after directives at the end of file #14920

Merged
merged 3 commits into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 13 additions & 28 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -95,35 +95,20 @@ export default (superClass: typeof Parser) =>
return this.estreeParseLiteral(value);
}

// Cast a Directive to an ExpressionStatement. Mutates the input Directive.
directiveToStmt(directive: N.Directive): N.ExpressionStatement {
const directiveLiteral = directive.value;

const stmt = this.startNodeAt<N.ExpressionStatement>(
directive.start,
directive.loc.start,
);
const expression = this.startNodeAt<N.EstreeLiteral>(
directiveLiteral.start,
directiveLiteral.loc.start,
);

expression.value = directiveLiteral.extra.expressionValue;
// @ts-expect-error TS2339: Property 'raw' does not exist on type 'Undone '.
expression.raw = directiveLiteral.extra.raw;

stmt.expression = this.finishNodeAt(
expression,
"Literal",
directiveLiteral.loc.end,
);
// @ts-expect-error N.Directive.value is not defined
stmt.directive = directiveLiteral.extra.raw.slice(1, -1);

return this.finishNodeAt(
stmt,
"ExpressionStatement",
directive.loc.end,
) as N.ExpressionStatement;
const stmt = directive as any;
stmt.type = "ExpressionStatement";
stmt.directive = stmt.value.extra.rawValue;
stmt.expression = stmt.value;
delete stmt.value;

stmt.expression.type = "Literal";
stmt.expression.raw = stmt.expression.extra.raw;
stmt.expression.value = stmt.expression.extra.expressionValue;
delete stmt.expression.extra;

return stmt as N.ExpressionStatement;
}

// ==================================
Expand Down
@@ -0,0 +1,4 @@
// 1;
"use strict";
// 2;
"use strict";
@@ -0,0 +1,67 @@
{
"type": "File",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":13}},
"program": {
"type": "Program",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":13}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":6,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":13}},
"trailingComments": [
{
"type": "CommentLine",
"value": " 2;",
"start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}}
}
],
"leadingComments": [
{
"type": "CommentLine",
"value": " 1;",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}}
}
],
"directive": "use strict",
"expression": {
"type": "Literal",
"start":6,"end":18,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}},
"value": "use strict",
"raw": "\"use strict\""
}
},
{
"type": "ExpressionStatement",
"start":26,"end":39,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":13}},
"leadingComments": [
{
"type": "CommentLine",
"value": " 2;",
"start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}}
}
],
"directive": "use strict",
"expression": {
"type": "Literal",
"start":26,"end":38,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":12}},
"value": "use strict",
"raw": "\"use strict\""
}
}
]
},
"comments": [
{
"type": "CommentLine",
"value": " 1;",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}}
},
{
"type": "CommentLine",
"value": " 2;",
"start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}}
}
]
}
@@ -0,0 +1,3 @@
"use strict";

/** @external foo */
@@ -0,0 +1,37 @@
{
"type": "File",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":20}},
"program": {
"type": "Program",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":20}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"trailingComments": [
{
"type": "CommentBlock",
"value": "* @external foo ",
"start":15,"end":35,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":20,"index":35}}
}
],
"expression": {
"type": "Literal",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"value": "use strict",
"raw": "\"use strict\""
},
"directive": "use strict"
}
]
},
"comments": [
{
"type": "CommentBlock",
"value": "* @external foo ",
"start":15,"end":35,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":20,"index":35}}
}
]
}