added basic support for module attributes and tests updated (#10962)

Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Vivek Nayyar 2020-05-25 01:26:28 +02:00 committed by GitHub
parent c5ba345ac2
commit 66b86e088c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
66 changed files with 1231 additions and 184 deletions

View File

@ -765,12 +765,22 @@ export default class ExpressionParser extends LValParser {
optional: boolean,
): N.Expression {
if (node.callee.type === "Import") {
if (node.arguments.length !== 1) {
this.raise(node.start, Errors.ImportCallArity);
if (node.arguments.length === 2) {
this.expectPlugin("moduleAttributes");
}
if (node.arguments.length === 0 || node.arguments.length > 2) {
this.raise(
node.start,
Errors.ImportCallArity,
this.hasPlugin("moduleAttributes")
? "one or two arguments"
: "one argument",
);
} else {
const importArg = node.arguments[0];
if (importArg?.type === "SpreadElement") {
this.raise(importArg.start, Errors.ImportCallSpreadArgument);
for (const arg of node.arguments) {
if (arg.type === "SpreadElement") {
this.raise(arg.start, Errors.ImportCallSpreadArgument);
}
}
}
}
@ -799,7 +809,7 @@ export default class ExpressionParser extends LValParser {
} else {
this.expect(tt.comma);
if (this.match(close)) {
if (dynamicImport) {
if (dynamicImport && !this.hasPlugin("moduleAttributes")) {
this.raise(
this.state.lastTokStart,
Errors.ImportCallArgumentTrailingComma,

View File

@ -67,7 +67,7 @@ export const Errors = Object.freeze({
IllegalReturn: "'return' outside of function",
ImportCallArgumentTrailingComma:
"Trailing comma is disallowed inside import(...) arguments",
ImportCallArity: "import() requires exactly one argument",
ImportCallArity: "import() requires exactly %0",
ImportCallNotNewExpression: "Cannot use new with import(...)",
ImportCallSpreadArgument: "... is not allowed in import()",
ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
@ -96,6 +96,12 @@ export const Errors = Object.freeze({
MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX",
MixingCoalesceWithLogical:
"Nullish coalescing operator(??) requires parens when mixing with logical operators",
ModuleAttributeDifferentFromType:
"The only accepted module attribute is `type`",
ModuleAttributeInvalidValue:
"Only string literals are allowed as module attribute values",
ModuleAttributesWithDuplicateKeys:
'Duplicate key "%0" is not allowed in module attributes',
ModuleExportUndefined: "Export '%0' is not defined",
MultipleDefaultsInSwitch: "Multiple default clauses",
NewlineAfterThrow: "Illegal newline after throw",

View File

@ -2038,13 +2038,31 @@ export default class StatementParser extends ExpressionParser {
// import '...'
node.specifiers = [];
if (!this.match(tt.string)) {
// check if we have a default import like
// import React from "react";
const hasDefault = this.maybeParseDefaultImportSpecifier(node);
/* we are checking if we do not have a default import, then it is obvious that we need named imports
* import { get } from "axios";
* but if we do have a default import
* we need to check if we have a comma after that and
* that is where this `|| this.eat` condition comes into play
*/
const parseNext = !hasDefault || this.eat(tt.comma);
// if we do have to parse the next set of specifiers, we first check for star imports
// import React, * from "react";
const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
// now we check if we need to parse the next imports
// but only if they are not importing * (everything)
if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
this.expectContextual("from");
}
node.source = this.parseImportSource();
// https://github.com/tc39/proposal-module-attributes
// parse module attributes if the next token is `with` or ignore and finish the ImportDeclaration node.
const attributes = this.maybeParseModuleAttributes();
if (attributes) {
node.attributes = attributes;
}
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
}
@ -2075,6 +2093,59 @@ export default class StatementParser extends ExpressionParser {
node.specifiers.push(this.finishNode(specifier, type));
}
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 {
// we are trying to parse a node which has the following syntax
// with type: "json"
// [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral]
const node = this.startNode();
node.key = this.parseIdentifier(true);
// for now we are only allowing `type` as the only allowed module attribute
if (node.key.name !== "type") {
this.raise(
node.key.start,
Errors.ModuleAttributeDifferentFromType,
node.key.name,
);
}
// check if we already have an entry for an attribute
// if a duplicate entry is found, throw an error
// for now this logic will come into play only when someone declares `type` twice
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);
// check if the value set to the module attribute is a string as we only allow string literals
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;
}
maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean {
if (this.shouldParseDefaultImport(node)) {
// import defaultObj, { x, y as z } from '...'

View File

@ -86,6 +86,20 @@ 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, "recordAndTuple") &&
!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(

View File

@ -1,111 +1,33 @@
{
"type": "File",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 2
}
},
"start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"program": {
"type": "Program",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 2
}
},
"start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "X"
},
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"X"},
"name": "X"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 10
}
},
"start":8,"end":10,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":10}},
"body": []
}
},
{
"type": "ExpressionStatement",
"start": 11,
"end": 13,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 2
}
},
"start":11,"end":13,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":2}},
"expression": {
"type": "NumericLiteral",
"start": 11,
"end": 13,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 2
}
},
"start":11,"end":13,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":2}},
"extra": {
"rawValue": 5,
"raw": "05"
@ -115,33 +37,10 @@
},
{
"type": "FunctionDeclaration",
"start": 15,
"end": 44,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 29
}
},
"start":15,"end":44,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":29}},
"id": {
"type": "Identifier",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 4,
"column": 9
},
"end": {
"line": 4,
"column": 10
},
"identifierName": "x"
},
"start":24,"end":25,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":10},"identifierName":"x"},
"name": "x"
},
"generator": false,
@ -149,48 +48,15 @@
"params": [],
"body": {
"type": "BlockStatement",
"start": 28,
"end": 44,
"loc": {
"start": {
"line": 4,
"column": 13
},
"end": {
"line": 4,
"column": 29
}
},
"start":28,"end":44,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":29}},
"body": [],
"directives": [
{
"type": "Directive",
"start": 30,
"end": 42,
"loc": {
"start": {
"line": 4,
"column": 15
},
"end": {
"line": 4,
"column": 27
}
},
"start":30,"end":42,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":27}},
"value": {
"type": "DirectiveLiteral",
"start": 30,
"end": 42,
"loc": {
"start": {
"line": 4,
"column": 15
},
"end": {
"line": 4,
"column": 27
}
},
"start":30,"end":42,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":27}},
"value": "use strict",
"extra": {
"raw": "'use strict'",
@ -203,32 +69,10 @@
},
{
"type": "ExpressionStatement",
"start": 45,
"end": 47,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 2
}
},
"start":45,"end":47,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":2}},
"expression": {
"type": "NumericLiteral",
"start": 45,
"end": 47,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 2
}
},
"start":45,"end":47,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":2}},
"extra": {
"rawValue": 5,
"raw": "05"

View File

@ -1 +1 @@
import('hello', 'world');
import('hello', 'world', '!');

View File

@ -1,21 +1,21 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}},
"errors": [
"SyntaxError: import() requires exactly one argument (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}},
"expression": {
"type": "CallExpression",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
@ -38,6 +38,15 @@
"raw": "'world'"
},
"value": "world"
},
{
"type": "StringLiteral",
"start":25,"end":28,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":28}},
"extra": {
"rawValue": "!",
"raw": "'!'"
},
"value": "!"
}
]
}

