From 4f72232ca92631166ad305f6c9d81a0962649a2b Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sun, 12 Mar 2017 23:25:30 -0700 Subject: [PATCH] Move option parsing to babel-cli. --- packages/babel-cli/src/babel/index.js | 40 ++++++++----- packages/babel-core/src/index.js | 1 - .../src/transformation/file/index.js | 4 -- .../src/transformation/file/options/config.js | 57 ++++--------------- .../src/transformation/file/options/index.js | 22 ------- .../file/options/option-manager.js | 18 +++++- .../transformation/file/options/parsers.js | 16 ------ packages/babel-core/test/api.js | 18 +++--- .../plugins/inference-recursion/exec.js | 16 +++--- .../multiple-definition-evaluation/exec.js | 18 +++--- .../plugins/nested-if-alternate/exec.js | 28 ++++----- 11 files changed, 95 insertions(+), 143 deletions(-) delete mode 100644 packages/babel-core/src/transformation/file/options/index.js delete mode 100644 packages/babel-core/src/transformation/file/options/parsers.js diff --git a/packages/babel-cli/src/babel/index.js b/packages/babel-cli/src/babel/index.js index 0159cf49d2..99da42c6df 100755 --- a/packages/babel-cli/src/babel/index.js +++ b/packages/babel-cli/src/babel/index.js @@ -2,7 +2,7 @@ import fs from "fs"; import commander from "commander"; -import { options, util, version } from "babel-core"; +import { util, version } from "babel-core"; import uniq from "lodash/uniq"; import glob from "glob"; @@ -14,11 +14,11 @@ import pkg from "../../package.json"; /* eslint-disable max-len */ // Standard Babel input configs. commander.option("-f, --filename [filename]", "filename to use when reading from stdin - this will be used in source-maps, errors etc"); -commander.option("--presets [list]", ""); -commander.option("--plugins [list]", ""); +commander.option("--presets [list]", "comma-separated list of preset names"); +commander.option("--plugins [list]", "comma-separated list of plugin names"); // Basic file input configuration. -commander.option("--source-type [string]", ""); +commander.option("--source-type [script|module]", ""); commander.option("--no-babelrc", "Whether or not to look up .babelrc and .babelignore files"); commander.option("--ignore [list]", "list of glob paths to **not** compile"); commander.option("--only [list]", "list of glob paths to **only** compile"); @@ -29,13 +29,13 @@ commander.option("--no-highlight-code", "enable/disable ANSI syntax highlighting // General output formatting. commander.option("--no-comments", "write comments to generated output (true by default)"); commander.option("--retain-lines", "retain line numbers - will result in really ugly code"); -commander.option("--compact [booleanString]", "do not include superfluous whitespace characters and line terminators [true|false|auto]"); +commander.option("--compact [true|false|auto]", "do not include superfluous whitespace characters and line terminators"); commander.option("--minified", "save as much bytes when printing [true|false]"); commander.option("--auxiliary-comment-before [string]", "print a comment before any injected non-user code"); commander.option("--auxiliary-comment-after [string]", "print a comment after any injected non-user code"); // General soucemap formatting. -commander.option("-s, --source-maps [booleanString]", "[true|false|inline]"); +commander.option("-s, --source-maps [true|false|inline|both]", ""); commander.option("--source-map-target [string]", "set `file` on returned source map"); commander.option("--source-file-name [string]", "set `sources[0]` on returned source map"); commander.option("--source-root [filename]", "the root from which all sources are relative"); @@ -112,14 +112,23 @@ if (errors.length) { // -const opts = {}; +const opts = commander.opts(); -Object.keys(options).forEach(function (key) { - const opt = options[key]; - if (commander[key] !== undefined && commander[key] !== opt.default) { - opts[key] = commander[key]; - } -}); +// Delete options that are specific to babel-cli and shouldn't be passed to babel-core. +delete opts.version; +delete opts.extensions; +delete opts.watch; +delete opts.skipInitialBuild; +delete opts.outFile; +delete opts.outDir; +delete opts.copyFiles; +delete opts.quiet; + +// Commander will default the "--no-" arguments to true, but we want to leave them undefined so that +// babel-core can handle the default-assignment logic on its own. +if (opts.babelrc === true) opts.babelrc = undefined; +if (opts.comments === true) opts.comments = undefined; +if (opts.highlightCode === true) opts.highlightCode = undefined; opts.ignore = util.arrayify(opts.ignore, util.regexify); @@ -127,5 +136,10 @@ if (opts.only) { opts.only = util.arrayify(opts.only, util.regexify); } +if (opts.sourceMaps) opts.sourceMaps = util.booleanify(opts.sourceMaps); +if (opts.compact) opts.compact = util.booleanify(opts.compact); +if (opts.presets) opts.presets = util.list(opts.presets); +if (opts.plugins) opts.plugins = util.list(opts.plugins); + const fn = commander.outDir ? dirCommand : fileCommand; fn(commander, filenames, opts); diff --git a/packages/babel-core/src/index.js b/packages/babel-core/src/index.js index 1efc832d1f..ced0312a2d 100644 --- a/packages/babel-core/src/index.js +++ b/packages/babel-core/src/index.js @@ -1,7 +1,6 @@ import fs from "fs"; export { default as File } from "./transformation/file"; -export { default as options } from "./transformation/file/options/config"; export { default as buildExternalHelpers } from "./tools/build-external-helpers"; export { default as template } from "babel-template"; export { default as resolvePlugin } from "./helpers/resolve-plugin"; diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index f11e750303..17beab202c 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -143,10 +143,6 @@ export default class File extends Store { opts.basename = path.basename(opts.filename, path.extname(opts.filename)); - opts.ignore = util.arrayify(opts.ignore, util.regexify); - - if (opts.only) opts.only = util.arrayify(opts.only, util.regexify); - defaults(opts, { moduleRoot: opts.sourceRoot, }); diff --git a/packages/babel-core/src/transformation/file/options/config.js b/packages/babel-core/src/transformation/file/options/config.js index 61de15bebb..aa955a5f96 100644 --- a/packages/babel-core/src/transformation/file/options/config.js +++ b/packages/babel-core/src/transformation/file/options/config.js @@ -2,13 +2,10 @@ export default { filename: { - type: "filename", default: "unknown", }, - filenameRelative: { - type: "string", - }, + filenameRelative: {}, inputSourceMap: {}, @@ -19,60 +16,46 @@ export default { mode: {}, retainLines: { - type: "boolean", default: false, }, highlightCode: { - type: "boolean", default: true, }, suppressDeprecationMessages: { - type: "boolean", default: false, }, presets: { - type: "list", default: [], }, plugins: { - type: "list", default: [], }, ignore: { - type: "list", default: [], }, - only: { - type: "list", - }, + only: {}, code: { default: true, - type: "boolean", }, metadata: { default: true, - type: "boolean", }, ast: { default: true, - type: "boolean", }, - extends: { - type: "string", - }, + extends: {}, comments: { - type: "boolean", default: true, }, @@ -81,12 +64,10 @@ export default { wrapPluginVisitorMethod: {}, compact: { - type: "booleanString", default: "auto", }, minified: { - type: "boolean", default: false, }, @@ -95,24 +76,16 @@ export default { }, sourceMaps: { - type: "booleanString", default: false, }, - sourceMapTarget: { - type: "string", - }, + sourceMapTarget: {}, - sourceFileName: { - type: "string", - }, + sourceFileName: {}, - sourceRoot: { - type: "filename", - }, + sourceRoot: {}, babelrc: { - type: "boolean", default: true, }, @@ -120,33 +93,23 @@ export default { default: "module", }, - auxiliaryCommentBefore: { - type: "string", - }, + auxiliaryCommentBefore: {}, - auxiliaryCommentAfter: { - type: "string", - }, + auxiliaryCommentAfter: {}, resolveModuleSource: {}, getModuleId: {}, - moduleRoot: { - type: "filename", - }, + moduleRoot: {}, moduleIds: { - type: "boolean", default: false, }, - moduleId: { - type: "string", - }, + moduleId: {}, passPerPreset: { - type: "boolean", default: false, }, diff --git a/packages/babel-core/src/transformation/file/options/index.js b/packages/babel-core/src/transformation/file/options/index.js deleted file mode 100644 index c5f8a73c51..0000000000 --- a/packages/babel-core/src/transformation/file/options/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import * as parsers from "./parsers"; -import config from "./config"; - -export { config }; - -export function normaliseOptions(options: Object = {}): Object { - for (const key in options) { - let val = options[key]; - if (val == null) continue; - - let opt = config[key]; - if (opt && opt.alias) opt = config[opt.alias]; - if (!opt) continue; - - const parser = parsers[opt.type]; - if (parser) val = parser(val); - - options[key] = val; - } - - return options; -} diff --git a/packages/babel-core/src/transformation/file/options/option-manager.js b/packages/babel-core/src/transformation/file/options/option-manager.js index 762a643784..84ffedce33 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -1,7 +1,6 @@ import * as context from "../../../index"; import Plugin from "../../plugin"; import * as messages from "babel-messages"; -import { normaliseOptions } from "./index"; import resolvePlugin from "../../../helpers/resolve-plugin"; import resolvePreset from "../../../helpers/resolve-preset"; import cloneDeepWith from "lodash/cloneDeepWith"; @@ -11,6 +10,7 @@ import config from "./config"; import removed from "./removed"; import buildConfigChain from "./build-config-chain"; import path from "path"; +import * as util from "../../../util"; type PluginObject = { pre?: Function; @@ -185,16 +185,28 @@ export default class OptionManager { } } - // normalise options - normaliseOptions(opts); + if (opts.ignore) { + if (!Array.isArray(rawOpts.ignore)) throw new Error(`${alias}.ignore should be an array`); + + opts.ignore = opts.ignore.map(util.regexify); + } + if (opts.only) { + if (!Array.isArray(rawOpts.only)) throw new Error(`${alias}.only should be an array`); + + opts.only = opts.only.map(util.regexify); + } // resolve plugins if (opts.plugins) { + if (!Array.isArray(rawOpts.plugins)) throw new Error(`${alias}.plugins should be an array`); + opts.plugins = OptionManager.normalisePlugins(loc, dirname, opts.plugins); } // resolve presets if (opts.presets) { + if (!Array.isArray(rawOpts.presets)) throw new Error(`${alias}.presets should be an array`); + // If we're in the "pass per preset" mode, we resolve the presets // and keep them for further execution to calculate the options. if (opts.passPerPreset) { diff --git a/packages/babel-core/src/transformation/file/options/parsers.js b/packages/babel-core/src/transformation/file/options/parsers.js deleted file mode 100644 index 2dcf5b7a71..0000000000 --- a/packages/babel-core/src/transformation/file/options/parsers.js +++ /dev/null @@ -1,16 +0,0 @@ -import slash from "slash"; -import * as util from "../../../util"; - -export const filename = slash; - -export function boolean(val: any): boolean { - return !!val; -} - -export function booleanString(val: any): boolean | any { - return util.booleanify(val); -} - -export function list(val: any): Array { - return util.list(val); -} diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 097b515b1a..3d181488c3 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -496,17 +496,17 @@ describe("api", function () { it("ignore option", function () { return Promise.all([ transformAsync("", { - ignore: "node_modules", + ignore: ["node_modules"], filename: "/foo/node_modules/bar", }).then(assertIgnored), transformAsync("", { - ignore: "foo/node_modules", + ignore: ["foo/node_modules"], filename: "/foo/node_modules/bar", }).then(assertIgnored), transformAsync("", { - ignore: "foo/node_modules/*.bar", + ignore: ["foo/node_modules/*.bar"], filename: "/foo/node_modules/foo.bar", }).then(assertIgnored), ]); @@ -515,32 +515,32 @@ describe("api", function () { it("only option", function () { return Promise.all([ transformAsync("", { - only: "node_modules", + only: ["node_modules"], filename: "/foo/node_modules/bar", }).then(assertNotIgnored), transformAsync("", { - only: "foo/node_modules", + only: ["foo/node_modules"], filename: "/foo/node_modules/bar", }).then(assertNotIgnored), transformAsync("", { - only: "foo/node_modules/*.bar", + only: ["foo/node_modules/*.bar"], filename: "/foo/node_modules/foo.bar", }).then(assertNotIgnored), transformAsync("", { - only: "node_modules", + only: ["node_modules"], filename: "/foo/node_module/bar", }).then(assertIgnored), transformAsync("", { - only: "foo/node_modules", + only: ["foo/node_modules"], filename: "/bar/node_modules/foo", }).then(assertIgnored), transformAsync("", { - only: "foo/node_modules/*.bar", + only: ["foo/node_modules/*.bar"], filename: "/foo/node_modules/bar.foo", }).then(assertIgnored), ]); diff --git a/packages/babel-core/test/fixtures/plugins/inference-recursion/exec.js b/packages/babel-core/test/fixtures/plugins/inference-recursion/exec.js index f643ac27d0..0091a95bcd 100644 --- a/packages/babel-core/test/fixtures/plugins/inference-recursion/exec.js +++ b/packages/babel-core/test/fixtures/plugins/inference-recursion/exec.js @@ -44,14 +44,16 @@ var code = (function() { }).toString().split('\n').slice(1, -1).join('\n'); transform(code, { - plugins: function (b) { - var t = b.types; - return { - visitor: { - BinaryExpression: function(path) { - path.get("left").baseTypeStrictlyMatches(path.get("right")); + plugins: [ + function (b) { + var t = b.types; + return { + visitor: { + BinaryExpression: function(path) { + path.get("left").baseTypeStrictlyMatches(path.get("right")); + } } } } - } + ] }); diff --git a/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js b/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js index de27da3c0b..522d97637c 100644 --- a/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js +++ b/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js @@ -5,14 +5,16 @@ var code = multiline([ ]); transform(code, { - plugins: function (b) { - var t = b.types; - return { - visitor: { - ConditionalExpression: function(path) { - path.get("test").evaluateTruthy(); + plugins: [ + function (b) { + var t = b.types; + return { + visitor: { + ConditionalExpression: function(path) { + path.get("test").evaluateTruthy(); + } } } - } - } + }, + ], }); diff --git a/packages/babel-core/test/fixtures/plugins/nested-if-alternate/exec.js b/packages/babel-core/test/fixtures/plugins/nested-if-alternate/exec.js index c9dae86ffa..6bc6fccc2d 100644 --- a/packages/babel-core/test/fixtures/plugins/nested-if-alternate/exec.js +++ b/packages/babel-core/test/fixtures/plugins/nested-if-alternate/exec.js @@ -1,19 +1,21 @@ var res = transform('', { - plugins: function (b) { - var t = b.types; - return { - visitor: { - Program: function(path) { - if (this.done) return; - // if (false) { if (true) 42; } else 23; - var inner = t.ifStatement(t.booleanLiteral(true), t.expressionStatement(t.numericLiteral(42)), null); - var outer = t.ifStatement(t.booleanLiteral(false), inner, t.expressionStatement(t.numericLiteral(23))); - path.replaceWith(t.program([outer])); - this.done = true; + plugins: [ + function (b) { + var t = b.types; + return { + visitor: { + Program: function(path) { + if (this.done) return; + // if (false) { if (true) 42; } else 23; + var inner = t.ifStatement(t.booleanLiteral(true), t.expressionStatement(t.numericLiteral(42)), null); + var outer = t.ifStatement(t.booleanLiteral(false), inner, t.expressionStatement(t.numericLiteral(23))); + path.replaceWith(t.program([outer])); + this.done = true; + } } } - } - } + }, + ], }); assert.equal(eval(res.code), 23);