Skip to content

Commit

Permalink
Update finishCallExpression and avoid type cast
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 12, 2023
1 parent 8534573 commit aeabbd9
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 32 deletions.
10 changes: 8 additions & 2 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -967,17 +967,23 @@ export default abstract class ExpressionParser extends LValParser {
);
}

expectImportAttributesPlugin() {
if (!this.hasPlugin("importAssertions")) {
this.expectPlugin("importAttributes");
}
}

finishCallExpression<T extends N.CallExpression | N.OptionalCallExpression>(
node: Undone<T>,
optional: boolean,
): T {
if (node.callee.type === "Import") {
if (node.arguments.length === 2) {
if (process.env.BABEL_8_BREAKING) {
this.expectPlugin("importAssertions");
this.expectImportAttributesPlugin();
} else {
if (!this.hasPlugin("moduleAttributes")) {
this.expectPlugin("importAssertions");
this.expectImportAttributesPlugin();
}
}
}
Expand Down
13 changes: 3 additions & 10 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -3042,10 +3042,10 @@ export default abstract class StatementParser extends ExpressionParser {
* @deprecated It will be removed in Babel 8
*/
parseModuleAttributes() {
const attrs = [];
const attrs: N.ImportAttribute[] = [];
const attributes = new Set();
do {
const node = this.startNode();
const node = this.startNode<N.ImportAttribute>();
node.key = this.parseIdentifier(true);

if (node.key.name !== "type") {
Expand All @@ -3068,19 +3068,12 @@ export default abstract class StatementParser extends ExpressionParser {
});
}
node.value = this.parseStringLiteral(this.state.value);
this.finishNode(node, "ImportAttribute");
attrs.push(node as N.ImportAttribute);
attrs.push(this.finishNode(node, "ImportAttribute"));
} while (this.eat(tt.comma));

return attrs;
}

expectImportAttributesPlugin() {
if (!this.hasPlugin("importAssertions")) {
this.expectPlugin("importAttributes");
}
}

maybeParseImportAttributes(
node: Undone<N.ImportDeclaration | N.ExportNamedDeclaration>,
) {
Expand Down
@@ -1,5 +1,5 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: \"importAssertions\". (1:24)",
"sourceType": "module",
"plugins": []
"plugins": [],
"throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:24)"
}

This file was deleted.

@@ -0,0 +1,81 @@
{
"type": "File",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
"program": {
"type": "Program",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
"expression": {
"type": "CallExpression",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":17,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":17,"index":17}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":19,"end":47,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":47,"index":47}},
"properties": [
{
"type": "ObjectProperty",
"start":21,"end":45,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":45,"index":45}},
"method": false,
"key": {
"type": "Identifier",
"start":21,"end":27,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":27,"index":27},"identifierName":"assert"},
"name": "assert"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":29,"end":45,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":45,"index":45}},
"properties": [
{
"type": "ObjectProperty",
"start":31,"end":43,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":43,"index":43}},
"method": false,
"key": {
"type": "Identifier",
"start":31,"end":35,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":35,"index":35},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":37,"end":43,"loc":{"start":{"line":1,"column":37,"index":37},"end":{"line":1,"column":43,"index":43}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
]
}
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,48 @@
{
"type": "File",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}},
"errors": [
"SyntaxError: `...` is not allowed in `import()`. (1:21)"
],
"program": {
"type": "Program",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}},
"expression": {
"type": "CallExpression",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":19,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":19,"index":19}},
"extra": {
"rawValue": "./foo.json",
"raw": "\"./foo.json\""
},
"value": "./foo.json"
},
{
"type": "SpreadElement",
"start":21,"end":26,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":26,"index":26}},
"argument": {
"type": "ArrayExpression",
"start":24,"end":26,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":26,"index":26}},
"elements": []
}
}
]
}
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,114 @@
{
"type": "File",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":50,"index":69}},
"errors": [
"SyntaxError: Trailing comma is disallowed inside import(...) arguments. (1:15)",
"SyntaxError: Trailing comma is disallowed inside import(...) arguments. (2:47)"
],
"program": {
"type": "Program",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":50,"index":69}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}},
"expression": {
"type": "CallExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}}
},
"extra": {
"trailingComma": 15
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":15,"index":15}},
"extra": {
"rawValue": "foo.js",
"raw": "\"foo.js\""
},
"value": "foo.js"
}
]
}
},
{
"type": "ExpressionStatement",
"start":19,"end":69,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":50,"index":69}},
"expression": {
"type": "CallExpression",
"start":19,"end":68,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":49,"index":68}},
"callee": {
"type": "Import",
"start":19,"end":25,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":6,"index":25}}
},
"extra": {
"trailingComma": 66
},
"arguments": [
{
"type": "StringLiteral",
"start":26,"end":36,"loc":{"start":{"line":2,"column":7,"index":26},"end":{"line":2,"column":17,"index":36}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":38,"end":66,"loc":{"start":{"line":2,"column":19,"index":38},"end":{"line":2,"column":47,"index":66}},
"properties": [
{
"type": "ObjectProperty",
"start":40,"end":64,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":45,"index":64}},
"method": false,
"key": {
"type": "Identifier",
"start":40,"end":46,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":27,"index":46},"identifierName":"assert"},
"name": "assert"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":48,"end":64,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":45,"index":64}},
"properties": [
{
"type": "ObjectProperty",
"start":50,"end":62,"loc":{"start":{"line":2,"column":31,"index":50},"end":{"line":2,"column":43,"index":62}},
"method": false,
"key": {
"type": "Identifier",
"start":50,"end":54,"loc":{"start":{"line":2,"column":31,"index":50},"end":{"line":2,"column":35,"index":54},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":56,"end":62,"loc":{"start":{"line":2,"column":37,"index":56},"end":{"line":2,"column":43,"index":62}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
]
}
}
],
"directives": []
}
}

This file was deleted.

0 comments on commit aeabbd9

Please sign in to comment.