Skip to content

Commit

Permalink
fix: restore moduleAttributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 8, 2020
1 parent 66b7fed commit ed15a13
Show file tree
Hide file tree
Showing 53 changed files with 1,113 additions and 6 deletions.
9 changes: 9 additions & 0 deletions packages/babel-generator/src/generators/modules.js
Expand Up @@ -190,6 +190,15 @@ export function ImportDeclaration(node: Object) {
this.space();
this.token("}");
}
// todo(Babel 8): remove this if branch
// `module-attributes` support is discontinued, use `import-assertions` instead.
else if (node.attributes?.length) {
this.space();
this.word("with");
this.space();
this.space();
this.printList(node.attributes, node);
}

this.semicolon();
}
Expand Down
@@ -0,0 +1,3 @@
import "foo.json" with type: "json";
import("foo.json", { with: { type: "json" } },);

@@ -1,9 +1,9 @@
{
"plugins": [
[
"importAssertions",
"moduleAttributes",
{
"version": "september-2020"
"version": "may-2020"
}
]
],
Expand Down
@@ -1,3 +1,4 @@
import "foo.json" with type: "json";
import("foo.json", {
with: {
type: "json"
Expand Down
15 changes: 12 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -829,13 +829,18 @@ export default class ExpressionParser extends LValParser {
): N.Expression {
if (node.callee.type === "Import") {
if (node.arguments.length === 2) {
this.expectPlugin("importAssertions");
// todo(Babel 8): remove the if condition,
// moduleAttributes is renamed to importAssertions
if (!this.hasPlugin("moduleAttributes")) {
this.expectPlugin("importAssertions");
}
}
if (node.arguments.length === 0 || node.arguments.length > 2) {
this.raise(
node.start,
Errors.ImportCallArity,
this.hasPlugin("importAssertions")
this.hasPlugin("importAssertions") ||
this.hasPlugin("moduleAttributes")
? "one or two arguments"
: "one argument",
);
Expand Down Expand Up @@ -872,7 +877,11 @@ export default class ExpressionParser extends LValParser {
} else {
this.expect(tt.comma);
if (this.match(close)) {
if (dynamicImport && !this.hasPlugin("importAssertions")) {
if (
dynamicImport &&
!this.hasPlugin("importAssertions") &&
!this.hasPlugin("moduleAttributes")
) {
this.raise(
this.state.lastTokStart,
Errors.ImportCallArgumentTrailingComma,
Expand Down
59 changes: 59 additions & 0 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -2084,6 +2084,14 @@ export default class StatementParser extends ExpressionParser {
if (assertions) {
node.assertions = assertions;
}
// todo(Babel 8): remove module attributes support
else {
const attributes = this.maybeParseModuleAttributes();
if (attributes) {
node.attributes = attributes;
}
}
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
}
Expand Down Expand Up @@ -2171,6 +2179,57 @@ export default class StatementParser extends ExpressionParser {
return attrs;
}
/**
* parse module attributes
* @deprecated It will be removed in Babel 8
* @returns
* @memberof StatementParser
*/
maybeParseModuleAttributes() {
if (this.match(tt._with) && !this.hasPrecedingLineBreak()) {
this.expectPlugin("moduleAttributes");
this.next();
} else {
if (this.hasPlugin("moduleAttributes")) return [];
return null;
}
const attrs = [];
const attributes = new Set();
do {
const node = this.startNode();
node.key = this.parseIdentifier(true);
if (node.key.name !== "type") {
this.raise(
node.key.start,
Errors.ModuleAttributeDifferentFromType,
node.key.name,
);
}
if (attributes.has(node.key.name)) {
this.raise(
node.key.start,
Errors.ModuleAttributesWithDuplicateKeys,
node.key.name,
);
}
attributes.add(node.key.name);
this.expect(tt.colon);
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
Errors.ModuleAttributeInvalidValue,
);
}
node.value = this.parseLiteral(this.state.value, "StringLiteral");
this.finishNode(node, "ImportAttribute");
attrs.push(node);
} while (this.eat(tt.comma));
return attrs;
}
maybeParseImportAssertions() {
if (
this.match(tt.name) &&
Expand Down
20 changes: 20 additions & 0 deletions packages/babel-parser/src/plugin-utils.js
Expand Up @@ -86,6 +86,21 @@ export function validatePlugins(plugins: PluginList) {
);
}

if (hasPlugin(plugins, "moduleAttributes")) {
const moduleAttributesVerionPluginOption = getPluginOption(
plugins,
"moduleAttributes",
"version",
);
if (moduleAttributesVerionPluginOption !== "may-2020") {
throw new Error(
"The 'moduleAttributes' plugin requires a 'version' option," +
" representing the last proposal update. Currently, the" +
" only supported value is 'may-2020'.",
);
}
}

if (hasPlugin(plugins, "importAssertions")) {
const importAssertionsVerionPluginOption = getPluginOption(
plugins,
Expand All @@ -99,6 +114,11 @@ export function validatePlugins(plugins: PluginList) {
" only supported value is 'september-2020'.",
);
}
if (hasPlugin(plugins, "moduleAttributes")) {
throw new Error(
"Cannot combine importAssertions and moduleAttributes plugins.",
);
}
}
if (
hasPlugin(plugins, "recordAndTuple") &&
Expand Down
@@ -1 +1 @@
import foo from "foo.json" assert { type: "json" };
import foo from "foo.json" with type: "json";
@@ -0,0 +1,3 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)"
}
@@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } })
@@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}
@@ -0,0 +1,81 @@
{
"type": "File",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"program": {
"type": "Program",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"expression": {
"type": "CallExpression",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":17,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":17}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":19,"end":45,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":45}},
"properties": [
{
"type": "ObjectProperty",
"start":21,"end":43,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":43}},
"method": false,
"key": {
"type": "Identifier",
"start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25},"identifierName":"with"},
"name": "with"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":27,"end":43,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":43}},
"properties": [
{
"type": "ObjectProperty",
"start":29,"end":41,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":41}},
"method": false,
"key": {
"type": "Identifier",
"start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
import "x"
with ({});
@@ -0,0 +1,12 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
],
"estree"
],
"sourceType": "module"
}
@@ -0,0 +1,40 @@
{
"type": "File",
"start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}},
"errors": [
"SyntaxError: 'with' in strict mode (2:0)"
],
"program": {
"type": "Program",
"start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"specifiers": [],
"source": {
"type": "Literal",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"value": "x",
"raw": "\"x\""
},
"attributes": []
},
{
"type": "WithStatement",
"start":11,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}},
"object": {
"type": "ObjectExpression",
"start":17,"end":19,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":8}},
"properties": []
},
"body": {
"type": "EmptyStatement",
"start":20,"end":21,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":10}}
}
}
]
}
}
@@ -0,0 +1,2 @@
import();
import("./foo.json", { with: { type: "json"} }, "unsupported");
@@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

0 comments on commit ed15a13

Please sign in to comment.