Skip to content

Commit

Permalink
Update missed hasPlugin checks, and add estree tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 29, 2023
1 parent 13b7f56 commit 01bdaa6
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -991,6 +991,7 @@ export default abstract class ExpressionParser extends LValParser {
this.raise(Errors.ImportCallArity, {
at: node,
maxArgumentCount:
this.hasPlugin("importAttributes") ||
this.hasPlugin("importAssertions") ||
this.hasPlugin("moduleAttributes")
? 2
Expand Down Expand Up @@ -1031,6 +1032,7 @@ export default abstract class ExpressionParser extends LValParser {
if (this.match(close)) {
if (
dynamicImport &&
!this.hasPlugin("importAttributes") &&
!this.hasPlugin("importAssertions") &&
!this.hasPlugin("moduleAttributes")
) {
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -420,7 +420,10 @@ export default (superClass: typeof Parser) =>
if (node.callee.type === "Import") {
(node as N.Node as N.EstreeImportExpression).type = "ImportExpression";
(node as N.Node as N.EstreeImportExpression).source = node.arguments[0];
if (this.hasPlugin("importAssertions")) {
if (
this.hasPlugin("importAttributes") ||
this.hasPlugin("importAssertions")
) {
(node as N.Node as N.EstreeImportExpression).attributes =
node.arguments[1] ?? null;
}
Expand Down
@@ -0,0 +1 @@
import("module");
@@ -0,0 +1,3 @@
{
"plugins": ["estree", "importAttributes"]
}
@@ -0,0 +1,27 @@
{
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"program": {
"type": "Program",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"expression": {
"type": "ImportExpression",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"source": {
"type": "Literal",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}},
"value": "module",
"raw": "\"module\""
},
"attributes": null
}
}
]
}
}
@@ -0,0 +1 @@
import("module", { with: { type: "json" } });
@@ -0,0 +1,3 @@
{
"plugins": ["estree", "importAttributes"]
}
@@ -0,0 +1,70 @@
{
"type": "File",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"program": {
"type": "Program",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"expression": {
"type": "ImportExpression",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"source": {
"type": "Literal",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}},
"value": "module",
"raw": "\"module\""
},
"attributes": {
"type": "ObjectExpression",
"start":17,"end":43,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":43}},
"properties": [
{
"type": "Property",
"start":19,"end":41,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":41}},
"method": false,
"key": {
"type": "Identifier",
"start":19,"end":23,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":23},"identifierName":"with"},
"name": "with"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":25,"end":41,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":41}},
"properties": [
{
"type": "Property",
"start":27,"end":39,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":39}},
"method": false,
"key": {
"type": "Identifier",
"start":27,"end":31,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":31},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "Literal",
"start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}},
"value": "json",
"raw": "\"json\""
},
"kind": "init"
}
]
},
"kind": "init"
}
]
}
}
}
]
}
}
@@ -0,0 +1 @@
import foo from "foo.json" with { type: "json" };
@@ -0,0 +1,9 @@
{
"plugins": [
"flow",
"jsx",
"estree",
"importAttributes"
],
"sourceType": "module"
}
@@ -0,0 +1,51 @@
{
"type": "File",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}},
"program": {
"type": "Program",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"local": {
"type": "Identifier",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"},
"name": "foo"
}
}
],
"importKind": "value",
"source": {
"type": "Literal",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"value": "foo.json",
"raw": "\"foo.json\""
},
"attributes": [
{
"type": "ImportAttribute",
"start":34,"end":46,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":46}},
"key": {
"type": "Identifier",
"start":34,"end":38,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":38},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "Literal",
"start":40,"end":46,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":46}},
"value": "json",
"raw": "\"json\""
}
}
]
}
]
}
}
Expand Up @@ -2,8 +2,8 @@
"type": "File",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":65,"index":75}},
"errors": [
"SyntaxError: `import()` requires exactly one argument. (1:0)",
"SyntaxError: `import()` requires exactly one argument. (2:0)"
"SyntaxError: `import()` requires exactly one or two arguments. (1:0)",
"SyntaxError: `import()` requires exactly one or two arguments. (2:0)"
],
"program": {
"type": "Program",
Expand Down
@@ -1,10 +1,6 @@
{
"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}},
Expand Down
Expand Up @@ -2,8 +2,8 @@
"type": "File",
"start":0,"end":73,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":63,"index":73}},
"errors": [
"SyntaxError: `import()` requires exactly one argument. (1:0)",
"SyntaxError: `import()` requires exactly one argument. (2:0)"
"SyntaxError: `import()` requires exactly one or two arguments. (1:0)",
"SyntaxError: `import()` requires exactly one or two arguments. (2:0)"
],
"program": {
"type": "Program",
Expand Down
@@ -1,10 +1,6 @@
{
"type": "File",
"start":0,"end":67,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":48,"index":67}},
"errors": [
"SyntaxError: Trailing comma is disallowed inside import(...) arguments. (1:15)",
"SyntaxError: Trailing comma is disallowed inside import(...) arguments. (2:45)"
],
"program": {
"type": "Program",
"start":0,"end":67,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":48,"index":67}},
Expand Down

0 comments on commit 01bdaa6

Please sign in to comment.