From ca67637da4167b8499d67f85e33726d1291787f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 30 Jun 2019 11:16:05 +0200 Subject: [PATCH] [@babel/parser] Add "allowUndeclaredExports" option (#9864) Ref: https://github.com/babel/notes/blob/master/2019/04/09.md#export-changes-in-scope-pr-break-meteor-issue --- packages/babel-parser/src/options.js | 3 + packages/babel-parser/src/parser/statement.js | 6 +- .../core/opts/allowUndeclaredExports/input.js | 1 + .../opts/allowUndeclaredExports/options.json | 4 + .../opts/allowUndeclaredExports/output.json | 103 ++++++++++++++++++ 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/input.js create mode 100644 packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/options.json create mode 100644 packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/output.json diff --git a/packages/babel-parser/src/options.js b/packages/babel-parser/src/options.js index 5a615ccddf..8b030847f9 100755 --- a/packages/babel-parser/src/options.js +++ b/packages/babel-parser/src/options.js @@ -15,6 +15,7 @@ export type Options = { allowReturnOutsideFunction: boolean, allowImportExportEverywhere: boolean, allowSuperOutsideMethod: boolean, + allowUndeclaredExports: boolean, plugins: PluginList, strictMode: ?boolean, ranges: boolean, @@ -41,6 +42,8 @@ export const defaultOptions: Options = { allowImportExportEverywhere: false, // TODO allowSuperOutsideMethod: false, + // When enabled, export statements can reference undeclared variables. + allowUndeclaredExports: false, // An array of plugins to enable plugins: [], // TODO diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index ad29af2529..e5b8086c04 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -45,7 +45,11 @@ export default class StatementParser extends ExpressionParser { this.parseBlockBody(program, true, true, tt.eof); - if (this.inModule && this.scope.undefinedExports.size > 0) { + if ( + this.inModule && + !this.options.allowUndeclaredExports && + this.scope.undefinedExports.size > 0 + ) { for (const [name] of Array.from(this.scope.undefinedExports)) { const pos = this.scope.undefinedExports.get(name); // $FlowIssue diff --git a/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/input.js b/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/input.js new file mode 100644 index 0000000000..f604032cb6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/input.js @@ -0,0 +1 @@ +export { foo }; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/options.json b/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/options.json new file mode 100644 index 0000000000..2de0f0b2d6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "allowUndeclaredExports": true +} diff --git a/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/output.json b/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/output.json new file mode 100644 index 0000000000..a454ef5bbd --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/opts/allowUndeclaredExports/output.json @@ -0,0 +1,103 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "local": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} \ No newline at end of file