Skip to content

Commit 580d730

Browse files
[estree] attach comments after directives at the end of file (#14920)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent 57d2b47 commit 580d730

5 files changed

Lines changed: 126 additions & 25 deletions

File tree

packages/babel-parser/src/plugins/estree.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,25 @@ export default (superClass: typeof Parser) =>
9595
return this.estreeParseLiteral(value);
9696
}
9797

98+
// Cast a Directive to an ExpressionStatement. Mutates the input Directive.
9899
directiveToStmt(directive: N.Directive): N.ExpressionStatement {
99-
const directiveLiteral = directive.value;
100+
const expression = directive.value as any as N.EstreeLiteral;
101+
delete directive.value;
100102

101-
const stmt = this.startNodeAt<N.ExpressionStatement>(
102-
directive.start,
103-
directive.loc.start,
104-
);
105-
const expression = this.startNodeAt<N.EstreeLiteral>(
106-
directiveLiteral.start,
107-
directiveLiteral.loc.start,
108-
);
103+
expression.type = "Literal";
104+
// @ts-expect-error N.EstreeLiteral.raw is not defined.
105+
expression.raw = expression.extra.raw;
106+
expression.value = expression.extra.expressionValue;
109107

110-
expression.value = directiveLiteral.extra.expressionValue;
111-
// @ts-expect-error TS2339: Property 'raw' does not exist on type 'Undone '.
112-
expression.raw = directiveLiteral.extra.raw;
108+
const stmt = directive as any as N.ExpressionStatement;
109+
stmt.type = "ExpressionStatement";
110+
stmt.expression = expression;
111+
// @ts-expect-error N.ExpressionStatement.directive is not defined
112+
stmt.directive = expression.extra.rawValue;
113113

114-
stmt.expression = this.finishNodeAt(
115-
expression,
116-
"Literal",
117-
directiveLiteral.loc.end,
118-
);
119-
// @ts-expect-error N.Directive.value is not defined
120-
stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
121-
122-
return this.finishNodeAt(
123-
stmt,
124-
"ExpressionStatement",
125-
directive.loc.end,
126-
) as N.ExpressionStatement;
114+
delete expression.extra;
115+
116+
return stmt;
127117
}
128118

129119
// ==================================
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// 1;
2+
"use strict";
3+
// 2;
4+
"use strict";
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":13}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":13}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ExpressionStatement",
12+
"start":6,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":13}},
13+
"trailingComments": [
14+
{
15+
"type": "CommentLine",
16+
"value": " 2;",
17+
"start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}}
18+
}
19+
],
20+
"leadingComments": [
21+
{
22+
"type": "CommentLine",
23+
"value": " 1;",
24+
"start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}}
25+
}
26+
],
27+
"directive": "use strict",
28+
"expression": {
29+
"type": "Literal",
30+
"start":6,"end":18,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}},
31+
"value": "use strict",
32+
"raw": "\"use strict\""
33+
}
34+
},
35+
{
36+
"type": "ExpressionStatement",
37+
"start":26,"end":39,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":13}},
38+
"leadingComments": [
39+
{
40+
"type": "CommentLine",
41+
"value": " 2;",
42+
"start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}}
43+
}
44+
],
45+
"directive": "use strict",
46+
"expression": {
47+
"type": "Literal",
48+
"start":26,"end":38,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":12}},
49+
"value": "use strict",
50+
"raw": "\"use strict\""
51+
}
52+
}
53+
]
54+
},
55+
"comments": [
56+
{
57+
"type": "CommentLine",
58+
"value": " 1;",
59+
"start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}}
60+
},
61+
{
62+
"type": "CommentLine",
63+
"value": " 2;",
64+
"start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}}
65+
}
66+
]
67+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
/** @external foo */
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":20}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":20}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ExpressionStatement",
12+
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
13+
"trailingComments": [
14+
{
15+
"type": "CommentBlock",
16+
"value": "* @external foo ",
17+
"start":15,"end":35,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":20,"index":35}}
18+
}
19+
],
20+
"expression": {
21+
"type": "Literal",
22+
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
23+
"value": "use strict",
24+
"raw": "\"use strict\""
25+
},
26+
"directive": "use strict"
27+
}
28+
]
29+
},
30+
"comments": [
31+
{
32+
"type": "CommentBlock",
33+
"value": "* @external foo ",
34+
"start":15,"end":35,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":20,"index":35}}
35+
}
36+
]
37+
}

0 commit comments

Comments
 (0)