Skip to content

Commit

Permalink
added basic support for module attributes and tests updated (#10962)
Browse files Browse the repository at this point in the history
Co-Authored-By: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
vivek12345 and nicolo-ribaudo committed May 24, 2020
1 parent c5ba345 commit 66b86e0
Show file tree
Hide file tree
Showing 66 changed files with 1,231 additions and 184 deletions.
22 changes: 16 additions & 6 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion packages/babel-parser/src/parser/location.js
Expand Up @@ -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"'`,
Expand Down Expand Up @@ -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",
Expand Down
71 changes: 71 additions & 0 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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 '...'
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-parser/src/plugin-utils.js
Expand Up @@ -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(
Expand Down

0 comments on commit 66b86e0

Please sign in to comment.