From dfc4b618561ec6827dbc785d8efdc76dcb757ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 26 Oct 2022 15:35:22 -0400 Subject: [PATCH] Parse import reflection (#14926) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- babel.config.js | 2 +- .../babel-generator/src/generators/modules.ts | 5 ++ .../import-reflection-inner-comment/input.mjs | 1 + .../options.json | 3 + .../import-reflection-inner-comment/output.js | 1 + .../fixtures/import-reflection/basic/input.js | 1 + .../import-reflection/basic/output.js | 1 + .../fixtures/import-reflection/options.json | 3 + packages/babel-parser/data/schema.json | 1 + packages/babel-parser/src/index.ts | 4 +- .../src/parse-error/standard-errors.ts | 3 + packages/babel-parser/src/parser/statement.ts | 53 +++++++++++++++ .../babel-parser/src/plugins/flow/index.ts | 11 ++++ .../src/plugins/typescript/index.ts | 11 ++++ packages/babel-parser/src/tokenizer/types.ts | 8 +-- packages/babel-parser/src/types.d.ts | 1 + packages/babel-parser/src/typings.d.ts | 1 + .../_no-plugin/import-reflection/input.mjs | 1 + .../_no-plugin/import-reflection/options.json | 3 + .../invalid-assertions/input.mjs | 1 + .../invalid-assertions/options.json | 4 ++ .../invalid-assertions/output.json | 61 +++++++++++++++++ .../invalid-default-named-import/input.mjs | 1 + .../invalid-default-named-import/output.json | 55 ++++++++++++++++ .../invalid-export-from/input.mjs | 1 + .../invalid-export-from/options.json | 8 +++ .../invalid-flow-type-import-1/input.mjs | 1 + .../invalid-flow-type-import-1/options.json | 8 +++ .../invalid-flow-type-import-2/input.mjs | 1 + .../invalid-flow-type-import-2/options.json | 4 ++ .../invalid-flow-type-import-2/output.json | 42 ++++++++++++ .../invalid-flow-typeof-import-1/input.mjs | 1 + .../invalid-flow-typeof-import-1/options.json | 8 +++ .../invalid-flow-typeof-import-2/input.mjs | 1 + .../invalid-flow-typeof-import-2/options.json | 4 ++ .../invalid-flow-typeof-import-2/output.json | 42 ++++++++++++ .../invalid-identifier-source/input.mjs | 1 + .../invalid-identifier-source/options.json | 3 + .../invalid-module-escape/input.mjs | 1 + .../invalid-module-escape/options.json | 3 + .../invalid-module-source/input.mjs | 1 + .../invalid-module-source/options.json | 3 + .../invalid-named-import/input.mjs | 1 + .../invalid-named-import/output.json | 46 +++++++++++++ .../invalid-namespace-import/input.mjs | 1 + .../invalid-namespace-import/output.json | 41 ++++++++++++ .../invalid-ts-type-import-1/input.mts | 1 + .../invalid-ts-type-import-1/options.json | 4 ++ .../invalid-ts-type-import-1/output.json | 42 ++++++++++++ .../invalid-ts-type-import-2/input.mts | 1 + .../invalid-ts-type-import-2/options.json | 8 +++ .../import-reflection/options.json | 4 ++ .../valid-default-import/input.mjs | 2 + .../valid-default-import/output.json | 63 ++++++++++++++++++ .../valid-flow-default-import/input.mts | 2 + .../valid-flow-default-import/options.json | 4 ++ .../valid-flow-default-import/output.json | 65 +++++++++++++++++++ .../input.mjs | 1 + .../output.json | 38 +++++++++++ .../input.mjs | 1 + .../output.json | 38 +++++++++++ .../valid-module-as-default-binding/input.mjs | 1 + .../output.json | 38 +++++++++++ .../valid-ts-default-import/input.mts | 2 + .../valid-ts-default-import/options.json | 4 ++ .../valid-ts-default-import/output.json | 65 +++++++++++++++++++ .../README.md | 19 ++++++ .../package.json | 50 ++++++++++++++ .../src/index.ts | 13 ++++ packages/babel-standalone/package.json | 1 + .../scripts/pluginConfig.json | 3 +- .../babel-standalone/src/generated/plugins.ts | 9 ++- .../babel-standalone/src/preset-stage-2.ts | 1 + .../src/ast-types/generated/index.ts | 1 + packages/babel-types/src/definitions/core.ts | 4 ++ tsconfig.json | 4 ++ yarn.lock | 12 ++++ 77 files changed, 948 insertions(+), 11 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/input.mjs create mode 100644 packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/options.json create mode 100644 packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/output.js create mode 100644 packages/babel-generator/test/fixtures/import-reflection/basic/input.js create mode 100644 packages/babel-generator/test/fixtures/import-reflection/basic/output.js create mode 100644 packages/babel-generator/test/fixtures/import-reflection/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/input.mts create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/input.mts create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/input.mts create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/input.mts create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/output.json create mode 100644 packages/babel-plugin-syntax-import-reflection/README.md create mode 100644 packages/babel-plugin-syntax-import-reflection/package.json create mode 100644 packages/babel-plugin-syntax-import-reflection/src/index.ts diff --git a/babel.config.js b/babel.config.js index e4c369d7d6f7..206537d9417d 100644 --- a/babel.config.js +++ b/babel.config.js @@ -772,7 +772,7 @@ function getTokenTypesMapping() { ); } - const tokenTypesDefinition = typesDeclaration.init.properties; + const tokenTypesDefinition = typesDeclaration.init.expression.properties; for (let i = 0; i < tokenTypesDefinition.length; i++) { tokenTypesMapping.set(tokenTypesDefinition[i].key.name, i); } diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index 33b754920382..1ccd29d45719 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -201,8 +201,13 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { const isTypeKind = node.importKind === "type" || node.importKind === "typeof"; if (isTypeKind) { + this.printInnerComments(node, false); this.word(node.importKind); this.space(); + } else if (node.module) { + this.printInnerComments(node, false); + this.word("module"); + this.space(); } const specifiers = node.specifiers.slice(0); diff --git a/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/input.mjs b/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/input.mjs new file mode 100644 index 000000000000..225520995e5e --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/input.mjs @@ -0,0 +1 @@ +/* 0 */import /* 1 */module /* 2 */from /* 3 */from /* 4 */"./module.wasm"/* 5 */; diff --git a/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/options.json b/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/options.json new file mode 100644 index 000000000000..3890afedd2e0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["importReflection"] +} diff --git a/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/output.js b/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/output.js new file mode 100644 index 000000000000..088c141403a0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/import-reflection-inner-comment/output.js @@ -0,0 +1 @@ +/* 0 */import /* 1 */module /* 2 */from /* 3 */ from /* 4 */"./module.wasm" /* 5 */; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/import-reflection/basic/input.js b/packages/babel-generator/test/fixtures/import-reflection/basic/input.js new file mode 100644 index 000000000000..0c84d385708e --- /dev/null +++ b/packages/babel-generator/test/fixtures/import-reflection/basic/input.js @@ -0,0 +1 @@ +import module foo from "./module.wasm"; diff --git a/packages/babel-generator/test/fixtures/import-reflection/basic/output.js b/packages/babel-generator/test/fixtures/import-reflection/basic/output.js new file mode 100644 index 000000000000..aace725125c1 --- /dev/null +++ b/packages/babel-generator/test/fixtures/import-reflection/basic/output.js @@ -0,0 +1 @@ +import module foo from "./module.wasm"; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/import-reflection/options.json b/packages/babel-generator/test/fixtures/import-reflection/options.json new file mode 100644 index 000000000000..3890afedd2e0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/import-reflection/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["importReflection"] +} diff --git a/packages/babel-parser/data/schema.json b/packages/babel-parser/data/schema.json index 0fbcf6d9e2a3..d4b6810da732 100644 --- a/packages/babel-parser/data/schema.json +++ b/packages/babel-parser/data/schema.json @@ -142,6 +142,7 @@ "functionSent", "importAssertions", "importMeta", + "importReflection", "jsx", "logicalAssignment", "moduleStringNames", diff --git a/packages/babel-parser/src/index.ts b/packages/babel-parser/src/index.ts index 4c81655b7758..a22baff1ba48 100644 --- a/packages/babel-parser/src/index.ts +++ b/packages/babel-parser/src/index.ts @@ -80,7 +80,9 @@ function generateExportedTokenTypes( internalTokenTypes: InternalTokenTypes, ): Record { const tokenTypes: Record = {}; - for (const typeName of Object.keys(internalTokenTypes)) { + for (const typeName of Object.keys( + internalTokenTypes, + ) as (keyof InternalTokenTypes)[]) { tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]); } return tokenTypes; diff --git a/packages/babel-parser/src/parse-error/standard-errors.ts b/packages/babel-parser/src/parse-error/standard-errors.ts index e50a5a33e007..9b69912655f1 100644 --- a/packages/babel-parser/src/parse-error/standard-errors.ts +++ b/packages/babel-parser/src/parse-error/standard-errors.ts @@ -117,6 +117,9 @@ export default { ImportCallSpreadArgument: "`...` is not allowed in `import()`.", ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.", + ImportReflectionHasAssertion: "`import module x` cannot have assertions.", + ImportReflectionNotBinding: + 'Only `import module x from "./module"` is valid.', IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.", InvalidBigIntLiteral: "Invalid BigIntLiteral.", diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index 8fe7f45e423f..0b46aa96ae15 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -2654,6 +2654,24 @@ export default abstract class StatementParser extends ExpressionParser { return false; } + checkImportReflection(node: Undone) { + if (node.module) { + if ( + node.specifiers.length !== 1 || + node.specifiers[0].type !== "ImportDefaultSpecifier" + ) { + this.raise(Errors.ImportReflectionNotBinding, { + at: node.specifiers[0].loc.start, + }); + } + if (node.assertions?.length > 0) { + this.raise(Errors.ImportReflectionHasAssertion, { + at: node.specifiers[0].loc.start, + }); + } + } + } + checkJSONModuleImport( node: Undone< N.ExportAllDeclaration | N.ExportNamedDeclaration | N.ImportDeclaration @@ -2687,6 +2705,39 @@ export default abstract class StatementParser extends ExpressionParser { } } + parseMaybeImportReflection(node: Undone) { + let isImportReflection = false; + if (this.isContextual(tt._module)) { + const lookahead = this.lookahead(); + if (tokenIsIdentifier(lookahead.type)) { + if (lookahead.type !== tt._from) { + // import module x + isImportReflection = true; + } else { + const nextNextTokenFirstChar = this.input.charCodeAt( + this.nextTokenStartSince(lookahead.end), + ); + if (nextNextTokenFirstChar === charCodes.lowercaseF) { + // import module from from ... + isImportReflection = true; + } + } + } else { + // import module { x } ... + // This is invalid, we will continue parsing and throw + // a recoverable error later + isImportReflection = true; + } + } + if (isImportReflection) { + this.expectPlugin("importReflection"); + this.next(); // eat tt._module; + node.module = true; + } else if (this.hasPlugin("importReflection")) { + node.module = false; + } + } + // Parses import declaration. // https://tc39.es/ecma262/#prod-ImportDeclaration @@ -2694,6 +2745,7 @@ export default abstract class StatementParser extends ExpressionParser { // import '...' node.specifiers = []; if (!this.match(tt.string)) { + this.parseMaybeImportReflection(node); // check if we have a default import like // import React from "react"; const hasDefault = this.maybeParseDefaultImportSpecifier(node); @@ -2726,6 +2778,7 @@ export default abstract class StatementParser extends ExpressionParser { node.attributes = attributes; } } + this.checkImportReflection(node); this.checkJSONModuleImport(node); this.semicolon(); diff --git a/packages/babel-parser/src/plugins/flow/index.ts b/packages/babel-parser/src/plugins/flow/index.ts index 7cc0a5afb648..17bc42bd07b0 100644 --- a/packages/babel-parser/src/plugins/flow/index.ts +++ b/packages/babel-parser/src/plugins/flow/index.ts @@ -164,6 +164,8 @@ const FlowErrors = ParseErrorEnum`flow`({ }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`, GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.", + ImportReflectionHasImportType: + "An `import module` declaration can not use `type` or `typeof` keyword.", ImportTypeShorthandOnlyInPureImport: "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements.", InexactInsideExact: @@ -2718,6 +2720,15 @@ export default (superClass: typeof Parser) => return isMaybeDefaultImport(this.state.type); } + checkImportReflection(node: Undone) { + super.checkImportReflection(node); + if (node.module && node.importKind !== "value") { + this.raise(FlowErrors.ImportReflectionHasImportType, { + at: node.specifiers[0].loc.start, + }); + } + } + parseImportSpecifierLocal< T extends | N.ImportSpecifier diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index ca7d2d846e1a..5d5cdd283a8f 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -121,6 +121,8 @@ const TSErrors = ParseErrorEnum`typescript`({ ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.", ImportAliasHasImportType: "An import alias can not use 'import type'.", + ImportReflectionHasImportType: + "An `import module` declaration can not use `type` modifier", IncompatibleModifiers: ({ modifiers, }: { @@ -2603,6 +2605,15 @@ export default (superClass: ClassWithMixin) => } } + checkImportReflection(node: Undone) { + super.checkImportReflection(node); + if (node.module && node.importKind !== "value") { + this.raise(TSErrors.ImportReflectionHasImportType, { + at: node.specifiers[0].loc.start, + }); + } + } + /* Don't bother doing this check in TypeScript code because: 1. We may have a nested export statement with the same name: diff --git a/packages/babel-parser/src/tokenizer/types.ts b/packages/babel-parser/src/tokenizer/types.ts index ca7bdc071f27..59369d35cb4e 100644 --- a/packages/babel-parser/src/tokenizer/types.ts +++ b/packages/babel-parser/src/tokenizer/types.ts @@ -134,11 +134,9 @@ function createKeywordLike( // For performance the token type helpers depend on the following declarations order. // When adding new token types, please also check if the token helpers need update. -export type InternalTokenTypes = { - [name: string]: TokenType; -}; +export type InternalTokenTypes = typeof tt; -export const tt: InternalTokenTypes = { +export const tt = { // Punctuation token types. bracketL: createToken("[", { beforeExpr, startsExpr }), bracketHashL: createToken("#[", { beforeExpr, startsExpr }), @@ -346,7 +344,7 @@ export const tt: InternalTokenTypes = { // placeholder plugin placeholder: createToken("%%", { startsExpr: true }), -}; +} as const; export function tokenIsIdentifier(token: TokenType): boolean { return token >= tt._as && token <= tt.name; diff --git a/packages/babel-parser/src/types.d.ts b/packages/babel-parser/src/types.d.ts index 6356bf8f6549..a154322c2927 100644 --- a/packages/babel-parser/src/types.d.ts +++ b/packages/babel-parser/src/types.d.ts @@ -922,6 +922,7 @@ export interface ImportDeclaration extends NodeBase { source: Literal; importKind?: "type" | "typeof" | "value"; // TODO: Not in spec, assertions?: ImportAttribute[]; + module?: boolean; } export interface ImportSpecifier extends ModuleSpecifier { diff --git a/packages/babel-parser/src/typings.d.ts b/packages/babel-parser/src/typings.d.ts index 6cf279daa349..a437901fc45a 100644 --- a/packages/babel-parser/src/typings.d.ts +++ b/packages/babel-parser/src/typings.d.ts @@ -22,6 +22,7 @@ export type Plugin = | "jsx" | "logicalAssignment" | "importAssertions" + | "importReflection" | "moduleBlocks" | "moduleStringNames" | "nullishCoalescingOperator" diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/input.mjs b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/input.mjs new file mode 100644 index 000000000000..0c84d385708e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/input.mjs @@ -0,0 +1 @@ +import module foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/options.json new file mode 100644 index 000000000000..626c04783d02 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-reflection/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"importReflection\". (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/input.mjs new file mode 100644 index 000000000000..3db160892a93 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/input.mjs @@ -0,0 +1 @@ +import module foo from "./module.json" assert { type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/options.json new file mode 100644 index 000000000000..343fca529107 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection", "importAssertions"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/output.json new file mode 100644 index 000000000000..d659444d99bf --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-assertions/output.json @@ -0,0 +1,61 @@ +{ + "type": "File", + "start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":62,"index":62}}, + "errors": [ + "SyntaxError: `import module x` cannot have assertions. (1:14)" + ], + "program": { + "type": "Program", + "start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":62,"index":62}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":62,"index":62}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, + "local": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":23,"end":38,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":38,"index":38}}, + "extra": { + "rawValue": "./module.json", + "raw": "\"./module.json\"" + }, + "value": "./module.json" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":48,"end":60,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":60,"index":60}}, + "key": { + "type": "Identifier", + "start":48,"end":52,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":52,"index":52},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":54,"end":60,"loc":{"start":{"line":1,"column":54,"index":54},"end":{"line":1,"column":60,"index":60}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/input.mjs new file mode 100644 index 000000000000..d66223d4f3d1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/input.mjs @@ -0,0 +1 @@ +import module foo, {bar} from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/output.json new file mode 100644 index 000000000000..b3e3b377cc37 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-default-named-import/output.json @@ -0,0 +1,55 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "errors": [ + "SyntaxError: Only `import module x from \"./module\"` is valid. (1:14)" + ], + "program": { + "type": "Program", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, + "local": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, + "name": "foo" + } + }, + { + "type": "ImportSpecifier", + "start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23}}, + "imported": { + "type": "Identifier", + "start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23},"identifierName":"bar"}, + "name": "bar" + }, + "local": { + "type": "Identifier", + "start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23},"identifierName":"bar"}, + "name": "bar" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":30,"end":45,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":45,"index":45}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/input.mjs new file mode 100644 index 000000000000..4f76efb00148 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/input.mjs @@ -0,0 +1 @@ +export module x from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/options.json new file mode 100644 index 000000000000..8aa67ebb74aa --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-export-from/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "importReflection", + "exportDefaultFrom" + ], + "sourceType": "module", + "throws": "Unexpected token, expected \"{\" (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/input.mjs new file mode 100644 index 000000000000..b1b4eccaaf47 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/input.mjs @@ -0,0 +1 @@ +import type module foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/options.json new file mode 100644 index 000000000000..2360f761dd93 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-1/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "importReflection", + "flow" + ], + "sourceType": "module", + "throws": "Unexpected token, expected \"from\" (1:19)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/input.mjs new file mode 100644 index 000000000000..bb7a737e565e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/input.mjs @@ -0,0 +1 @@ +import module type foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/options.json new file mode 100644 index 000000000000..2dd1ff29f752 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection", "flow"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/output.json new file mode 100644 index 000000000000..1e45993a703d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-type-import-2/output.json @@ -0,0 +1,42 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "errors": [ + "SyntaxError: An `import module` declaration can not use `type` or `typeof` keyword. (1:19)" + ], + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22}}, + "local": { + "type": "Identifier", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "importKind": "type", + "source": { + "type": "StringLiteral", + "start":28,"end":43,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":43,"index":43}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/input.mjs new file mode 100644 index 000000000000..36d70c71a925 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/input.mjs @@ -0,0 +1 @@ +import typeof module foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/options.json new file mode 100644 index 000000000000..bfd6a7a73ddd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-1/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "importReflection", + "flow" + ], + "sourceType": "module", + "throws": "Unexpected token, expected \"from\" (1:21)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/input.mjs new file mode 100644 index 000000000000..08a67d55b3c7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/input.mjs @@ -0,0 +1 @@ +import module typeof foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/options.json new file mode 100644 index 000000000000..2dd1ff29f752 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection", "flow"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/output.json new file mode 100644 index 000000000000..fffe18921787 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-flow-typeof-import-2/output.json @@ -0,0 +1,42 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "errors": [ + "SyntaxError: An `import module` declaration can not use `type` or `typeof` keyword. (1:21)" + ], + "program": { + "type": "Program", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":21,"end":24,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":24,"index":24}}, + "local": { + "type": "Identifier", + "start":21,"end":24,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":24,"index":24},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "importKind": "typeof", + "source": { + "type": "StringLiteral", + "start":30,"end":45,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":45,"index":45}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/input.mjs new file mode 100644 index 000000000000..59b145183cb6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/input.mjs @@ -0,0 +1 @@ +import module from foo diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/options.json new file mode 100644 index 000000000000..20c22df0b79e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-identifier-source/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"from\" (1:19)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/input.mjs new file mode 100644 index 000000000000..358c34647b08 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/input.mjs @@ -0,0 +1 @@ +import modul\u0065 foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/options.json new file mode 100644 index 000000000000..20c22df0b79e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-escape/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"from\" (1:19)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/input.mjs new file mode 100644 index 000000000000..663ccd64954c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/input.mjs @@ -0,0 +1 @@ +import module "foo"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/options.json new file mode 100644 index 000000000000..0756c2d80455 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-module-source/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"{\" (1:14)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/input.mjs new file mode 100644 index 000000000000..bb7ab691bc6c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/input.mjs @@ -0,0 +1 @@ +import module { foo } from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/output.json new file mode 100644 index 000000000000..3bf434fb3470 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-named-import/output.json @@ -0,0 +1,46 @@ +{ + "type": "File", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":43,"index":43}}, + "errors": [ + "SyntaxError: Only `import module x from \"./module\"` is valid. (1:16)" + ], + "program": { + "type": "Program", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":43,"index":43}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":43,"index":43}}, + "specifiers": [ + { + "type": "ImportSpecifier", + "start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19}}, + "imported": { + "type": "Identifier", + "start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19},"identifierName":"foo"}, + "name": "foo" + }, + "local": { + "type": "Identifier", + "start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":27,"end":42,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":42,"index":42}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/input.mjs new file mode 100644 index 000000000000..a8e4a46aea75 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/input.mjs @@ -0,0 +1 @@ +import module * as foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/output.json new file mode 100644 index 000000000000..26852d193fea --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-namespace-import/output.json @@ -0,0 +1,41 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "errors": [ + "SyntaxError: Only `import module x from \"./module\"` is valid. (1:14)" + ], + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start":14,"end":22,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":22,"index":22}}, + "local": { + "type": "Identifier", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":28,"end":43,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":43,"index":43}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/input.mts b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/input.mts new file mode 100644 index 000000000000..b1b4eccaaf47 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/input.mts @@ -0,0 +1 @@ +import type module foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/options.json new file mode 100644 index 000000000000..43d760edb3d7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection", "typescript"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/output.json new file mode 100644 index 000000000000..c443420a3c87 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-1/output.json @@ -0,0 +1,42 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "errors": [ + "SyntaxError: An `import module` declaration can not use `type` modifier (1:19)" + ], + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "importKind": "type", + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22}}, + "local": { + "type": "Identifier", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":28,"end":43,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":43,"index":43}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/input.mts b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/input.mts new file mode 100644 index 000000000000..bb7a737e565e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/input.mts @@ -0,0 +1 @@ +import module type foo from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/options.json new file mode 100644 index 000000000000..d2768d790303 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/invalid-ts-type-import-2/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "importReflection", + "typescript" + ], + "sourceType": "module", + "throws": "Unexpected token, expected \"from\" (1:19)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/options.json new file mode 100644 index 000000000000..1ca990789fef --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/input.mjs new file mode 100644 index 000000000000..b0f11212d097 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/input.mjs @@ -0,0 +1,2 @@ +import module foo from "./module.wasm"; +import bar from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/output.json new file mode 100644 index 000000000000..73ea501e6017 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-default-import/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":72,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":32,"index":72}}, + "program": { + "type": "Program", + "start":0,"end":72,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":32,"index":72}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, + "local": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":23,"end":38,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":38,"index":38}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + }, + { + "type": "ImportDeclaration", + "start":40,"end":72,"loc":{"start":{"line":2,"column":0,"index":40},"end":{"line":2,"column":32,"index":72}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":47,"end":50,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":10,"index":50}}, + "local": { + "type": "Identifier", + "start":47,"end":50,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":10,"index":50},"identifierName":"bar"}, + "name": "bar" + } + } + ], + "module": false, + "source": { + "type": "StringLiteral", + "start":56,"end":71,"loc":{"start":{"line":2,"column":16,"index":56},"end":{"line":2,"column":31,"index":71}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/input.mts b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/input.mts new file mode 100644 index 000000000000..b0f11212d097 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/input.mts @@ -0,0 +1,2 @@ +import module foo from "./module.wasm"; +import bar from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/options.json new file mode 100644 index 000000000000..2dd1ff29f752 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection", "flow"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/output.json new file mode 100644 index 000000000000..6d61e19297e9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-flow-default-import/output.json @@ -0,0 +1,65 @@ +{ + "type": "File", + "start":0,"end":72,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":32,"index":72}}, + "program": { + "type": "Program", + "start":0,"end":72,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":32,"index":72}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, + "local": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "importKind": "value", + "source": { + "type": "StringLiteral", + "start":23,"end":38,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":38,"index":38}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + }, + { + "type": "ImportDeclaration", + "start":40,"end":72,"loc":{"start":{"line":2,"column":0,"index":40},"end":{"line":2,"column":32,"index":72}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":47,"end":50,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":10,"index":50}}, + "local": { + "type": "Identifier", + "start":47,"end":50,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":10,"index":50},"identifierName":"bar"}, + "name": "bar" + } + } + ], + "module": false, + "importKind": "value", + "source": { + "type": "StringLiteral", + "start":56,"end":71,"loc":{"start":{"line":2,"column":16,"index":56},"end":{"line":2,"column":31,"index":71}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/input.mjs new file mode 100644 index 000000000000..6185d417527e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/input.mjs @@ -0,0 +1 @@ +import module \u0066rom from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/output.json new file mode 100644 index 000000000000..72ef89a839f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding-escaped/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}}, + "program": { + "type": "Program", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":23,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":23,"index":23}}, + "local": { + "type": "Identifier", + "start":14,"end":23,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":23,"index":23},"identifierName":"from"}, + "name": "from" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":29,"end":44,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":44,"index":44}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/input.mjs new file mode 100644 index 000000000000..bac2eb722087 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/input.mjs @@ -0,0 +1 @@ +import module from from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/output.json new file mode 100644 index 000000000000..d1cc1d26068d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-from-as-default-module-binding/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":40,"index":40}}, + "program": { + "type": "Program", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":40,"index":40}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":40,"index":40}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":18,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":18,"index":18}}, + "local": { + "type": "Identifier", + "start":14,"end":18,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":18,"index":18},"identifierName":"from"}, + "name": "from" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":24,"end":39,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":39,"index":39}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/input.mjs new file mode 100644 index 000000000000..bd14dd36b0c3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/input.mjs @@ -0,0 +1 @@ +import module from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/output.json new file mode 100644 index 000000000000..4fceda93d417 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":13,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":13,"index":13}}, + "local": { + "type": "Identifier", + "start":7,"end":13,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":13,"index":13},"identifierName":"module"}, + "name": "module" + } + } + ], + "module": false, + "source": { + "type": "StringLiteral", + "start":19,"end":34,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":34,"index":34}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/input.mts b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/input.mts new file mode 100644 index 000000000000..b0f11212d097 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/input.mts @@ -0,0 +1,2 @@ +import module foo from "./module.wasm"; +import bar from "./module.wasm"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/options.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/options.json new file mode 100644 index 000000000000..43d760edb3d7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["importReflection", "typescript"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/output.json new file mode 100644 index 000000000000..40d43769defe --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-ts-default-import/output.json @@ -0,0 +1,65 @@ +{ + "type": "File", + "start":0,"end":72,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":32,"index":72}}, + "program": { + "type": "Program", + "start":0,"end":72,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":32,"index":72}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "importKind": "value", + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, + "local": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "module": true, + "source": { + "type": "StringLiteral", + "start":23,"end":38,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":38,"index":38}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + }, + { + "type": "ImportDeclaration", + "start":40,"end":72,"loc":{"start":{"line":2,"column":0,"index":40},"end":{"line":2,"column":32,"index":72}}, + "importKind": "value", + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":47,"end":50,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":10,"index":50}}, + "local": { + "type": "Identifier", + "start":47,"end":50,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":10,"index":50},"identifierName":"bar"}, + "name": "bar" + } + } + ], + "module": false, + "source": { + "type": "StringLiteral", + "start":56,"end":71,"loc":{"start":{"line":2,"column":16,"index":56},"end":{"line":2,"column":31,"index":71}}, + "extra": { + "rawValue": "./module.wasm", + "raw": "\"./module.wasm\"" + }, + "value": "./module.wasm" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-plugin-syntax-import-reflection/README.md b/packages/babel-plugin-syntax-import-reflection/README.md new file mode 100644 index 000000000000..589f6615d8ba --- /dev/null +++ b/packages/babel-plugin-syntax-import-reflection/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-import-reflection + +> Allow parsing of the reflection attributes in the import statement + +See our website [@babel/plugin-syntax-import-reflection](https://babeljs.io/docs/en/babel-plugin-syntax-import-reflection) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-import-reflection +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-import-reflection --dev +``` diff --git a/packages/babel-plugin-syntax-import-reflection/package.json b/packages/babel-plugin-syntax-import-reflection/package.json new file mode 100644 index 000000000000..6e3906871e07 --- /dev/null +++ b/packages/babel-plugin-syntax-import-reflection/package.json @@ -0,0 +1,50 @@ +{ + "name": "@babel/plugin-syntax-import-reflection", + "version": "7.18.6", + "description": "Allow parsing of the reflection attributes in the import statement", + "repository": { + "type": "git", + "url": "https://github.com/babel/babel.git", + "directory": "packages/babel-plugin-syntax-import-reflection" + }, + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "./lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "workspace:^" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + }, + "devDependencies": { + "@babel/core": "workspace:^" + }, + "engines": { + "node": ">=6.9.0" + }, + "author": "The Babel Team (https://babel.dev/team)", + "conditions": { + "BABEL_8_BREAKING": [ + null, + { + "exports": null + } + ], + "USE_ESM": [ + { + "type": "module" + }, + null + ] + }, + "exports": { + ".": "./lib/index.js", + "./package.json": "./package.json" + }, + "type": "commonjs" +} diff --git a/packages/babel-plugin-syntax-import-reflection/src/index.ts b/packages/babel-plugin-syntax-import-reflection/src/index.ts new file mode 100644 index 000000000000..d371a47f3840 --- /dev/null +++ b/packages/babel-plugin-syntax-import-reflection/src/index.ts @@ -0,0 +1,13 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare(api => { + api.assertVersion(7); + + return { + name: "syntax-import-reflection", + + manipulateOptions(_, parserOpts) { + parserOpts.plugins.push("importReflection"); + }, + }; +}); diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index 41f119faf666..20a31bf1e823 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -47,6 +47,7 @@ "@babel/plugin-syntax-function-bind": "workspace:^", "@babel/plugin-syntax-function-sent": "workspace:^", "@babel/plugin-syntax-import-assertions": "workspace:^", + "@babel/plugin-syntax-import-reflection": "workspace:^", "@babel/plugin-syntax-jsx": "workspace:^", "@babel/plugin-syntax-module-blocks": "workspace:^", "@babel/plugin-syntax-pipeline-operator": "workspace:^", diff --git a/packages/babel-standalone/scripts/pluginConfig.json b/packages/babel-standalone/scripts/pluginConfig.json index 8ea84da6e37d..f459b6c6b54f 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -28,9 +28,10 @@ "syntax-flow", "syntax-function-bind", "syntax-function-sent", + "syntax-import-assertions", + "syntax-import-reflection", "syntax-module-blocks", "syntax-jsx", - "syntax-import-assertions", "syntax-pipeline-operator", "syntax-record-and-tuple", "syntax-typescript", diff --git a/packages/babel-standalone/src/generated/plugins.ts b/packages/babel-standalone/src/generated/plugins.ts index 977eee8a7892..5b32ab70f61d 100644 --- a/packages/babel-standalone/src/generated/plugins.ts +++ b/packages/babel-standalone/src/generated/plugins.ts @@ -12,9 +12,10 @@ import syntaxExportDefaultFrom from "@babel/plugin-syntax-export-default-from"; import syntaxFlow from "@babel/plugin-syntax-flow"; import syntaxFunctionBind from "@babel/plugin-syntax-function-bind"; import syntaxFunctionSent from "@babel/plugin-syntax-function-sent"; +import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions"; +import syntaxImportReflection from "@babel/plugin-syntax-import-reflection"; import syntaxModuleBlocks from "@babel/plugin-syntax-module-blocks"; import syntaxJsx from "@babel/plugin-syntax-jsx"; -import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions"; import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator"; import syntaxRecordAndTuple from "@babel/plugin-syntax-record-and-tuple"; import syntaxTypescript from "@babel/plugin-syntax-typescript"; @@ -112,9 +113,10 @@ export { syntaxFlow, syntaxFunctionBind, syntaxFunctionSent, + syntaxImportAssertions, + syntaxImportReflection, syntaxModuleBlocks, syntaxJsx, - syntaxImportAssertions, syntaxPipelineOperator, syntaxRecordAndTuple, syntaxTypescript, @@ -213,9 +215,10 @@ export const all: { [k: string]: any } = { "syntax-flow": syntaxFlow, "syntax-function-bind": syntaxFunctionBind, "syntax-function-sent": syntaxFunctionSent, + "syntax-import-assertions": syntaxImportAssertions, + "syntax-import-reflection": syntaxImportReflection, "syntax-module-blocks": syntaxModuleBlocks, "syntax-jsx": syntaxJsx, - "syntax-import-assertions": syntaxImportAssertions, "syntax-pipeline-operator": syntaxPipelineOperator, "syntax-record-and-tuple": syntaxRecordAndTuple, "syntax-typescript": syntaxTypescript, diff --git a/packages/babel-standalone/src/preset-stage-2.ts b/packages/babel-standalone/src/preset-stage-2.ts index c38a0d2f7991..5e4aabec0b0d 100644 --- a/packages/babel-standalone/src/preset-stage-2.ts +++ b/packages/babel-standalone/src/preset-stage-2.ts @@ -35,6 +35,7 @@ export default (_: any, opts: any = {}) => { { syntaxType: recordAndTupleSyntax }, ], babelPlugins.syntaxModuleBlocks, + babelPlugins.syntaxImportReflection, ], }; }; diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index 6beb4233a2ef..5c7467faf699 100644 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -857,6 +857,7 @@ export interface ImportDeclaration extends BaseNode { source: StringLiteral; assertions?: Array | null; importKind?: "type" | "typeof" | "value" | null; + module?: boolean | null; } export interface ImportDefaultSpecifier extends BaseNode { diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index c9c9b580dc68..457a5ff49dae 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -1695,6 +1695,10 @@ defineType("ImportDeclaration", { assertEach(assertNodeType("ImportAttribute")), ), }, + module: { + optional: true, + validate: assertValueType("boolean"), + }, specifiers: { validate: chain( assertValueType("array"), diff --git a/tsconfig.json b/tsconfig.json index 8869ed393c1e..4dd62fd01e8e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -81,6 +81,7 @@ "./packages/babel-plugin-syntax-function-bind/src/**/*.ts", "./packages/babel-plugin-syntax-function-sent/src/**/*.ts", "./packages/babel-plugin-syntax-import-assertions/src/**/*.ts", + "./packages/babel-plugin-syntax-import-reflection/src/**/*.ts", "./packages/babel-plugin-syntax-jsx/src/**/*.ts", "./packages/babel-plugin-syntax-module-blocks/src/**/*.ts", "./packages/babel-plugin-syntax-partial-application/src/**/*.ts", @@ -407,6 +408,9 @@ "@babel/plugin-syntax-import-assertions": [ "./packages/babel-plugin-syntax-import-assertions/src" ], + "@babel/plugin-syntax-import-reflection": [ + "./packages/babel-plugin-syntax-import-reflection/src" + ], "@babel/plugin-syntax-jsx": [ "./packages/babel-plugin-syntax-jsx/src" ], diff --git a/yarn.lock b/yarn.lock index 706ca4ad5578..89e0af6ed6db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1996,6 +1996,17 @@ __metadata: languageName: node linkType: hard +"@babel/plugin-syntax-import-reflection@workspace:^, @babel/plugin-syntax-import-reflection@workspace:packages/babel-plugin-syntax-import-reflection": + version: 0.0.0-use.local + resolution: "@babel/plugin-syntax-import-reflection@workspace:packages/babel-plugin-syntax-import-reflection" + dependencies: + "@babel/core": "workspace:^" + "@babel/helper-plugin-utils": "workspace:^" + peerDependencies: + "@babel/core": ^7.0.0-0 + languageName: unknown + linkType: soft + "@babel/plugin-syntax-json-strings@npm:^7.8.3": version: 7.8.3 resolution: "@babel/plugin-syntax-json-strings@npm:7.8.3" @@ -3652,6 +3663,7 @@ __metadata: "@babel/plugin-syntax-function-bind": "workspace:^" "@babel/plugin-syntax-function-sent": "workspace:^" "@babel/plugin-syntax-import-assertions": "workspace:^" + "@babel/plugin-syntax-import-reflection": "workspace:^" "@babel/plugin-syntax-jsx": "workspace:^" "@babel/plugin-syntax-module-blocks": "workspace:^" "@babel/plugin-syntax-pipeline-operator": "workspace:^"