Parse import-assertions (#12139)
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com> Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
@@ -829,13 +829,18 @@ export default class ExpressionParser extends LValParser {
|
||||
): N.Expression {
|
||||
if (node.callee.type === "Import") {
|
||||
if (node.arguments.length === 2) {
|
||||
this.expectPlugin("moduleAttributes");
|
||||
// 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("moduleAttributes")
|
||||
this.hasPlugin("importAssertions") ||
|
||||
this.hasPlugin("moduleAttributes")
|
||||
? "one or two arguments"
|
||||
: "one argument",
|
||||
);
|
||||
@@ -872,7 +877,11 @@ export default class ExpressionParser extends LValParser {
|
||||
} else {
|
||||
this.expect(tt.comma);
|
||||
if (this.match(close)) {
|
||||
if (dynamicImport && !this.hasPlugin("moduleAttributes")) {
|
||||
if (
|
||||
dynamicImport &&
|
||||
!this.hasPlugin("importAssertions") &&
|
||||
!this.hasPlugin("moduleAttributes")
|
||||
) {
|
||||
this.raise(
|
||||
this.state.lastTokStart,
|
||||
Errors.ImportCallArgumentTrailingComma,
|
||||
|
||||
@@ -2144,12 +2144,21 @@ export default class StatementParser extends ExpressionParser {
|
||||
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;
|
||||
// https://github.com/tc39/proposal-import-assertions
|
||||
// parse module import assertions if the next token is `assert` or ignore
|
||||
// and finish the ImportDeclaration node.
|
||||
const assertions = this.maybeParseImportAssertions();
|
||||
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");
|
||||
}
|
||||
@@ -2180,6 +2189,69 @@ export default class StatementParser extends ExpressionParser {
|
||||
node.specifiers.push(this.finishNode(specifier, type));
|
||||
}
|
||||
|
||||
parseAssertEntries() {
|
||||
this.expectPlugin("importAssertions");
|
||||
|
||||
const attrs = [];
|
||||
const attrNames = new Set();
|
||||
|
||||
do {
|
||||
if (this.match(tt.braceR)) {
|
||||
break;
|
||||
}
|
||||
|
||||
const node = this.startNode();
|
||||
|
||||
// parse AssertionKey : IdentifierName, StringLiteral
|
||||
let assertionKeyNode;
|
||||
if (this.match(tt.string)) {
|
||||
assertionKeyNode = this.parseLiteral(this.state.value, "StringLiteral");
|
||||
} else {
|
||||
assertionKeyNode = this.parseIdentifier(true);
|
||||
}
|
||||
this.next();
|
||||
node.key = assertionKeyNode;
|
||||
|
||||
// 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 (attrNames.has(node.key.name)) {
|
||||
this.raise(
|
||||
node.key.start,
|
||||
Errors.ModuleAttributesWithDuplicateKeys,
|
||||
node.key.name,
|
||||
);
|
||||
}
|
||||
attrNames.add(node.key.name);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
@@ -2191,13 +2263,9 @@ export default class StatementParser extends ExpressionParser {
|
||||
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,
|
||||
@@ -2206,9 +2274,6 @@ export default class StatementParser extends ExpressionParser {
|
||||
);
|
||||
}
|
||||
|
||||
// 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,
|
||||
@@ -2218,7 +2283,6 @@ export default class StatementParser extends ExpressionParser {
|
||||
}
|
||||
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,
|
||||
@@ -2233,6 +2297,26 @@ export default class StatementParser extends ExpressionParser {
|
||||
return attrs;
|
||||
}
|
||||
|
||||
maybeParseImportAssertions() {
|
||||
if (
|
||||
this.match(tt.name) &&
|
||||
this.state.value === "assert" &&
|
||||
!this.hasPrecedingLineBreak()
|
||||
) {
|
||||
this.expectPlugin("importAssertions");
|
||||
this.next();
|
||||
} else {
|
||||
if (this.hasPlugin("importAssertions")) return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
this.eat(tt.braceL);
|
||||
const attrs = this.parseAssertEntries();
|
||||
this.eat(tt.braceR);
|
||||
|
||||
return attrs;
|
||||
}
|
||||
|
||||
maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean {
|
||||
if (this.shouldParseDefaultImport(node)) {
|
||||
// import defaultObj, { x, y as z } from '...'
|
||||
|
||||
@@ -87,6 +87,11 @@ export function validatePlugins(plugins: PluginList) {
|
||||
}
|
||||
|
||||
if (hasPlugin(plugins, "moduleAttributes")) {
|
||||
if (hasPlugin(plugins, "importAssertions")) {
|
||||
throw new Error(
|
||||
"Cannot combine importAssertions and moduleAttributes plugins.",
|
||||
);
|
||||
}
|
||||
const moduleAttributesVerionPluginOption = getPluginOption(
|
||||
plugins,
|
||||
"moduleAttributes",
|
||||
@@ -100,6 +105,7 @@ export function validatePlugins(plugins: PluginList) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
hasPlugin(plugins, "recordAndTuple") &&
|
||||
!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(
|
||||
|
||||
Reference in New Issue
Block a user