From 92f28706c1b978497afc71d142789c8ba3e4acd2 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 13 Nov 2018 13:26:03 +0700 Subject: [PATCH] Various code style improvements --- .gitattributes | 3 +- cli-main.js | 63 +++++++++--------- config/overrides.js | 1 + config/plugins.js | 80 +++++++++++++++++----- index.js | 71 +++++++++++--------- lib/options-manager.js | 144 ++++++++++++++++++++-------------------- package.json | 1 - test/cli-main.js | 2 +- test/cli.js | 2 +- test/options-manager.js | 73 ++++++++++++++------ 10 files changed, 262 insertions(+), 178 deletions(-) diff --git a/.gitattributes b/.gitattributes index 6fa9c460..7fc4baee 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,2 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf *.ai binary diff --git a/cli-main.js b/cli-main.js index 5eca96a1..206fd328 100755 --- a/cli-main.js +++ b/cli-main.js @@ -112,71 +112,70 @@ const cli = meow(` updateNotifier({pkg: cli.pkg}).notify(); -const {input, flags: opts} = cli; - -// Make data types for `opts.space` match those of the API -// Check for string type because `xo --no-space` sets `opts.space` to `false` -if (typeof opts.space === 'string') { - if (/^\d+$/u.test(opts.space)) { - opts.space = parseInt(opts.space, 10); - } else if (opts.space === 'true') { - opts.space = true; - } else if (opts.space === 'false') { - opts.space = false; +const {input, flags: options} = cli; + +// Make data types for `options.space` match those of the API +// Check for string type because `xo --no-space` sets `options.space` to `false` +if (typeof options.space === 'string') { + if (/^\d+$/u.test(options.space)) { + options.space = parseInt(options.space, 10); + } else if (options.space === 'true') { + options.space = true; + } else if (options.space === 'false') { + options.space = false; } else { - if (opts.space !== '') { - // Assume `opts.space` was set to a filename when run as `xo --space file.js` - input.push(opts.space); + if (options.space !== '') { + // Assume `options.space` was set to a filename when run as `xo --space file.js` + input.push(options.space); } - opts.space = true; + options.space = true; } } const log = report => { - const reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty; - + const reporter = options.reporter ? xo.getFormatter(options.reporter) : formatterPretty; process.stdout.write(reporter(report.results)); process.exit(report.errorCount === 0 ? 0 : 1); }; // `xo -` => `xo --stdin` if (input[0] === '-') { - opts.stdin = true; + options.stdin = true; input.shift(); } -if (opts.nodeVersion) { - if (opts.nodeVersion === 'false') { - opts.nodeVersion = false; - } else if (!semver.validRange(opts.nodeVersion)) { +if (options.nodeVersion) { + if (options.nodeVersion === 'false') { + options.nodeVersion = false; + } else if (!semver.validRange(options.nodeVersion)) { console.error('The `node-engine` option must be a valid semver range (for example `>=6`)'); process.exit(1); } } -if (opts.init) { +if (options.init) { require('xo-init')(); -} else if (opts.stdin) { - getStdin().then(str => { - if (opts.fix) { - console.log(xo.lintText(str, opts).results[0].output); +} else if (options.stdin) { + getStdin().then(stdin => { + if (options.fix) { + console.log(xo.lintText(stdin, options).results[0].output); return; } - if (opts.open) { + if (options.open) { console.error('The `open` option is not supported on stdin'); process.exit(1); } - log(xo.lintText(str, opts)); + log(xo.lintText(stdin, options)); }); } else { - xo.lintFiles(input, opts).then(report => { - if (opts.fix) { + xo.lintFiles(input, options).then(report => { + if (options.fix) { xo.outputFixes(report); } - if (opts.open) { + if (options.open) { openReport(report); } diff --git a/config/overrides.js b/config/overrides.js index 728355e6..f181757c 100644 --- a/config/overrides.js +++ b/config/overrides.js @@ -1,4 +1,5 @@ 'use strict'; + module.exports = { // Put rule overrides here }; diff --git a/config/plugins.js b/config/plugins.js index c6c18a55..3eb04b89 100644 --- a/config/plugins.js +++ b/config/plugins.js @@ -1,4 +1,5 @@ 'use strict'; + module.exports = { // Repeated here from eslint-config-xo in case some plugins set something different parserOptions: { @@ -8,7 +9,7 @@ module.exports = { jsx: true } }, - // -- end repeat + // -- End repeat plugins: [ 'no-use-extend-native', 'ava', @@ -30,20 +31,37 @@ module.exports = { rules: { 'no-use-extend-native/no-use-extend-native': 'error', 'promise/param-names': 'error', - 'promise/no-return-wrap': ['error', {allowReject: true}], + 'promise/no-return-wrap': [ + 'error', + { + allowReject: true + } + ], 'promise/no-new-statics': 'error', 'promise/no-return-in-finally': 'error', 'promise/valid-params': 'error', + + // TODO: Enable this when XO targets Node.js 8 + // 'promise/prefer-await-to-then': 'error', + 'import/default': 'error', 'import/export': 'error', - 'import/extensions': ['error', { - js: 'never', - json: 'never', - jsx: 'never' - }], + 'import/extensions': [ + 'error', + { + js: 'never', + json: 'never', + jsx: 'never' + } + ], 'import/first': 'error', 'import/named': 'error', - 'import/namespace': ['error', {allowComputed: true}], + 'import/namespace': [ + 'error', + { + allowComputed: true + } + ], 'import/no-absolute-path': 'error', 'import/no-webpack-loader-syntax': 'error', 'import/no-self-import': 'error', @@ -69,11 +87,24 @@ module.exports = { 'import/no-mutable-exports': 'error', 'import/no-named-as-default-member': 'error', 'import/no-named-as-default': 'error', - 'import/no-unresolved': ['error', {commonjs: true}], + 'import/no-unresolved': [ + 'error', + { + commonjs: true + } + ], 'import/order': 'error', - 'import/no-unassigned-import': ['error', { - allow: ['babel-polyfill', '@babel/polyfill', 'babel-register', '@babel/register'] - }], + 'import/no-unassigned-import': [ + 'error', + { + allow: [ + 'babel-polyfill', + '@babel/polyfill', + 'babel-register', + '@babel/register' + ] + } + ], // Redundant with import/no-extraneous-dependencies // 'node/no-extraneous-import': 'error', @@ -87,8 +118,24 @@ module.exports = { // Disabled because they're too annoying, see: // https://github.com/mysticatea/eslint-plugin-node/issues/105 - // 'node/no-unpublished-import': ['error', {allowModules: ['electron', 'atom']}], - // 'node/no-unpublished-require': ['error', {allowModules: ['electron', 'atom']}], + // 'node/no-unpublished-import': [ + // 'error', + // { + // allowModules: [ + // 'electron', + // 'atom' + // ] + // } + // ], + // 'node/no-unpublished-require': [ + // 'error', + // { + // allowModules: [ + // 'electron', + // 'atom' + // ] + // } + // ], // Disabled as the rule doesn't allow to exclude compiled sources // 'node/no-unsupported-features': 'error', @@ -101,7 +148,10 @@ module.exports = { 'node/no-deprecated-api': 'error', // Disabled because it causes too much churn and will be moot when we switch to ES2015 modules - // 'node/exports-style': ['error', 'module.exports'] + // 'node/exports-style': [ + // 'error', + // 'module.exports' + // ] 'node/prefer-global/buffer': [ 'error', diff --git a/index.js b/index.js index 494196dc..8a0c7672 100644 --- a/index.js +++ b/index.js @@ -26,44 +26,45 @@ const mergeReports = reports => { }; }; -const processReport = (report, opts) => { - report.results = opts.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results; +const processReport = (report, options) => { + report.results = options.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results; return report; }; -const runEslint = (paths, opts) => { - const config = optionsManager.buildConfig(opts); +const runEslint = (paths, options) => { + const config = optionsManager.buildConfig(options); const engine = new eslint.CLIEngine(config); const report = engine.executeOnFiles(paths, config); - - return processReport(report, opts); + return processReport(report, options); }; -module.exports.lintText = (str, opts) => { - opts = optionsManager.preprocess(opts); +module.exports.lintText = (str, options) => { + options = optionsManager.preprocess(options); - if (opts.overrides && opts.overrides.length > 0) { - const {overrides} = opts; - delete opts.overrides; + if (options.overrides && options.overrides.length > 0) { + const {overrides} = options; + delete options.overrides; - const filename = path.relative(opts.cwd, opts.filename); + const filename = path.relative(options.cwd, options.filename); const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides); - opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable); + options = optionsManager.mergeApplicableOverrides(options, foundOverrides.applicable); } - opts = optionsManager.buildConfig(opts); + options = optionsManager.buildConfig(options); const defaultIgnores = optionsManager.getIgnores({}).ignores; - if (opts.ignores && !isEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') { + if (options.ignores && !isEqual(defaultIgnores, options.ignores) && typeof options.filename !== 'string') { throw new Error('The `ignores` option requires the `filename` option to be defined.'); } - if (opts.filename) { - const filename = path.relative(opts.cwd, opts.filename); + if (options.filename) { + const filename = path.relative(options.cwd, options.filename); - if (multimatch(filename, opts.ignores).length > 0 || - globby.gitignore.sync({cwd: opts.cwd, ignore: opts.ignores})(opts.filename)) { + if ( + multimatch(filename, options.ignores).length > 0 || + globby.gitignore.sync({cwd: options.cwd, ignore: options.ignores})(options.filename) + ) { return { errorCount: 0, warningCount: 0, @@ -77,41 +78,45 @@ module.exports.lintText = (str, opts) => { } } - const engine = new eslint.CLIEngine(opts); - const report = engine.executeOnText(str, opts.filename); + const engine = new eslint.CLIEngine(options); + const report = engine.executeOnText(str, options.filename); - return processReport(report, opts); + return processReport(report, options); }; -module.exports.lintFiles = (patterns, opts) => { - opts = optionsManager.preprocess(opts); +module.exports.lintFiles = (patterns, options) => { + options = optionsManager.preprocess(options); const isEmptyPatterns = patterns.length === 0; - const defaultPattern = `**/*.{${opts.extensions.join(',')}}`; + const defaultPattern = `**/*.{${options.extensions.join(',')}}`; return globby( isEmptyPatterns ? [defaultPattern] : arrify(patterns), - {ignore: opts.ignores, gitignore: true, cwd: opts.cwd} + { + ignore: options.ignores, + gitignore: true, + cwd: options.cwd + } ).then(paths => { // Filter out unwanted file extensions // For silly users that don't specify an extension in the glob pattern if (!isEmptyPatterns) { paths = paths.filter(filePath => { const ext = path.extname(filePath).replace('.', ''); - return opts.extensions.includes(ext); + return options.extensions.includes(ext); }); } - if (!(opts.overrides && opts.overrides.length > 0)) { - return runEslint(paths, opts); + if (!(options.overrides && options.overrides.length > 0)) { + return runEslint(paths, options); } - const {overrides} = opts; - delete opts.overrides; + const {overrides} = options; + delete options.overrides; - const grouped = optionsManager.groupConfigs(paths, opts, overrides); + const grouped = optionsManager.groupConfigs(paths, options, overrides); - return mergeReports(grouped.map(data => runEslint(data.paths, data.opts))); + return mergeReports(grouped.map(data => runEslint(data.paths, data.options))); }); }; diff --git a/lib/options-manager.js b/lib/options-manager.js index dc3e5c41..971574a8 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -42,10 +42,11 @@ const DEFAULT_CONFIG = { }; /** - * Define the rules that are enabled only for specific version of Node, based on `engines.node` in package.json or the `node-version` option. + * Define the rules that are enabled only for specific version of Node.js based on `engines.node` in package.json or the `node-version` option. * * The keys are rule names and the values are an Object with a valid semver (`4.0.0` is valid `4` is not) as keys and the rule configuration as values. - * Each entry define the rule configuration and the minimum Node version for which to set it. + * + * Each entry define the rule config and the minimum Node.js version for which to set it. * The entry with the highest version that is compliant with the `engines.node`/`node-version` range will be used. * * @type {Object} @@ -97,14 +98,14 @@ const ENGINE_RULES = { }; // Keep the same behaviour in mergeWith as deepAssign -const mergeFn = (prev, val) => { - if (Array.isArray(prev) && Array.isArray(val)) { - return val; +const mergeFn = (previousValue, value) => { + if (Array.isArray(previousValue) && Array.isArray(value)) { + return value; } }; -const normalizeOpts = opts => { - opts = Object.assign({}, opts); +const normalizeOptions = options => { + options = Object.assign({}, options); // Aliases for humans const aliases = [ @@ -120,9 +121,9 @@ const normalizeOpts = opts => { for (const singular of aliases) { const plural = singular + 's'; - let value = opts[plural] || opts[singular]; + let value = options[plural] || options[singular]; - delete opts[singular]; + delete options[singular]; if (value === undefined) { continue; @@ -132,35 +133,35 @@ const normalizeOpts = opts => { value = arrify(value); } - opts[plural] = value; + options[plural] = value; } - return opts; + return options; }; -const mergeWithPkgConf = opts => { - opts = Object.assign({cwd: process.cwd()}, opts); - opts.cwd = path.resolve(opts.cwd); - const conf = pkgConf.sync('xo', {cwd: opts.cwd, skipOnFalse: true}); - const engines = pkgConf.sync('engines', {cwd: opts.cwd}); - return Object.assign({}, conf, {nodeVersion: engines && engines.node && semver.validRange(engines.node)}, opts); +const mergeWithPkgConf = options => { + options = Object.assign({cwd: process.cwd()}, options); + options.cwd = path.resolve(options.cwd); + const conf = pkgConf.sync('xo', {cwd: options.cwd, skipOnFalse: true}); + const engines = pkgConf.sync('engines', {cwd: options.cwd}); + return Object.assign({}, conf, {nodeVersion: engines && engines.node && semver.validRange(engines.node)}, options); }; -const normalizeSpaces = opts => typeof opts.space === 'number' ? opts.space : 2; +const normalizeSpaces = options => typeof options.space === 'number' ? options.space : 2; -const mergeWithPrettierConf = (opts, prettierOpts) => { - if ((opts.semicolon === true && prettierOpts.semi === false) || - (opts.semicolon === false && prettierOpts.semi === true)) { - throw new Error(`The Prettier config \`semi\` is ${prettierOpts.semi} while XO \`semicolon\` is ${opts.semicolon}`); +const mergeWithPrettierConf = (options, prettierOptions) => { + if ((options.semicolon === true && prettierOptions.semi === false) || + (options.semicolon === false && prettierOptions.semi === true)) { + throw new Error(`The Prettier config \`semi\` is ${prettierOptions.semi} while XO \`semicolon\` is ${options.semicolon}`); } - if (((opts.space === true || typeof opts.space === 'number') && prettierOpts.useTabs === true) || - ((opts.space === false) && prettierOpts.useTabs === false)) { - throw new Error(`The Prettier config \`useTabs\` is ${prettierOpts.useTabs} while XO \`space\` is ${opts.space}`); + if (((options.space === true || typeof options.space === 'number') && prettierOptions.useTabs === true) || + ((options.space === false) && prettierOptions.useTabs === false)) { + throw new Error(`The Prettier config \`useTabs\` is ${prettierOptions.useTabs} while XO \`space\` is ${options.space}`); } - if (typeof opts.space === 'number' && typeof prettierOpts.tabWidth === 'number' && opts.space !== prettierOpts.tabWidth) { - throw new Error(`The Prettier config \`tabWidth\` is ${prettierOpts.tabWidth} while XO \`space\` is ${opts.space}`); + if (typeof options.space === 'number' && typeof prettierOptions.tabWidth === 'number' && options.space !== prettierOptions.tabWidth) { + throw new Error(`The Prettier config \`tabWidth\` is ${prettierOptions.tabWidth} while XO \`space\` is ${options.space}`); } return mergeWith( @@ -170,11 +171,11 @@ const mergeWithPrettierConf = (opts, prettierOpts) => { bracketSpacing: false, jsxBracketSameLine: false, trailingComma: 'none', - tabWidth: normalizeSpaces(opts), - useTabs: !opts.space, - semi: opts.semicolon !== false + tabWidth: normalizeSpaces(options), + useTabs: !options.space, + semi: options.semicolon !== false }, - prettierOpts, + prettierOptions, mergeFn ); }; @@ -189,38 +190,38 @@ const emptyOptions = () => ({ extends: [] }); -const buildConfig = opts => { +const buildConfig = options => { const config = mergeWith( emptyOptions(), DEFAULT_CONFIG, - opts, + options, mergeFn ); - const spaces = normalizeSpaces(opts); + const spaces = normalizeSpaces(options); - if (opts.nodeVersion) { + if (options.nodeVersion) { for (const rule of Object.keys(ENGINE_RULES)) { // Use the rule value for the highest version that is lower or equal to the oldest version of Node.js supported for (const minVersion of Object.keys(ENGINE_RULES[rule]).sort(semver.compare)) { - if (!semver.intersects(opts.nodeVersion, `<${minVersion}`)) { + if (!semver.intersects(options.nodeVersion, `<${minVersion}`)) { config.rules[rule] = ENGINE_RULES[rule][minVersion]; } } } } - if (opts.space && !opts.prettier) { + if (options.space && !options.prettier) { config.rules.indent = ['error', spaces, {SwitchCase: 1}]; // Only apply if the user has the React plugin - if (opts.cwd && resolveFrom.silent(opts.cwd, 'eslint-plugin-react')) { + if (options.cwd && resolveFrom.silent(options.cwd, 'eslint-plugin-react')) { config.plugins = config.plugins.concat('react'); config.rules['react/jsx-indent-props'] = ['error', spaces]; config.rules['react/jsx-indent'] = ['error', spaces]; } } - if (opts.semicolon === false && !opts.prettier) { + if (options.semicolon === false && !options.prettier) { config.rules.semi = ['error', 'never']; config.rules['semi-spacing'] = ['error', { before: false, @@ -228,29 +229,29 @@ const buildConfig = opts => { }]; } - if (opts.esnext !== false) { + if (options.esnext !== false) { config.baseConfig.extends = [ 'xo/esnext', path.join(__dirname, '../config/plugins.js') ]; } - if (opts.rules) { - Object.assign(config.rules, opts.rules); + if (options.rules) { + Object.assign(config.rules, options.rules); } - if (opts.settings) { - config.baseConfig.settings = opts.settings; + if (options.settings) { + config.baseConfig.settings = options.settings; } - if (opts.parser) { - config.baseConfig.parser = opts.parser; + if (options.parser) { + config.baseConfig.parser = options.parser; } - if (opts.extends && opts.extends.length > 0) { + if (options.extends && options.extends.length > 0) { // TODO: This logic needs to be improved, preferably use the same code as ESLint // user's configs must be resolved to their absolute paths - const configs = opts.extends.map(name => { + const configs = options.extends.map(name => { // Don't do anything if it's a filepath if (pathExists.sync(name)) { return name; @@ -265,7 +266,7 @@ const buildConfig = opts => { name = `eslint-config-${name}`; } - const ret = resolveFrom(opts.cwd, name); + const ret = resolveFrom(options.cwd, name); if (!ret) { throw new Error(`Couldn't find ESLint config: ${name}`); @@ -278,7 +279,7 @@ const buildConfig = opts => { } // If the user sets the `prettier` options then add the `prettier` plugin and config - if (opts.prettier) { + if (options.prettier) { // Disable formatting rules conflicting with Prettier config.rules['unicorn/number-literal-case'] = 'off'; // The prettier plugin uses Prettier to format the code with `--fix` @@ -287,12 +288,12 @@ const buildConfig = opts => { config.baseConfig.extends = config.baseConfig.extends.concat('prettier'); // The `prettier/prettier` rule reports errors if the code is not formatted in accordance to Prettier config.rules['prettier/prettier'] = [ - 'error', mergeWithPrettierConf(opts, prettier.resolveConfig.sync(opts.cwd || process.cwd()) || {}) + 'error', mergeWithPrettierConf(options, prettier.resolveConfig.sync(options.cwd || process.cwd()) || {}) ]; - // If the user has the React, Flowtype or Standard plugin, add the corresponding Prettier rule overrides + // If the user has the React, Flowtype, or Standard plugin, add the corresponding Prettier rule overrides // See https://github.com/prettier/eslint-config-prettier for the list of plugins overrrides for (const override of ['react', 'flowtype', 'standard']) { - if (opts.cwd && resolveFrom.silent(opts.cwd, `eslint-plugin-${override}`)) { + if (options.cwd && resolveFrom.silent(options.cwd, `eslint-plugin-${override}`)) { config.baseConfig.extends = config.baseConfig.extends.concat(`prettier/${override}`); } } @@ -304,7 +305,7 @@ const buildConfig = opts => { // Builds a list of overrides for a particular path, and a hash value. // The hash value is a binary representation of which elements in the `overrides` array apply to the path. // -// If overrides.length === 4, and only the first and third elements apply, then our hash is: 1010 (in binary) +// If `overrides.length === 4`, and only the first and third elements apply, then our hash is: 1010 (in binary) const findApplicableOverrides = (path, overrides) => { let hash = 0; const applicable = []; @@ -325,7 +326,7 @@ const findApplicableOverrides = (path, overrides) => { }; const mergeApplicableOverrides = (baseOptions, applicableOverrides) => { - applicableOverrides = applicableOverrides.map(override => normalizeOpts(override)); + applicableOverrides = applicableOverrides.map(override => normalizeOptions(override)); const overrides = [emptyOptions(), baseOptions].concat(applicableOverrides, mergeFn); return mergeWith(...overrides); }; @@ -333,17 +334,17 @@ const mergeApplicableOverrides = (baseOptions, applicableOverrides) => { // Creates grouped sets of merged options together with the paths they apply to. const groupConfigs = (paths, baseOptions, overrides) => { const map = {}; - const arr = []; + const array = []; for (const x of paths) { const data = findApplicableOverrides(x, overrides); if (!map[data.hash]) { - const mergedOpts = mergeApplicableOverrides(baseOptions, data.applicable); - delete mergedOpts.files; + const mergedOptions = mergeApplicableOverrides(baseOptions, data.applicable); + delete mergedOptions.files; - arr.push(map[data.hash] = { - opts: mergedOpts, + array.push(map[data.hash] = { + options: mergedOptions, paths: [] }); } @@ -351,28 +352,27 @@ const groupConfigs = (paths, baseOptions, overrides) => { map[data.hash].paths.push(x); } - return arr; + return array; }; -const getIgnores = opts => { - opts.ignores = DEFAULT_IGNORE.concat(opts.ignores || []); - return opts; +const getIgnores = options => { + options.ignores = DEFAULT_IGNORE.concat(options.ignores || []); + return options; }; -const preprocess = opts => { - opts = mergeWithPkgConf(opts); - opts = normalizeOpts(opts); - opts = getIgnores(opts); - opts.extensions = DEFAULT_EXTENSION.concat(opts.extensions || []); - - return opts; +const preprocess = options => { + options = mergeWithPkgConf(options); + options = normalizeOptions(options); + options = getIgnores(options); + options.extensions = DEFAULT_EXTENSION.concat(options.extensions || []); + return options; }; module.exports.DEFAULT_IGNORE = DEFAULT_IGNORE; module.exports.DEFAULT_CONFIG = DEFAULT_CONFIG; module.exports.mergeWithPkgConf = mergeWithPkgConf; module.exports.mergeWithPrettierConf = mergeWithPrettierConf; -module.exports.normalizeOpts = normalizeOpts; +module.exports.normalizeOptions = normalizeOptions; module.exports.buildConfig = buildConfig; module.exports.findApplicableOverrides = findApplicableOverrides; module.exports.mergeApplicableOverrides = mergeApplicableOverrides; diff --git a/package.json b/package.json index c7354d6f..c934eca5 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "*.js" ], "keywords": [ - "❤️", "cli-app", "cli", "xo", diff --git a/test/cli-main.js b/test/cli-main.js index c12e1b3b..d9b185d4 100644 --- a/test/cli-main.js +++ b/test/cli-main.js @@ -7,7 +7,7 @@ import tempWrite from 'temp-write'; process.chdir(__dirname); -const main = (args, opts) => execa(path.join(__dirname, '../cli-main.js'), args, opts); +const main = (args, options) => execa(path.join(__dirname, '../cli-main.js'), args, options); test('fix option', async t => { const filepath = await tempWrite('console.log()\n', 'x.js'); diff --git a/test/cli.js b/test/cli.js index 9b8eff80..7d0503ab 100644 --- a/test/cli.js +++ b/test/cli.js @@ -4,7 +4,7 @@ import execa from 'execa'; const cwd = path.dirname(__dirname); const packageJson = require(path.join(cwd, 'package.json')); -const cli = (args, opts) => execa(path.join(cwd, 'cli.js'), args, opts); +const cli = (args, options) => execa(path.join(cwd, 'cli.js'), args, options); test('runs no-local install of XO', async t => { const {stdout} = await cli(['--no-local', '--version'], {cwd}); diff --git a/test/options-manager.js b/test/options-manager.js index 1aeb1407..6c040382 100644 --- a/test/options-manager.js +++ b/test/options-manager.js @@ -12,8 +12,8 @@ const manager = proxyquire('../lib/options-manager', { 'resolve-from': (cwd, path) => `cwd/${path}` }); -test('normalizeOpts: makes all the opts plural and arrays', t => { - const opts = manager.normalizeOpts({ +test('normalizeOptions: makes all the options plural and arrays', t => { + const options = manager.normalizeOptions({ env: 'node', global: 'foo', ignore: 'test.js', @@ -24,7 +24,7 @@ test('normalizeOpts: makes all the opts plural and arrays', t => { extension: 'html' }); - t.deepEqual(opts, { + t.deepEqual(options, { envs: ['node'], globals: ['foo'], ignores: ['test.js'], @@ -36,8 +36,8 @@ test('normalizeOpts: makes all the opts plural and arrays', t => { }); }); -test('normalizeOpts: falsie values stay falsie', t => { - t.deepEqual(manager.normalizeOpts({}), {}); +test('normalizeOptions: falsie values stay falsie', t => { + t.deepEqual(manager.normalizeOptions({}), {}); }); test('buildConfig: defaults', t => { @@ -235,28 +235,59 @@ test('buildConfig: nodeVersion: >=8', t => { }); test('mergeWithPrettierConf: use `singleQuote`, `trailingComma`, `bracketSpacing` and `jsxBracketSameLine` from `prettier` config if defined', t => { - const prettierOpts = {singleQuote: false, trailingComma: 'all', bracketSpacing: false, jsxBracketSameLine: false}; - const result = manager.mergeWithPrettierConf({}, prettierOpts); - const expected = Object.assign({}, prettierOpts, {tabWidth: 2, useTabs: true, semi: true}); + const prettierOptions = { + singleQuote: false, + trailingComma: 'all', + bracketSpacing: false, + jsxBracketSameLine: false + }; + const result = manager.mergeWithPrettierConf({}, prettierOptions); + const expected = Object.assign( + {}, + prettierOptions, + { + tabWidth: 2, + useTabs: true, + semi: true + } + ); t.deepEqual(result, expected); }); test('mergeWithPrettierConf: determine `tabWidth`, `useTabs`, `semi` from xo config', t => { - const prettierOpts = {tabWidth: 4, useTabs: false, semi: false}; + const prettierOptions = { + tabWidth: 4, + useTabs: false, + semi: false + }; const result = manager.mergeWithPrettierConf({space: 4, semicolon: false}, {}); const expected = Object.assign( - {bracketSpacing: false, jsxBracketSameLine: false, singleQuote: true, trailingComma: 'none'}, - prettierOpts + { + bracketSpacing: false, + jsxBracketSameLine: false, + singleQuote: true, + trailingComma: 'none' + }, + prettierOptions ); t.deepEqual(result, expected); }); test('mergeWithPrettierConf: determine `tabWidth`, `useTabs`, `semi` from prettier config', t => { - const prettierOpts = {useTabs: false, semi: false, tabWidth: 4}; - const result = manager.mergeWithPrettierConf({}, prettierOpts); + const prettierOptions = { + useTabs: false, + semi: false, + tabWidth: 4 + }; + const result = manager.mergeWithPrettierConf({}, prettierOptions); const expected = Object.assign( - {bracketSpacing: false, jsxBracketSameLine: false, singleQuote: true, trailingComma: 'none'}, - prettierOpts + { + bracketSpacing: false, + jsxBracketSameLine: false, + singleQuote: true, + trailingComma: 'none' + }, + prettierOptions ); t.deepEqual(result, expected); }); @@ -352,7 +383,7 @@ test('groupConfigs', t => { '/user/bar/hello.js' ]; - const opts = { + const options = { esnext: false }; @@ -368,17 +399,17 @@ test('groupConfigs', t => { } ]; - const result = manager.groupConfigs(paths, opts, overrides); + const result = manager.groupConfigs(paths, options, overrides); t.deepEqual(result, [ { - opts: { + options: { esnext: true }, paths: ['/user/foo/hello.js', '/user/foo/goodbye.js'] }, { - opts: { + options: { esnext: true, space: 3, envs: ['mocha'] @@ -386,13 +417,13 @@ test('groupConfigs', t => { paths: ['/user/foo/howdy.js'] }, { - opts: { + options: { esnext: false }, paths: ['/user/bar/hello.js'] } ].map(obj => { - obj.opts = Object.assign(manager.emptyOptions(), obj.opts); + obj.options = Object.assign(manager.emptyOptions(), obj.options); return obj; })); });