From 82513465ee6b8c7fc010909bdce41f36c17dfb9e Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 6 Nov 2017 17:55:34 -0800 Subject: [PATCH 1/4] Pass an explicit whitelist of options down into babel-generator. --- .../src/transformation/file/generate.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/file/generate.js b/packages/babel-core/src/transformation/file/generate.js index bb42de3d19..39327d75dc 100644 --- a/packages/babel-core/src/transformation/file/generate.js +++ b/packages/babel-core/src/transformation/file/generate.js @@ -21,7 +21,26 @@ export default function generateCode( let { code: outputCode, map: outputMap } = gen( ast, - opts.generatorOpts ? Object.assign(opts, opts.generatorOpts) : opts, + Object.assign( + { + // General generator flags. + filename: opts.filename, + auxiliaryCommentBefore: opts.auxiliaryCommentBefore, + auxiliaryCommentAfter: opts.auxiliaryCommentAfter, + retainLines: opts.retainLines, + comments: opts.comments, + compact: opts.compact, + minified: opts.minified, + concise: opts.concise, + + // Source-map generation flags. + sourceMaps: opts.sourceMaps, + sourceMapTarget: opts.sourceMapTarget, + sourceRoot: opts.sourceRoot, + sourceFileName: opts.sourceFileName, + }, + opts.generatorOpts, + ), code, ); From 6684986372c3c868db58459f6324fe348e709c66 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 6 Nov 2017 17:22:18 -0800 Subject: [PATCH 2/4] Expose an official API to access the current filename. --- .../babel-core/src/config/option-manager.js | 19 ++++++++++--------- .../src/transformation/file/generate.js | 6 +++--- .../src/transformation/normalize-file.js | 2 +- .../src/transformation/normalize-opts.js | 2 +- .../src/transformation/plugin-pass.js | 3 +++ .../src/index.js | 11 ++++++++--- .../src/index.js | 9 ++++----- .../src/index.js | 2 +- 8 files changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index 641440c353..0c4cd84ffa 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -163,17 +163,19 @@ class OptionManager { filenameRelative: opts.filename, }); - const basenameRelative = path.basename(opts.filenameRelative); + if (typeof opts.filenameRelative === "string") { + const basenameRelative = path.basename(opts.filenameRelative); - if (path.extname(opts.filenameRelative) === ".mjs") { - opts.sourceType = "module"; + if (path.extname(opts.filenameRelative) === ".mjs") { + opts.sourceType = "module"; + } + + defaults(opts, { + sourceFileName: basenameRelative, + sourceMapTarget: basenameRelative, + }); } - defaults(opts, { - sourceFileName: basenameRelative, - sourceMapTarget: basenameRelative, - }); - return { options: opts, passes: this.passes, @@ -461,7 +463,6 @@ function createInitialOptions() { return { sourceType: "module", babelrc: true, - filename: "unknown", code: true, ast: true, comments: true, diff --git a/packages/babel-core/src/transformation/file/generate.js b/packages/babel-core/src/transformation/file/generate.js index 39327d75dc..85b072e59b 100644 --- a/packages/babel-core/src/transformation/file/generate.js +++ b/packages/babel-core/src/transformation/file/generate.js @@ -24,7 +24,7 @@ export default function generateCode( Object.assign( { // General generator flags. - filename: opts.filename, + filename: opts.filename || "unknown", auxiliaryCommentBefore: opts.auxiliaryCommentBefore, auxiliaryCommentAfter: opts.auxiliaryCommentAfter, retainLines: opts.retainLines, @@ -35,9 +35,9 @@ export default function generateCode( // Source-map generation flags. sourceMaps: opts.sourceMaps, - sourceMapTarget: opts.sourceMapTarget, + sourceMapTarget: opts.sourceMapTarget || "unknown", sourceRoot: opts.sourceRoot, - sourceFileName: opts.sourceFileName, + sourceFileName: opts.sourceFileName || "unknown", }, opts.generatorOpts, ), diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index 72975a7951..c0722bb62e 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -70,7 +70,7 @@ function parser(options, code) { if (loc) { err.loc = null; err.message = - `${options.filename}: ${err.message}\n` + + `${options.filename || "unknown"}: ${err.message}\n` + codeFrameColumns( code, { diff --git a/packages/babel-core/src/transformation/normalize-opts.js b/packages/babel-core/src/transformation/normalize-opts.js index f9844de43e..591b6c4fbd 100644 --- a/packages/babel-core/src/transformation/normalize-opts.js +++ b/packages/babel-core/src/transformation/normalize-opts.js @@ -7,7 +7,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { parserOpts: Object.assign( { sourceType: config.options.sourceType, - sourceFileName: config.options.filename, + sourceFileName: config.options.filename || "unknown", plugins: [], }, config.options.parserOpts, diff --git a/packages/babel-core/src/transformation/plugin-pass.js b/packages/babel-core/src/transformation/plugin-pass.js index 000f7e4c62..c526d4256b 100644 --- a/packages/babel-core/src/transformation/plugin-pass.js +++ b/packages/babel-core/src/transformation/plugin-pass.js @@ -7,11 +7,14 @@ export default class PluginPass { key: ?string; file: File; opts: Object; + filename: string | void; constructor(file: File, key: ?string, options: ?Object) { this.key = key; this.file = file; this.opts = options || {}; + this.filename = + typeof file.opts.filename === "string" ? file.opts.filename : undefined; } set(key: mixed, val: mixed) { diff --git a/packages/babel-plugin-transform-modules-umd/src/index.js b/packages/babel-plugin-transform-modules-umd/src/index.js index de21197ffc..c806e8c3c6 100644 --- a/packages/babel-plugin-transform-modules-umd/src/index.js +++ b/packages/babel-plugin-transform-modules-umd/src/index.js @@ -44,10 +44,15 @@ export default function({ types: t }, options) { /** * Build the assignment statements that initialize the UMD global. */ - function buildBrowserInit(browserGlobals, exactGlobals, file, moduleName) { + function buildBrowserInit( + browserGlobals, + exactGlobals, + filename, + moduleName, + ) { const moduleNameOrBasename = moduleName ? moduleName.value - : basename(file.opts.filename, extname(file.opts.filename)); + : basename(filename, extname(filename)); let globalToAssign = t.memberExpression( t.identifier("global"), t.identifier(t.toIdentifier(moduleNameOrBasename)), @@ -204,7 +209,7 @@ export default function({ types: t }, options) { GLOBAL_TO_ASSIGN: buildBrowserInit( browserGlobals, exactGlobals, - this.file, + this.filename || "unknown", moduleName, ), }), diff --git a/packages/babel-plugin-transform-react-display-name/src/index.js b/packages/babel-plugin-transform-react-display-name/src/index.js index 6b5a0b5f90..3ed305380f 100644 --- a/packages/babel-plugin-transform-react-display-name/src/index.js +++ b/packages/babel-plugin-transform-react-display-name/src/index.js @@ -52,14 +52,13 @@ export default function({ types: t }) { visitor: { ExportDefaultDeclaration({ node }, state) { if (isCreateClass(node.declaration)) { - let displayName = path.basename( - state.file.opts.filename, - path.extname(state.file.opts.filename), - ); + const filename = state.filename || "unknown"; + + let displayName = path.basename(filename, path.extname(filename)); // ./{module name}/index.js if (displayName === "index") { - displayName = path.basename(path.dirname(state.file.opts.filename)); + displayName = path.basename(path.dirname(filename)); } addDisplayName(displayName, node.declaration); diff --git a/packages/babel-plugin-transform-react-jsx-source/src/index.js b/packages/babel-plugin-transform-react-jsx-source/src/index.js index 76a5cea1ee..d76aed50f8 100644 --- a/packages/babel-plugin-transform-react-jsx-source/src/index.js +++ b/packages/babel-plugin-transform-react-jsx-source/src/index.js @@ -50,7 +50,7 @@ export default function({ types: t }) { } if (!state.fileNameIdentifier) { - const fileName = state.file.opts.filename || ""; + const fileName = state.filename || ""; const fileNameIdentifier = path.scope.generateUidIdentifier( FILE_NAME_VAR, From 39119f88e734704b95a3bb61706a130ce8dbf565 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 8 Nov 2017 13:35:34 -0800 Subject: [PATCH 3/4] Pass explicit options list to babel-code-frame --- packages/babel-core/src/transformation/file/file.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index 2c026fd2c5..f9fa1f2f5d 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -212,7 +212,9 @@ export default class File { column: loc.column + 1, }, }, - this.opts, + { + highlightCode: this.opts.highlightCode, + }, ); } From 4d7d4605b66725fe9708e8719ea8e374131e5f94 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 8 Nov 2017 15:45:23 -0800 Subject: [PATCH 4/4] Centralize parserOpts and generatorOpts normalization. --- .../src/transformation/file/generate.js | 25 +------------- .../src/transformation/normalize-opts.js | 34 +++++++++++++++---- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/packages/babel-core/src/transformation/file/generate.js b/packages/babel-core/src/transformation/file/generate.js index 85b072e59b..e31c1726a1 100644 --- a/packages/babel-core/src/transformation/file/generate.js +++ b/packages/babel-core/src/transformation/file/generate.js @@ -19,30 +19,7 @@ export default function generateCode( gen = opts.generatorOpts.generator; } - let { code: outputCode, map: outputMap } = gen( - ast, - Object.assign( - { - // General generator flags. - filename: opts.filename || "unknown", - auxiliaryCommentBefore: opts.auxiliaryCommentBefore, - auxiliaryCommentAfter: opts.auxiliaryCommentAfter, - retainLines: opts.retainLines, - comments: opts.comments, - compact: opts.compact, - minified: opts.minified, - concise: opts.concise, - - // Source-map generation flags. - sourceMaps: opts.sourceMaps, - sourceMapTarget: opts.sourceMapTarget || "unknown", - sourceRoot: opts.sourceRoot, - sourceFileName: opts.sourceFileName || "unknown", - }, - opts.generatorOpts, - ), - code, - ); + let { code: outputCode, map: outputMap } = gen(ast, opts.generatorOpts, code); if (shebang) { // add back shebang diff --git a/packages/babel-core/src/transformation/normalize-opts.js b/packages/babel-core/src/transformation/normalize-opts.js index 591b6c4fbd..5b08e4f9a2 100644 --- a/packages/babel-core/src/transformation/normalize-opts.js +++ b/packages/babel-core/src/transformation/normalize-opts.js @@ -3,19 +3,41 @@ import type { ResolvedConfig } from "../config"; export default function normalizeOptions(config: ResolvedConfig): {} { - const options = Object.assign({}, config.options, { + const opts = config.options; + + const options = Object.assign({}, opts, { parserOpts: Object.assign( { - sourceType: config.options.sourceType, - sourceFileName: config.options.filename || "unknown", + sourceType: opts.sourceType, + sourceFileName: opts.filename || "unknown", plugins: [], }, - config.options.parserOpts, + opts.parserOpts, + ), + generatorOpts: Object.assign( + { + // General generator flags. + filename: opts.filename || "unknown", + auxiliaryCommentBefore: opts.auxiliaryCommentBefore, + auxiliaryCommentAfter: opts.auxiliaryCommentAfter, + retainLines: opts.retainLines, + comments: opts.comments, + compact: opts.compact, + minified: opts.minified, + concise: opts.concise, + + // Source-map generation flags. + sourceMaps: opts.sourceMaps, + sourceMapTarget: opts.sourceMapTarget || "unknown", + sourceRoot: opts.sourceRoot, + sourceFileName: opts.sourceFileName || "unknown", + }, + opts.generatorOpts, ), }); - for (const pluginPairs of config.passes) { - for (const plugin of pluginPairs) { + for (const plugins of config.passes) { + for (const plugin of plugins) { if (plugin.manipulateOptions) { plugin.manipulateOptions(options, options.parserOpts); }