View File

@ -0,0 +1 @@
import('hello', 'world');

View File

@ -0,0 +1,5 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:24)",
"sourceType": "module",
"plugins": []
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json";

View File

@ -0,0 +1,5 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)",
"sourceType": "module",
"plugins": []
}

View File

@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } })

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -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": []
}
}

View File

@ -0,0 +1,2 @@
import "x"
with ({});

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
],
"estree"
],
"sourceType": "module"
}

View File

@ -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}}
}
}
]
}
}

View File

@ -0,0 +1,2 @@
import();
import("./foo.json", { with: { type: "json"} }, "unsupported");

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,107 @@
{
"type": "File",
"start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}},
"errors": [
"SyntaxError: import() requires exactly one or two arguments (1:0)",
"SyntaxError: import() requires exactly one or two arguments (2:0)"
],
"program": {
"type": "Program",
"start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"expression": {
"type": "CallExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":10,"end":73,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":63}},
"expression": {
"type": "CallExpression",
"start":10,"end":72,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":62}},
"callee": {
"type": "Import",
"start":10,"end":16,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":17,"end":29,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":19}},
"extra": {
"rawValue": "./foo.json",
"raw": "\"./foo.json\""
},
"value": "./foo.json"
},
{
"type": "ObjectExpression",
"start":31,"end":56,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":46}},
"properties": [
{
"type": "ObjectProperty",
"start":33,"end":54,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":44}},
"method": false,
"key": {
"type": "Identifier",
"start":33,"end":37,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":27},"identifierName":"with"},
"name": "with"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":39,"end":54,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":44}},
"properties": [
{
"type": "ObjectProperty",
"start":41,"end":53,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}},
"method": false,
"key": {
"type": "Identifier",
"start":41,"end":45,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":47,"end":53,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
},
{
"type": "StringLiteral",
"start":58,"end":71,"loc":{"start":{"line":2,"column":48},"end":{"line":2,"column":61}},
"extra": {
"rawValue": "unsupported",
"raw": "\"unsupported\""
},
"value": "unsupported"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import("./foo.json", ...[]);

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

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

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,47 @@
{
"type": "File",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}},
"program": {
"type": "Program",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}},
"specifiers": [],
"source": {
"type": "StringLiteral",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"extra": {
"rawValue": "x",
"raw": "\"x\""
},
"value": "x"
},
"attributes": [
{
"type": "ImportAttribute",
"start":16,"end":28,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}},
"key": {
"type": "Identifier",
"start":16,"end":20,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":22,"end":28,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":12}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}

View File

@ -0,0 +1,12 @@
{
"throws": "Unexpected token (1:15)",
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,2 @@
import("foo.js",);
import("foo.json", { with: { type: "json" } },);

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,110 @@
{
"type": "File",
"start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}},
"program": {
"type": "Program",
"start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"expression": {
"type": "CallExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"extra": {
"trailingComma": 15
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}},
"extra": {
"rawValue": "foo.js",
"raw": "\"foo.js\""
},
"value": "foo.js"
}
]
}
},
{
"type": "ExpressionStatement",
"start":19,"end":67,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":48}},
"expression": {
"type": "CallExpression",
"start":19,"end":66,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":47}},
"callee": {
"type": "Import",
"start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}
},
"extra": {
"trailingComma": 64
},
"arguments": [
{
"type": "StringLiteral",
"start":26,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":38,"end":64,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":45}},
"properties": [
{
"type": "ObjectProperty",
"start":40,"end":62,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":43}},
"method": false,
"key": {
"type": "Identifier",
"start":40,"end":44,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":25},"identifierName":"with"},
"name": "with"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":46,"end":62,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":43}},
"properties": [
{
"type": "ObjectProperty",
"start":48,"end":60,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":41}},
"method": false,
"key": {
"type": "Identifier",
"start":48,"end":52,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":33},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":54,"end":60,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":41}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,2 @@
import "x" with type: "json"
[0]

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,66 @@
{
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}},
"program": {
"type": "Program",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"specifiers": [],
"source": {
"type": "StringLiteral",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"extra": {
"rawValue": "x",
"raw": "\"x\""
},
"value": "x"
},
"attributes": [
{
"type": "ImportAttribute",
"start":16,"end":28,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":28}},
"key": {
"type": "Identifier",
"start":16,"end":20,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":20},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":22,"end":28,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":28}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
},
{
"type": "ExpressionStatement",
"start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}},
"expression": {
"type": "ArrayExpression",
"start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}},
"elements": [
{
"type": "NumericLiteral",
"start":30,"end":31,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json";

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,57 @@
{
"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": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"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"
}
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"attributes": [
{
"type": "ImportAttribute",
"start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}},
"key": {
"type": "Identifier",
"start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json";

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "jan-2020"
}
]
],
"sourceType": "module",
"throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'may-2020'."
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json", lazy: true, startAtLine: 1;

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module",
"throws": "Only string literals are allowed as module attribute values (1:52)"
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with lazy: "true";

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,60 @@
{
"type": "File",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"errors": [
"SyntaxError: The only accepted module attribute is `type` (1:32)"
],
"program": {
"type": "Program",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}},
"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"
}
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"attributes": [
{
"type": "ImportAttribute",
"start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}},
"key": {
"type": "Identifier",
"start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"lazy"},
"name": "lazy"
},
"value": {
"type": "StringLiteral",
"start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}},
"extra": {
"rawValue": "true",
"raw": "\"true\""
},
"value": "true"
}
}
]
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json", hasOwnProperty: "true";

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,78 @@
{
"type": "File",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}},
"errors": [
"SyntaxError: The only accepted module attribute is `type` (1:46)"
],
"program": {
"type": "Program",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}},
"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"
}
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"attributes": [
{
"type": "ImportAttribute",
"start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}},
"key": {
"type": "Identifier",
"start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
},
{
"type": "ImportAttribute",
"start":46,"end":68,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":68}},
"key": {
"type": "Identifier",
"start":46,"end":60,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":60},"identifierName":"hasOwnProperty"},
"name": "hasOwnProperty"
},
"value": {
"type": "StringLiteral",
"start":62,"end":68,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":68}},
"extra": {
"rawValue": "true",
"raw": "\"true\""
},
"value": "true"
}
}
]
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json", type: "html";

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,78 @@
{
"type": "File",
"start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}},
"errors": [
"SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:46)"
],
"program": {
"type": "Program",
"start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}},
"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"
}
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"attributes": [
{
"type": "ImportAttribute",
"start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}},
"key": {
"type": "Identifier",
"start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
},
{
"type": "ImportAttribute",
"start":46,"end":58,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":58}},
"key": {
"type": "Identifier",
"start":46,"end":50,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":50},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":52,"end":58,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":58}},
"extra": {
"rawValue": "html",
"raw": "\"html\""
},
"value": "html"
}
}
]
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import foo from "foo.json";

View File

@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}

View File

@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"program": {
"type": "Program",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"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"
}
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"attributes": []
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import foo from "foo.json" with type: "json";

View File

@ -0,0 +1,4 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)",
"plugins": []
}

View File

@ -114,6 +114,7 @@ export type ParserPlugin =
'importMeta' |
'jsx' |
'logicalAssignment' |
'moduleAttributes' |
'nullishCoalescingOperator' |
'numericSeparator' |
'objectRestSpread' |

View File

@ -0,0 +1,3 @@
*.log
src
test

View File

@ -0,0 +1,19 @@
# @babel/plugin-syntax-module-attributes
> Allow parsing of the module attributes in the import statements
See our website [@babel/plugin-syntax-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-module-attributes.html) for more information.
## Install
Using npm:
```sh
npm install --save-dev @babel/plugin-syntax-module-attributes
```
or using yarn:
```sh
yarn add @babel/plugin-syntax-module-attributes --dev
```

View File

@ -0,0 +1,23 @@
{
"name": "@babel/plugin-syntax-module-attributes",
"version": "7.8.3",
"description": "Allow parsing of the module attributes in the import statement",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-module-attributes",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.8.3"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"devDependencies": {
"@babel/core": "^7.8.3"
}
}

View File

@ -0,0 +1,21 @@
import { declare } from "@babel/helper-plugin-utils";
export default declare((api, { version }) => {
api.assertVersion(7);
if (typeof version !== "string" || version !== "apr-2020") {
throw new Error(
"The 'moduleAttributes' plugin requires a 'version' option," +
" representing the last proposal update. Currently, the" +
" only supported value is 'apr-2020'.",
);
}
return {
name: "syntax-module-attributes",
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push(["moduleAttributes", { version }]);
},
};
});

View File

@ -44,6 +44,7 @@
"@babel/plugin-syntax-function-sent": "^7.8.3",
"@babel/plugin-syntax-import-meta": "^7.8.3",
"@babel/plugin-syntax-jsx": "^7.8.3",
"@babel/plugin-syntax-module-attributes": "^7.8.3",
"@babel/plugin-syntax-object-rest-spread": "^7.8.0",
"@babel/plugin-syntax-optional-catch-binding": "^7.8.0",
"@babel/plugin-syntax-pipeline-operator": "^7.8.3",

View File

@ -10,6 +10,7 @@
"syntax-function-sent",
"syntax-import-meta",
"syntax-jsx",
"syntax-module-attributes",
"syntax-object-rest-spread",
"syntax-optional-catch-binding",
"syntax-pipeline-operator",

View File

@ -14,6 +14,7 @@ import syntaxFunctionBind from "@babel/plugin-syntax-function-bind";
import syntaxFunctionSent from "@babel/plugin-syntax-function-sent";
import syntaxImportMeta from "@babel/plugin-syntax-import-meta";
import syntaxJsx from "@babel/plugin-syntax-jsx";
import syntaxModuleAttributes from "@babel/plugin-syntax-module-attributes";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator";
@ -103,6 +104,7 @@ export {
syntaxFunctionSent,
syntaxImportMeta,
syntaxJsx,
syntaxModuleAttributes,
syntaxObjectRestSpread,
syntaxOptionalCatchBinding,
syntaxPipelineOperator,
@ -193,6 +195,7 @@ export const all = {
"syntax-function-sent": syntaxFunctionSent,
"syntax-import-meta": syntaxImportMeta,
"syntax-jsx": syntaxJsx,
"syntax-module-attributes": syntaxModuleAttributes,
"syntax-object-rest-spread": syntaxObjectRestSpread,
"syntax-optional-catch-binding": syntaxOptionalCatchBinding,
"syntax-pipeline-operator": syntaxPipelineOperator,

View File

@ -10,6 +10,7 @@ export default (_: any, opts: Object = {}) => {
decoratorsBeforeExport,
pipelineProposal = "minimal",
recordAndTupleSyntax: recordAndTupleSyntax = "hash",
moduleAttributesVersion = "may-2020",
} = opts;
return {
@ -20,6 +21,10 @@ export default (_: any, opts: Object = {}) => {
],
],
plugins: [
[
babelPlugins.syntaxModuleAttributes,
{ version: moduleAttributesVersion },
],
[babelPlugins.syntaxRecordAndTuple, { syntaxType: recordAndTupleSyntax }],
babelPlugins.proposalExportDefaultFrom,
[babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal }],