Skip to content

Commit

Permalink
use safer and more reliable approach to cast Directive to ExpressionS…
Browse files Browse the repository at this point in the history
…tatement
  • Loading branch information
hegemonic committed Sep 13, 2022
1 parent 14b764d commit dca2259
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 31 deletions.
44 changes: 13 additions & 31 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -95,38 +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.leadingComments = directive.leadingComments?.slice();
stmt.trailingComments = directive.trailingComments?.slice();

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 comments on commit dca2259

Please sign in to comment.