From 0a381e6dc11dc75ea0a47420b0a9f93d32035b27 Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Wed, 20 Sep 2017 15:54:54 -0700 Subject: [PATCH 1/8] Allow config overrides --- README.md | 24 ++++++++++++++++++++++++ package.json | 1 + src/runESLint.js | 9 ++++++++- src/utils/getESLintOptions.js | 14 ++++++++++++++ yarn.lock | 33 ++++++++++++++++++++++++++++----- 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/utils/getESLintOptions.js diff --git a/README.md b/README.md index dc27133..4be1548 100644 --- a/README.md +++ b/README.md @@ -44,3 +44,27 @@ module.exports = { ```bash yarn jest ``` + + +## Options + +This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig), so you can provide config via: +* a `jest-runner-eslint` property in your `package.json` +* a `jest-runner-eslint.config.js` JS file +* a `.jest-runner-eslintrc` JSON file + +```js +module.exports = { + "cliOptions": { + // ESLint cli options options here + }, + "writeOnFix": // (true|false) +} +``` + +#### cliOptions +See https://eslint.org/docs/developer-guide/nodejs-api#cliengine for a full +set of supported `cliOptions` + +#### writeOnFix +Used for writing the updated file to disk when using `{ fix: true }` in the `cliOptions` \ No newline at end of file diff --git a/package.json b/package.json index 70d93a6..cf14c53 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "format": "prettier --single-quote --trailing-comma all --write \"!(build)/**/*.js\"" }, "dependencies": { + "cosmiconfig": "^3.0.1", "eslint": "4.5.0", "find-up": "^2.1.0", "pify": "3.0.0", diff --git a/src/runESLint.js b/src/runESLint.js index 86635d5..c9ac7e3 100644 --- a/src/runESLint.js +++ b/src/runESLint.js @@ -1,4 +1,5 @@ const getLocalESLint = require('./utils/getLocalESLint'); +const getESLintOptions = require('./utils/getESLintOptions'); const toTestResult = require('./utils/toTestResult'); const skip = ({ start, end, testPath }) => @@ -62,12 +63,18 @@ const runESLint = ({ testPath, config }, workerCallback) => { try { const start = +new Date(); const { CLIEngine } = getLocalESLint(config); - const cli = new CLIEngine({}); + const options = getESLintOptions(config); + const cli = new CLIEngine(options.cliOptions); if (cli.isPathIgnored(testPath)) { const end = +new Date(); workerCallback(null, skip({ start, end, testPath })); } else { const report = cli.executeOnFiles([testPath]); + + if (options.writeOnFix) { + CLIEngine.outputFixes(report); + } + const end = +new Date(); if (report.errorCount > 0) { diff --git a/src/utils/getESLintOptions.js b/src/utils/getESLintOptions.js new file mode 100644 index 0000000..033c5bd --- /dev/null +++ b/src/utils/getESLintOptions.js @@ -0,0 +1,14 @@ +const cosmiconfig = require('cosmiconfig'); + +const explorer = cosmiconfig('jest-runner-eslint', { sync: true }); + +const getESLintOptions = config => { + const result = explorer.load(config.rootDir); + if (result) { + return result.config; + } + + return {}; +}; + +module.exports = getESLintOptions; diff --git a/yarn.lock b/yarn.lock index 47e9455..dd910b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -644,7 +644,7 @@ babel-preset-jest@^21.0.0: dependencies: babel-plugin-jest-hoist "^21.0.0" -babel-register@6.26.0, babel-register@^6.26.0: +babel-register@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.26.0.tgz#6ed021173e2fcb486d7acb45c6009a856f647071" dependencies: @@ -932,6 +932,15 @@ core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" +cosmiconfig@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-3.0.1.tgz#d290e2b657a7f3a335257a0d306587836650fdc0" + dependencies: + is-directory "^0.3.1" + js-yaml "^3.9.0" + parse-json "^3.0.0" + require-from-string "^2.0.1" + cross-spawn@^5.0.1, cross-spawn@^5.1.0: version "5.1.0" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" @@ -1046,7 +1055,7 @@ errno@^0.1.4: dependencies: prr "~0.0.0" -error-ex@^1.2.0: +error-ex@^1.2.0, error-ex@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc" dependencies: @@ -1658,6 +1667,10 @@ is-ci@^1.0.10: dependencies: ci-info "^1.0.0" +is-directory@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1" + is-dotfile@^1.0.0: version "1.0.3" resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.3.tgz#a6a2f32ffd2dfb04f5ca25ecd0f6b83cf798a1e1" @@ -2070,7 +2083,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" -js-yaml@^3.7.0, js-yaml@^3.9.1: +js-yaml@^3.7.0, js-yaml@^3.9.0, js-yaml@^3.9.1: version "3.9.1" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0" dependencies: @@ -2215,7 +2228,7 @@ lodash.cond@^4.3.0: version "4.5.2" resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5" -lodash@4.17.4, lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0: +lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" @@ -2278,7 +2291,7 @@ mimic-fn@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.1.0.tgz#e667783d92e89dbd342818b5230b9d62a672ad18" -minimatch@3.0.4, minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4: +minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" dependencies: @@ -2493,6 +2506,12 @@ parse-json@^2.2.0: dependencies: error-ex "^1.2.0" +parse-json@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-3.0.0.tgz#fa6f47b18e23826ead32f263e744d0e1e847fb13" + dependencies: + error-ex "^1.3.1" + parse5@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/parse5/-/parse5-1.5.1.tgz#9b7f3b0de32be78dc2401b17573ccaf0f6f59d94" @@ -2777,6 +2796,10 @@ require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" +require-from-string@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.1.tgz#c545233e9d7da6616e9d59adfb39fc9f588676ff" + require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" From 7d133b4c2037b86ae9de52412b57d96d06d7f78e Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Wed, 20 Sep 2017 17:42:56 -0700 Subject: [PATCH 2/8] remove writeOnFix and rely on .fix, also updates README.md --- README.md | 51 +++++++++++++++++++++++++++++++++++++++--------- src/runESLint.js | 2 +- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4be1548..f021258 100644 --- a/README.md +++ b/README.md @@ -53,18 +53,51 @@ This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig), s * a `jest-runner-eslint.config.js` JS file * a `.jest-runner-eslintrc` JSON file -```js -module.exports = { - "cliOptions": { - // ESLint cli options options here - }, - "writeOnFix": // (true|false) + +In `package.json` +```json +{ + "jest-runner-eslint": { + "cliOptions": {} + } } ``` -#### cliOptions See https://eslint.org/docs/developer-guide/nodejs-api#cliengine for a full set of supported `cliOptions` -#### writeOnFix -Used for writing the updated file to disk when using `{ fix: true }` in the `cliOptions` \ No newline at end of file +#### `fix` +This is similar to running ESLint with `--fix` +```json +{ + "jest-runner-eslint": { + "cliOptions": { + "fix": true + } + } +} +``` + +#### `configFile` +This is similar to running ESLint with `-c /path/to/config-file` +```json +{ + "jest-runner-eslint": { + "cliOptions": { + "configFile": "/path/to/config-file" + } + } +} +``` + +#### `globals` +This is similar to running ESLint with `--globals` +```json +{ + "jest-runner-eslint": { + "cliOptions": { + "globals": ["describe", "it"] + } + } +} +``` diff --git a/src/runESLint.js b/src/runESLint.js index c9ac7e3..8a589dc 100644 --- a/src/runESLint.js +++ b/src/runESLint.js @@ -71,7 +71,7 @@ const runESLint = ({ testPath, config }, workerCallback) => { } else { const report = cli.executeOnFiles([testPath]); - if (options.writeOnFix) { + if (options.cliOptions && options.cliOptions.fix) { CLIEngine.outputFixes(report); } From 1bcb55f49558217b7a0045e73c5d0b5c5ec07fe2 Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Mon, 25 Sep 2017 13:54:58 -0500 Subject: [PATCH 3/8] Fist pass to use real CLI options --- src/utils/__tests__/normalizeOptions.test.js | 159 +++++++++++++++++++ src/utils/getESLintOptions.js | 3 +- src/utils/normalizeOptions.js | 65 ++++++++ 3 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/utils/__tests__/normalizeOptions.test.js create mode 100644 src/utils/normalizeOptions.js diff --git a/src/utils/__tests__/normalizeOptions.test.js b/src/utils/__tests__/normalizeOptions.test.js new file mode 100644 index 0000000..b33da84 --- /dev/null +++ b/src/utils/__tests__/normalizeOptions.test.js @@ -0,0 +1,159 @@ +const normalizeOptions = require('../normalizeOptions'); + +it('normalizes noInlineConfig', () => { + expect(normalizeOptions({})).toMatchObject({ + allowInlineConfig: true, + }); + + expect(normalizeOptions({ noInlineConfig: true })).toMatchObject({ + allowInlineConfig: false, + }); + + expect(normalizeOptions({ noInlineConfig: false })).toMatchObject({ + allowInlineConfig: true, + }); +}); + +it('normalizes cacheLocation', () => { + expect(normalizeOptions({})).toMatchObject({ + cacheLocation: '.eslintcache', + }); + + expect(normalizeOptions({ cacheLocation: '/path/to/cache' })).toMatchObject({ + cacheLocation: '/path/to/cache', + }); +}); + +it('normalizes config', () => { + expect(normalizeOptions({})).toMatchObject({ + configFile: null, + }); + + expect(normalizeOptions({ config: '/path/to/config' })).toMatchObject({ + configFile: '/path/to/config', + }); +}); + +it('normalizes envs', () => { + expect(normalizeOptions({})).toMatchObject({ + envs: [], + }); + + expect(normalizeOptions({ envs: ['mocha', 'browser'] })).toMatchObject({ + envs: ['mocha', 'browser'], + }); +}); + +it('normalizes exts', () => { + expect(normalizeOptions({})).toMatchObject({ + extensions: ['.js'], + }); + + expect(normalizeOptions({ exts: ['.js', '.jsx', '.ts'] })).toMatchObject({ + extensions: ['.js', '.jsx', '.ts'], + }); +}); + +it('normalizes fix', () => { + expect(normalizeOptions({})).toMatchObject({ + fix: false, + }); + + expect(normalizeOptions({ fix: true })).toMatchObject({ + fix: true, + }); +}); + +it('normalizes globals', () => { + expect(normalizeOptions({})).toMatchObject({ + globals: [], + }); + + expect(normalizeOptions({ globals: ['it', 'describe'] })).toMatchObject({ + globals: ['it', 'describe'], + }); +}); + +it('normalizes noIgnore', () => { + expect(normalizeOptions({})).toMatchObject({ + ignore: true, + }); + + expect(normalizeOptions({ noIgnore: true })).toMatchObject({ + ignore: false, + }); +}); + +it('normalizes ignorePath', () => { + expect(normalizeOptions({})).toMatchObject({ + ignorePath: null, + }); + + expect(normalizeOptions({ ignorePath: '/path/to/ignore' })).toMatchObject({ + ignorePath: '/path/to/ignore', + }); +}); + +it('normalizes parser', () => { + expect(normalizeOptions({})).toMatchObject({ + parser: 'espree', + }); + + expect(normalizeOptions({ parser: 'flow' })).toMatchObject({ + parser: 'flow', + }); +}); + +it('normalizes parserOptions', () => { + expect(normalizeOptions({})).toMatchObject({ + parserOptions: {}, + }); + + expect( + normalizeOptions({ parserOptions: { ecmaVersion: 2015 } }), + ).toMatchObject({ + parserOptions: { ecmaVersion: 2015 }, + }); +}); + +it('normalizes plugins', () => { + expect(normalizeOptions({})).toMatchObject({ + plugins: [], + }); + + expect(normalizeOptions({ plugins: ['prettier'] })).toMatchObject({ + plugins: ['prettier'], + }); +}); + +it('normalizes rulesdir', () => { + expect(normalizeOptions({})).toMatchObject({ + rulePaths: [], + }); + + expect( + normalizeOptions({ rulesdir: ['/path/to/rules', '/other/path'] }), + ).toMatchObject({ + rulePaths: ['/path/to/rules', '/other/path'], + }); +}); + +it('normalizes rules', () => { + expect(normalizeOptions({})).toMatchObject({ + rules: null, + }); + + expect(normalizeOptions({ rules: [] })).toMatchObject({ + rules: [], + }); +}); + +it('normalizes noEslintrc', () => { + expect(normalizeOptions({})).toMatchObject({ + useEslintrc: true, + }); + + expect(normalizeOptions({ noEslintrc: true })).toMatchObject({ + useEslintrc: false, + }); +}); diff --git a/src/utils/getESLintOptions.js b/src/utils/getESLintOptions.js index 033c5bd..6049a11 100644 --- a/src/utils/getESLintOptions.js +++ b/src/utils/getESLintOptions.js @@ -1,3 +1,4 @@ +const normalizeOptions = require('./normalizeOptions'); const cosmiconfig = require('cosmiconfig'); const explorer = cosmiconfig('jest-runner-eslint', { sync: true }); @@ -5,7 +6,7 @@ const explorer = cosmiconfig('jest-runner-eslint', { sync: true }); const getESLintOptions = config => { const result = explorer.load(config.rootDir); if (result) { - return result.config; + return normalizeOptions(result.config); } return {}; diff --git a/src/utils/normalizeOptions.js b/src/utils/normalizeOptions.js new file mode 100644 index 0000000..81d4c59 --- /dev/null +++ b/src/utils/normalizeOptions.js @@ -0,0 +1,65 @@ +const mapKeys = require('lodash/fp/mapKeys'); +const flow = require('lodash/fp/flow'); +const omit = require('lodash/fp/omit'); +/** + * This base config maps to the default values of the CLI options that have a corresponding `--cli-option` + * https://eslint.org/docs/developer-guide/nodejs-api#cliengine + */ + +const baseConfig = { + allowInlineConfig: true, + cache: false, + cacheLocation: '.eslintcache', + configFile: null, + envs: [], + extensions: ['.js'], + fix: false, + globals: [], + ignore: true, + ignorePath: null, + parser: 'espree', + parserOptions: {}, + plugins: [], + rulePaths: [], + rules: null, + useEslintrc: true, +}; + +const oneToOneKeyMap = { + cacheLocation: 'cacheLocation', + config: 'configFile', + envs: 'envs', + exts: 'extensions', + fix: 'fix', + globals: 'globals', + ignorePath: 'ignorePath', + parser: 'parser', + parserOptions: 'parserOptions', + plugins: 'plugins', + rulesdir: 'rulePaths', + rules: 'rules', +}; + +const mapOneToOneKeys = flow( + mapKeys(key => oneToOneKeyMap[key]), + omit(undefined), +); + +const normalizeOptions = rawConfig => { + const config = Object.assign({}, baseConfig, mapOneToOneKeys(rawConfig)); + + if (rawConfig.noInlineConfig) { + config.allowInlineConfig = !rawConfig.noInlineConfig; + } + + if (rawConfig.noIgnore) { + config.ignore = !rawConfig.noIgnore; + } + + if (rawConfig.noEslintrc) { + config.useEslintrc = !rawConfig.noEslintrc; + } + + return config; +}; +module.exports = normalizeOptions; From 69cc6a989d4283eae8598c8d6de192bbbd3dffef Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Mon, 25 Sep 2017 14:36:48 -0500 Subject: [PATCH 4/8] Use CLI (--) as interface --- src/utils/__tests__/normalizeOptions.test.js | 52 +++++++++++++++----- src/utils/normalizeOptions.js | 42 +++++++++++++--- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/utils/__tests__/normalizeOptions.test.js b/src/utils/__tests__/normalizeOptions.test.js index b33da84..42fb569 100644 --- a/src/utils/__tests__/normalizeOptions.test.js +++ b/src/utils/__tests__/normalizeOptions.test.js @@ -1,5 +1,11 @@ const normalizeOptions = require('../normalizeOptions'); +it('ignores unkown options', () => { + expect(normalizeOptions({ other: true })).not.toMatchObject({ + other: true, + }); +}); + it('normalizes noInlineConfig', () => { expect(normalizeOptions({})).toMatchObject({ allowInlineConfig: true, @@ -34,22 +40,30 @@ it('normalizes config', () => { }); }); -it('normalizes envs', () => { +it('normalizes env', () => { expect(normalizeOptions({})).toMatchObject({ envs: [], }); - expect(normalizeOptions({ envs: ['mocha', 'browser'] })).toMatchObject({ + expect(normalizeOptions({ env: 'mocha' })).toMatchObject({ + envs: ['mocha'], + }); + + expect(normalizeOptions({ env: ['mocha', 'browser'] })).toMatchObject({ envs: ['mocha', 'browser'], }); }); -it('normalizes exts', () => { +it('normalizes ext', () => { expect(normalizeOptions({})).toMatchObject({ extensions: ['.js'], }); - expect(normalizeOptions({ exts: ['.js', '.jsx', '.ts'] })).toMatchObject({ + expect(normalizeOptions({ ext: '.ts' })).toMatchObject({ + extensions: ['.ts'], + }); + + expect(normalizeOptions({ ext: ['.js', '.jsx', '.ts'] })).toMatchObject({ extensions: ['.js', '.jsx', '.ts'], }); }); @@ -64,12 +78,16 @@ it('normalizes fix', () => { }); }); -it('normalizes globals', () => { +it('normalizes global', () => { expect(normalizeOptions({})).toMatchObject({ globals: [], }); - expect(normalizeOptions({ globals: ['it', 'describe'] })).toMatchObject({ + expect(normalizeOptions({ global: 'it' })).toMatchObject({ + globals: ['it'], + }); + + expect(normalizeOptions({ global: ['it', 'describe'] })).toMatchObject({ globals: ['it', 'describe'], }); }); @@ -116,12 +134,16 @@ it('normalizes parserOptions', () => { }); }); -it('normalizes plugins', () => { +it('normalizes plugin', () => { expect(normalizeOptions({})).toMatchObject({ plugins: [], }); - expect(normalizeOptions({ plugins: ['prettier'] })).toMatchObject({ + expect(normalizeOptions({ plugin: 'prettier' })).toMatchObject({ + plugins: ['prettier'], + }); + + expect(normalizeOptions({ plugin: ['prettier'] })).toMatchObject({ plugins: ['prettier'], }); }); @@ -131,6 +153,10 @@ it('normalizes rulesdir', () => { rulePaths: [], }); + expect(normalizeOptions({ rulesdir: '/path/to/rules' })).toMatchObject({ + rulePaths: ['/path/to/rules'], + }); + expect( normalizeOptions({ rulesdir: ['/path/to/rules', '/other/path'] }), ).toMatchObject({ @@ -138,13 +164,17 @@ it('normalizes rulesdir', () => { }); }); -it('normalizes rules', () => { +it('normalizes rule', () => { expect(normalizeOptions({})).toMatchObject({ rules: null, }); - expect(normalizeOptions({ rules: [] })).toMatchObject({ - rules: [], + expect(normalizeOptions({ rule: ['quotes: [2, double]'] })).toMatchObject({ + rules: ['quotes: [2, double]'], + }); + + expect(normalizeOptions({ rule: 'quotes: [2, double]' })).toMatchObject({ + rules: ['quotes: [2, double]'], }); }); diff --git a/src/utils/normalizeOptions.js b/src/utils/normalizeOptions.js index 81d4c59..c3b4233 100644 --- a/src/utils/normalizeOptions.js +++ b/src/utils/normalizeOptions.js @@ -28,16 +28,10 @@ const baseConfig = { const oneToOneKeyMap = { cacheLocation: 'cacheLocation', config: 'configFile', - envs: 'envs', - exts: 'extensions', fix: 'fix', - globals: 'globals', ignorePath: 'ignorePath', parser: 'parser', parserOptions: 'parserOptions', - plugins: 'plugins', - rulesdir: 'rulePaths', - rules: 'rules', }; const mapOneToOneKeys = flow( @@ -60,6 +54,42 @@ const normalizeOptions = rawConfig => { config.useEslintrc = !rawConfig.noEslintrc; } + if (rawConfig.ext) { + config.extensions = + typeof rawConfig.ext === 'string' ? [rawConfig.ext] : rawConfig.ext; + } + + if (rawConfig.env) { + config.envs = + typeof rawConfig.env === 'string' ? [rawConfig.env] : rawConfig.env; + } + + if (rawConfig.global) { + config.globals = + typeof rawConfig.global === 'string' + ? [rawConfig.global] + : rawConfig.global; + } + + if (rawConfig.plugin) { + config.plugins = + typeof rawConfig.plugin === 'string' + ? [rawConfig.plugin] + : rawConfig.plugin; + } + + if (rawConfig.rulesdir) { + config.rulePaths = + typeof rawConfig.rulesdir === 'string' + ? [rawConfig.rulesdir] + : rawConfig.rulesdir; + } + + if (rawConfig.rule) { + config.rules = + typeof rawConfig.rule === 'string' ? [rawConfig.rule] : rawConfig.rule; + } + return config; }; module.exports = normalizeOptions; From 7e3065d75d1c45299b1686b45165f1a39ae725c8 Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Tue, 26 Sep 2017 12:42:14 -0500 Subject: [PATCH 5/8] Add good config util implementation --- src/utils/__tests__/normalizeConfig.test.js | 189 +++++++++++++++++++ src/utils/__tests__/normalizeOptions.test.js | 189 ------------------- src/utils/normalizeConfig.js | 93 +++++++++ src/utils/normalizeOptions.js | 95 ---------- 4 files changed, 282 insertions(+), 284 deletions(-) create mode 100644 src/utils/__tests__/normalizeConfig.test.js delete mode 100644 src/utils/__tests__/normalizeOptions.test.js create mode 100644 src/utils/normalizeConfig.js delete mode 100644 src/utils/normalizeOptions.js diff --git a/src/utils/__tests__/normalizeConfig.test.js b/src/utils/__tests__/normalizeConfig.test.js new file mode 100644 index 0000000..16db319 --- /dev/null +++ b/src/utils/__tests__/normalizeConfig.test.js @@ -0,0 +1,189 @@ +const normalizeConfig = require('../normalizeConfig'); + +it('ignores unkown options', () => { + expect(normalizeConfig({ other: true })).not.toMatchObject({ + other: true, + }); +}); + +it('normalizes noInlineConfig', () => { + expect(normalizeConfig({})).toMatchObject({ + allowInlineConfig: true, + }); + + expect(normalizeConfig({ noInlineConfig: true })).toMatchObject({ + allowInlineConfig: false, + }); + + expect(normalizeConfig({ noInlineConfig: false })).toMatchObject({ + allowInlineConfig: true, + }); +}); + +it('normalizes cacheLocation', () => { + expect(normalizeConfig({})).toMatchObject({ + cacheLocation: '.eslintcache', + }); + + expect(normalizeConfig({ cacheLocation: '/path/to/cache' })).toMatchObject({ + cacheLocation: '/path/to/cache', + }); +}); + +it('normalizes config', () => { + expect(normalizeConfig({})).toMatchObject({ + configFile: null, + }); + + expect(normalizeConfig({ config: '/path/to/config' })).toMatchObject({ + configFile: '/path/to/config', + }); +}); + +it('normalizes env', () => { + expect(normalizeConfig({})).toMatchObject({ + envs: [], + }); + + expect(normalizeConfig({ env: 'mocha' })).toMatchObject({ + envs: ['mocha'], + }); + + expect(normalizeConfig({ env: ['mocha', 'browser'] })).toMatchObject({ + envs: ['mocha', 'browser'], + }); +}); + +it('normalizes ext', () => { + expect(normalizeConfig({})).toMatchObject({ + extensions: ['.js'], + }); + + expect(normalizeConfig({ ext: '.ts' })).toMatchObject({ + extensions: ['.ts'], + }); + + expect(normalizeConfig({ ext: ['.js', '.jsx', '.ts'] })).toMatchObject({ + extensions: ['.js', '.jsx', '.ts'], + }); +}); + +it('normalizes fix', () => { + expect(normalizeConfig({})).toMatchObject({ + fix: false, + }); + + expect(normalizeConfig({ fix: true })).toMatchObject({ + fix: true, + }); +}); + +it('normalizes global', () => { + expect(normalizeConfig({})).toMatchObject({ + globals: [], + }); + + expect(normalizeConfig({ global: 'it' })).toMatchObject({ + globals: ['it'], + }); + + expect(normalizeConfig({ global: ['it', 'describe'] })).toMatchObject({ + globals: ['it', 'describe'], + }); +}); + +it('normalizes noIgnore', () => { + expect(normalizeConfig({})).toMatchObject({ + ignore: true, + }); + + expect(normalizeConfig({ noIgnore: true })).toMatchObject({ + ignore: false, + }); +}); + +it('normalizes ignorePath', () => { + expect(normalizeConfig({})).toMatchObject({ + ignorePath: null, + }); + + expect(normalizeConfig({ ignorePath: '/path/to/ignore' })).toMatchObject({ + ignorePath: '/path/to/ignore', + }); +}); + +it('normalizes parser', () => { + expect(normalizeConfig({})).toMatchObject({ + parser: 'espree', + }); + + expect(normalizeConfig({ parser: 'flow' })).toMatchObject({ + parser: 'flow', + }); +}); + +it('normalizes parserOptions', () => { + expect(normalizeConfig({})).toMatchObject({ + parserOptions: {}, + }); + + expect( + normalizeConfig({ parserOptions: { ecmaVersion: 2015 } }), + ).toMatchObject({ + parserOptions: { ecmaVersion: 2015 }, + }); +}); + +it('normalizes plugin', () => { + expect(normalizeConfig({})).toMatchObject({ + plugins: [], + }); + + expect(normalizeConfig({ plugin: 'prettier' })).toMatchObject({ + plugins: ['prettier'], + }); + + expect(normalizeConfig({ plugin: ['prettier'] })).toMatchObject({ + plugins: ['prettier'], + }); +}); + +it('normalizes rulesdir', () => { + expect(normalizeConfig({})).toMatchObject({ + rulePaths: [], + }); + + expect(normalizeConfig({ rulesdir: '/path/to/rules' })).toMatchObject({ + rulePaths: ['/path/to/rules'], + }); + + expect( + normalizeConfig({ rulesdir: ['/path/to/rules', '/other/path'] }), + ).toMatchObject({ + rulePaths: ['/path/to/rules', '/other/path'], + }); +}); + +it('normalizes rule', () => { + expect(normalizeConfig({})).toMatchObject({ + rules: null, + }); + + expect(normalizeConfig({ rule: ['quotes: [2, double]'] })).toMatchObject({ + rules: ['quotes: [2, double]'], + }); + + expect(normalizeConfig({ rule: 'quotes: [2, double]' })).toMatchObject({ + rules: ['quotes: [2, double]'], + }); +}); + +it('normalizes noEslintrc', () => { + expect(normalizeConfig({})).toMatchObject({ + useEslintrc: true, + }); + + expect(normalizeConfig({ noEslintrc: true })).toMatchObject({ + useEslintrc: false, + }); +}); diff --git a/src/utils/__tests__/normalizeOptions.test.js b/src/utils/__tests__/normalizeOptions.test.js deleted file mode 100644 index 42fb569..0000000 --- a/src/utils/__tests__/normalizeOptions.test.js +++ /dev/null @@ -1,189 +0,0 @@ -const normalizeOptions = require('../normalizeOptions'); - -it('ignores unkown options', () => { - expect(normalizeOptions({ other: true })).not.toMatchObject({ - other: true, - }); -}); - -it('normalizes noInlineConfig', () => { - expect(normalizeOptions({})).toMatchObject({ - allowInlineConfig: true, - }); - - expect(normalizeOptions({ noInlineConfig: true })).toMatchObject({ - allowInlineConfig: false, - }); - - expect(normalizeOptions({ noInlineConfig: false })).toMatchObject({ - allowInlineConfig: true, - }); -}); - -it('normalizes cacheLocation', () => { - expect(normalizeOptions({})).toMatchObject({ - cacheLocation: '.eslintcache', - }); - - expect(normalizeOptions({ cacheLocation: '/path/to/cache' })).toMatchObject({ - cacheLocation: '/path/to/cache', - }); -}); - -it('normalizes config', () => { - expect(normalizeOptions({})).toMatchObject({ - configFile: null, - }); - - expect(normalizeOptions({ config: '/path/to/config' })).toMatchObject({ - configFile: '/path/to/config', - }); -}); - -it('normalizes env', () => { - expect(normalizeOptions({})).toMatchObject({ - envs: [], - }); - - expect(normalizeOptions({ env: 'mocha' })).toMatchObject({ - envs: ['mocha'], - }); - - expect(normalizeOptions({ env: ['mocha', 'browser'] })).toMatchObject({ - envs: ['mocha', 'browser'], - }); -}); - -it('normalizes ext', () => { - expect(normalizeOptions({})).toMatchObject({ - extensions: ['.js'], - }); - - expect(normalizeOptions({ ext: '.ts' })).toMatchObject({ - extensions: ['.ts'], - }); - - expect(normalizeOptions({ ext: ['.js', '.jsx', '.ts'] })).toMatchObject({ - extensions: ['.js', '.jsx', '.ts'], - }); -}); - -it('normalizes fix', () => { - expect(normalizeOptions({})).toMatchObject({ - fix: false, - }); - - expect(normalizeOptions({ fix: true })).toMatchObject({ - fix: true, - }); -}); - -it('normalizes global', () => { - expect(normalizeOptions({})).toMatchObject({ - globals: [], - }); - - expect(normalizeOptions({ global: 'it' })).toMatchObject({ - globals: ['it'], - }); - - expect(normalizeOptions({ global: ['it', 'describe'] })).toMatchObject({ - globals: ['it', 'describe'], - }); -}); - -it('normalizes noIgnore', () => { - expect(normalizeOptions({})).toMatchObject({ - ignore: true, - }); - - expect(normalizeOptions({ noIgnore: true })).toMatchObject({ - ignore: false, - }); -}); - -it('normalizes ignorePath', () => { - expect(normalizeOptions({})).toMatchObject({ - ignorePath: null, - }); - - expect(normalizeOptions({ ignorePath: '/path/to/ignore' })).toMatchObject({ - ignorePath: '/path/to/ignore', - }); -}); - -it('normalizes parser', () => { - expect(normalizeOptions({})).toMatchObject({ - parser: 'espree', - }); - - expect(normalizeOptions({ parser: 'flow' })).toMatchObject({ - parser: 'flow', - }); -}); - -it('normalizes parserOptions', () => { - expect(normalizeOptions({})).toMatchObject({ - parserOptions: {}, - }); - - expect( - normalizeOptions({ parserOptions: { ecmaVersion: 2015 } }), - ).toMatchObject({ - parserOptions: { ecmaVersion: 2015 }, - }); -}); - -it('normalizes plugin', () => { - expect(normalizeOptions({})).toMatchObject({ - plugins: [], - }); - - expect(normalizeOptions({ plugin: 'prettier' })).toMatchObject({ - plugins: ['prettier'], - }); - - expect(normalizeOptions({ plugin: ['prettier'] })).toMatchObject({ - plugins: ['prettier'], - }); -}); - -it('normalizes rulesdir', () => { - expect(normalizeOptions({})).toMatchObject({ - rulePaths: [], - }); - - expect(normalizeOptions({ rulesdir: '/path/to/rules' })).toMatchObject({ - rulePaths: ['/path/to/rules'], - }); - - expect( - normalizeOptions({ rulesdir: ['/path/to/rules', '/other/path'] }), - ).toMatchObject({ - rulePaths: ['/path/to/rules', '/other/path'], - }); -}); - -it('normalizes rule', () => { - expect(normalizeOptions({})).toMatchObject({ - rules: null, - }); - - expect(normalizeOptions({ rule: ['quotes: [2, double]'] })).toMatchObject({ - rules: ['quotes: [2, double]'], - }); - - expect(normalizeOptions({ rule: 'quotes: [2, double]' })).toMatchObject({ - rules: ['quotes: [2, double]'], - }); -}); - -it('normalizes noEslintrc', () => { - expect(normalizeOptions({})).toMatchObject({ - useEslintrc: true, - }); - - expect(normalizeOptions({ noEslintrc: true })).toMatchObject({ - useEslintrc: false, - }); -}); diff --git a/src/utils/normalizeConfig.js b/src/utils/normalizeConfig.js new file mode 100644 index 0000000..88ffb01 --- /dev/null +++ b/src/utils/normalizeConfig.js @@ -0,0 +1,93 @@ +const identity = v => v; +const negate = v => !v; +const asArray = v => (typeof v === 'string' ? [v] : v); + +const BASE_CONFIG = { + cacheLocation: { + default: '.eslintcache', + }, + config: { + name: 'configFile', + default: null, + }, + env: { + name: 'envs', + default: [], + transform: asArray, + }, + ext: { + name: 'extensions', + default: ['.js'], + transform: asArray, + }, + fix: { + default: false, + }, + global: { + name: 'globals', + default: [], + transform: asArray, + }, + ignorePath: { + default: null, + }, + noEslintrc: { + name: 'useEslintrc', + default: true, + transform: negate, + }, + noIgnore: { + name: 'ignore', + default: true, + transform: negate, + }, + noInlineConfig: { + name: 'allowInlineConfig', + default: true, + transform: negate, + }, + parser: { + default: 'espree', + }, + parserOptions: { + default: {}, + }, + plugin: { + name: 'plugins', + default: [], + transform: asArray, + }, + rule: { + name: 'rules', + default: null, + transform: asArray, + }, + rulesdir: { + name: 'rulePaths', + default: [], + transform: asArray, + }, +}; + +/* eslint-disable no-param-reassign */ +const normalizeConfig = rawConfig => + Object.keys(BASE_CONFIG).reduce((config, key) => { + const { + name = key, + transform = identity, + default: defaultValue, + } = BASE_CONFIG[key]; + + if (rawConfig[key]) { + config[name] = transform(rawConfig[key]); + } + + if (config[name] === undefined) { + config[name] = defaultValue; + } + + return config; + }, {}); +/* eslint-enable no-param-reassign */ + +module.exports = normalizeConfig; diff --git a/src/utils/normalizeOptions.js b/src/utils/normalizeOptions.js deleted file mode 100644 index c3b4233..0000000 --- a/src/utils/normalizeOptions.js +++ /dev/null @@ -1,95 +0,0 @@ -const mapKeys = require('lodash/fp/mapKeys'); -const flow = require('lodash/fp/flow'); -const omit = require('lodash/fp/omit'); -/** - * This base config maps to the default values of the CLI options that have a corresponding `--cli-option` - * https://eslint.org/docs/developer-guide/nodejs-api#cliengine - */ - -const baseConfig = { - allowInlineConfig: true, - cache: false, - cacheLocation: '.eslintcache', - configFile: null, - envs: [], - extensions: ['.js'], - fix: false, - globals: [], - ignore: true, - ignorePath: null, - parser: 'espree', - parserOptions: {}, - plugins: [], - rulePaths: [], - rules: null, - useEslintrc: true, -}; - -const oneToOneKeyMap = { - cacheLocation: 'cacheLocation', - config: 'configFile', - fix: 'fix', - ignorePath: 'ignorePath', - parser: 'parser', - parserOptions: 'parserOptions', -}; - -const mapOneToOneKeys = flow( - mapKeys(key => oneToOneKeyMap[key]), - omit(undefined), -); - -const normalizeOptions = rawConfig => { - const config = Object.assign({}, baseConfig, mapOneToOneKeys(rawConfig)); - - if (rawConfig.noInlineConfig) { - config.allowInlineConfig = !rawConfig.noInlineConfig; - } - - if (rawConfig.noIgnore) { - config.ignore = !rawConfig.noIgnore; - } - - if (rawConfig.noEslintrc) { - config.useEslintrc = !rawConfig.noEslintrc; - } - - if (rawConfig.ext) { - config.extensions = - typeof rawConfig.ext === 'string' ? [rawConfig.ext] : rawConfig.ext; - } - - if (rawConfig.env) { - config.envs = - typeof rawConfig.env === 'string' ? [rawConfig.env] : rawConfig.env; - } - - if (rawConfig.global) { - config.globals = - typeof rawConfig.global === 'string' - ? [rawConfig.global] - : rawConfig.global; - } - - if (rawConfig.plugin) { - config.plugins = - typeof rawConfig.plugin === 'string' - ? [rawConfig.plugin] - : rawConfig.plugin; - } - - if (rawConfig.rulesdir) { - config.rulePaths = - typeof rawConfig.rulesdir === 'string' - ? [rawConfig.rulesdir] - : rawConfig.rulesdir; - } - - if (rawConfig.rule) { - config.rules = - typeof rawConfig.rule === 'string' ? [rawConfig.rule] : rawConfig.rule; - } - - return config; -}; -module.exports = normalizeOptions; From d7c563ccf8eedafe0e2efc13075385e484b82e29 Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Tue, 26 Sep 2017 16:21:07 -0500 Subject: [PATCH 6/8] Add config to readme --- README.md | 60 ++++++++------ .../__fixtures__/passing/__eslint__/file.js | 1 - .../passing/jest-runner-eslint.config.js | 5 ++ src/utils/__tests__/normalizeConfig.test.js | 81 ++++++++++--------- src/utils/getESLintOptions.js | 7 +- src/utils/normalizeConfig.js | 8 +- 6 files changed, 93 insertions(+), 69 deletions(-) create mode 100644 integrationTests/__fixtures__/passing/jest-runner-eslint.config.js diff --git a/README.md b/README.md index f021258..78565c9 100644 --- a/README.md +++ b/README.md @@ -63,41 +63,49 @@ In `package.json` } ``` -See https://eslint.org/docs/developer-guide/nodejs-api#cliengine for a full -set of supported `cliOptions` +### cliOptions + +jest-runner-eslint maps a lot of ESLint CLI arguments to config options. For example `--fix` is `cliOptions.fix` + +|cliOption|description|default|example +|-----|-----|-----|-----| +|cacheLocation||`.eslintcache`|`"cacheLocation": "/path/to/cache"` +|config||`null`|`"config": "/path/to/config"` +|env||`null`|`"env": "mocha"` or `"env": ["mocha", "other"]` +|ext||`[".js"]`|`"ext": ".jsx"` or `"ext": [".jsx", ".ts"]` +|fix||`false`|`"fix": true` +|global||`[]`|`"global": "it"` or `"global": ["it", "describe"]` +|ignorePath||`null`|`"ignorePath": "/path/to/ignore"` +|noEslintrc||`false`|`"noEslintrc": true` +|noIgnore||`false`|`"noIgnore": true` +|noInlineConfig||`false`|`"noInlineConfig": true` +|parser||`espree`|`"parser": "flow"` +|parserOptions||`{}`|`"parserOptions": { "myOption": true }` +|plugin||`[]`|`"plugin": "prettier"` or `"plugin": ["pettier", "other"]` +|rule||`null`|`"rule": "'quotes: [2, double]'"` or `"rule": ["quotes: [2, double]", "no-console: 2"]` +|rulesdir||`[]`|`"rulesdir": "/path/to/rules/dir"` or `"env": ["/path/to/rules/dir", "/path/to/other"]` + + +#### `noInlineConfig`: boolean -#### `fix` -This is similar to running ESLint with `--fix` ```json -{ - "jest-runner-eslint": { - "cliOptions": { - "fix": true - } - } +"cliOptions": { + "noInlineConfig": true } ``` -#### `configFile` -This is similar to running ESLint with `-c /path/to/config-file` +#### `env`: string|Array\ + +Single value ```json -{ - "jest-runner-eslint": { - "cliOptions": { - "configFile": "/path/to/config-file" - } - } +"cliOptions": { + "env": "mocha" } ``` -#### `globals` -This is similar to running ESLint with `--globals` +Multiple values ```json -{ - "jest-runner-eslint": { - "cliOptions": { - "globals": ["describe", "it"] - } - } +"cliOptions": { + "env": ["mocha", "other"] } ``` diff --git a/integrationTests/__fixtures__/passing/__eslint__/file.js b/integrationTests/__fixtures__/passing/__eslint__/file.js index 937eb2f..8782c1b 100644 --- a/integrationTests/__fixtures__/passing/__eslint__/file.js +++ b/integrationTests/__fixtures__/passing/__eslint__/file.js @@ -1,4 +1,3 @@ -/* global hello */ const a = 1; if (a === 2) { diff --git a/integrationTests/__fixtures__/passing/jest-runner-eslint.config.js b/integrationTests/__fixtures__/passing/jest-runner-eslint.config.js new file mode 100644 index 0000000..8c71237 --- /dev/null +++ b/integrationTests/__fixtures__/passing/jest-runner-eslint.config.js @@ -0,0 +1,5 @@ +module.exports = { + cliOptions: { + global: ['hello'], + }, +}; diff --git a/src/utils/__tests__/normalizeConfig.test.js b/src/utils/__tests__/normalizeConfig.test.js index 16db319..75e93d1 100644 --- a/src/utils/__tests__/normalizeConfig.test.js +++ b/src/utils/__tests__/normalizeConfig.test.js @@ -1,189 +1,194 @@ const normalizeConfig = require('../normalizeConfig'); +const normalizeCLIOptions = cliOptions => + normalizeConfig({ cliOptions }).cliOptions; + it('ignores unkown options', () => { - expect(normalizeConfig({ other: true })).not.toMatchObject({ + expect(normalizeCLIOptions({ other: true })).not.toMatchObject({ other: true, }); }); it('normalizes noInlineConfig', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ allowInlineConfig: true, }); - expect(normalizeConfig({ noInlineConfig: true })).toMatchObject({ + expect(normalizeCLIOptions({ noInlineConfig: true })).toMatchObject({ allowInlineConfig: false, }); - expect(normalizeConfig({ noInlineConfig: false })).toMatchObject({ + expect(normalizeCLIOptions({ noInlineConfig: false })).toMatchObject({ allowInlineConfig: true, }); }); it('normalizes cacheLocation', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ cacheLocation: '.eslintcache', }); - expect(normalizeConfig({ cacheLocation: '/path/to/cache' })).toMatchObject({ + expect( + normalizeCLIOptions({ cacheLocation: '/path/to/cache' }), + ).toMatchObject({ cacheLocation: '/path/to/cache', }); }); it('normalizes config', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ configFile: null, }); - expect(normalizeConfig({ config: '/path/to/config' })).toMatchObject({ + expect(normalizeCLIOptions({ config: '/path/to/config' })).toMatchObject({ configFile: '/path/to/config', }); }); it('normalizes env', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ envs: [], }); - expect(normalizeConfig({ env: 'mocha' })).toMatchObject({ + expect(normalizeCLIOptions({ env: 'mocha' })).toMatchObject({ envs: ['mocha'], }); - expect(normalizeConfig({ env: ['mocha', 'browser'] })).toMatchObject({ + expect(normalizeCLIOptions({ env: ['mocha', 'browser'] })).toMatchObject({ envs: ['mocha', 'browser'], }); }); it('normalizes ext', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ extensions: ['.js'], }); - expect(normalizeConfig({ ext: '.ts' })).toMatchObject({ + expect(normalizeCLIOptions({ ext: '.ts' })).toMatchObject({ extensions: ['.ts'], }); - expect(normalizeConfig({ ext: ['.js', '.jsx', '.ts'] })).toMatchObject({ + expect(normalizeCLIOptions({ ext: ['.js', '.jsx', '.ts'] })).toMatchObject({ extensions: ['.js', '.jsx', '.ts'], }); }); it('normalizes fix', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ fix: false, }); - expect(normalizeConfig({ fix: true })).toMatchObject({ + expect(normalizeCLIOptions({ fix: true })).toMatchObject({ fix: true, }); }); it('normalizes global', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ globals: [], }); - expect(normalizeConfig({ global: 'it' })).toMatchObject({ + expect(normalizeCLIOptions({ global: 'it' })).toMatchObject({ globals: ['it'], }); - expect(normalizeConfig({ global: ['it', 'describe'] })).toMatchObject({ + expect(normalizeCLIOptions({ global: ['it', 'describe'] })).toMatchObject({ globals: ['it', 'describe'], }); }); it('normalizes noIgnore', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ ignore: true, }); - expect(normalizeConfig({ noIgnore: true })).toMatchObject({ + expect(normalizeCLIOptions({ noIgnore: true })).toMatchObject({ ignore: false, }); }); it('normalizes ignorePath', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ ignorePath: null, }); - expect(normalizeConfig({ ignorePath: '/path/to/ignore' })).toMatchObject({ + expect(normalizeCLIOptions({ ignorePath: '/path/to/ignore' })).toMatchObject({ ignorePath: '/path/to/ignore', }); }); it('normalizes parser', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ parser: 'espree', }); - expect(normalizeConfig({ parser: 'flow' })).toMatchObject({ + expect(normalizeCLIOptions({ parser: 'flow' })).toMatchObject({ parser: 'flow', }); }); it('normalizes parserOptions', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ parserOptions: {}, }); expect( - normalizeConfig({ parserOptions: { ecmaVersion: 2015 } }), + normalizeCLIOptions({ parserOptions: { ecmaVersion: 2015 } }), ).toMatchObject({ parserOptions: { ecmaVersion: 2015 }, }); }); it('normalizes plugin', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ plugins: [], }); - expect(normalizeConfig({ plugin: 'prettier' })).toMatchObject({ + expect(normalizeCLIOptions({ plugin: 'prettier' })).toMatchObject({ plugins: ['prettier'], }); - expect(normalizeConfig({ plugin: ['prettier'] })).toMatchObject({ + expect(normalizeCLIOptions({ plugin: ['prettier'] })).toMatchObject({ plugins: ['prettier'], }); }); it('normalizes rulesdir', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ rulePaths: [], }); - expect(normalizeConfig({ rulesdir: '/path/to/rules' })).toMatchObject({ + expect(normalizeCLIOptions({ rulesdir: '/path/to/rules' })).toMatchObject({ rulePaths: ['/path/to/rules'], }); expect( - normalizeConfig({ rulesdir: ['/path/to/rules', '/other/path'] }), + normalizeCLIOptions({ rulesdir: ['/path/to/rules', '/other/path'] }), ).toMatchObject({ rulePaths: ['/path/to/rules', '/other/path'], }); }); it('normalizes rule', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ rules: null, }); - expect(normalizeConfig({ rule: ['quotes: [2, double]'] })).toMatchObject({ + expect(normalizeCLIOptions({ rule: ['quotes: [2, double]'] })).toMatchObject({ rules: ['quotes: [2, double]'], }); - expect(normalizeConfig({ rule: 'quotes: [2, double]' })).toMatchObject({ + expect(normalizeCLIOptions({ rule: 'quotes: [2, double]' })).toMatchObject({ rules: ['quotes: [2, double]'], }); }); it('normalizes noEslintrc', () => { - expect(normalizeConfig({})).toMatchObject({ + expect(normalizeCLIOptions({})).toMatchObject({ useEslintrc: true, }); - expect(normalizeConfig({ noEslintrc: true })).toMatchObject({ + expect(normalizeCLIOptions({ noEslintrc: true })).toMatchObject({ useEslintrc: false, }); }); diff --git a/src/utils/getESLintOptions.js b/src/utils/getESLintOptions.js index 6049a11..92dc4e7 100644 --- a/src/utils/getESLintOptions.js +++ b/src/utils/getESLintOptions.js @@ -1,15 +1,16 @@ -const normalizeOptions = require('./normalizeOptions'); +const normalizeConfig = require('./normalizeConfig'); const cosmiconfig = require('cosmiconfig'); const explorer = cosmiconfig('jest-runner-eslint', { sync: true }); const getESLintOptions = config => { const result = explorer.load(config.rootDir); + if (result) { - return normalizeOptions(result.config); + return normalizeConfig(result.config); } - return {}; + return normalizeConfig({}); }; module.exports = getESLintOptions; diff --git a/src/utils/normalizeConfig.js b/src/utils/normalizeConfig.js index 88ffb01..aa1a67a 100644 --- a/src/utils/normalizeConfig.js +++ b/src/utils/normalizeConfig.js @@ -70,7 +70,7 @@ const BASE_CONFIG = { }; /* eslint-disable no-param-reassign */ -const normalizeConfig = rawConfig => +const normalizeCliOptions = rawConfig => Object.keys(BASE_CONFIG).reduce((config, key) => { const { name = key, @@ -90,4 +90,10 @@ const normalizeConfig = rawConfig => }, {}); /* eslint-enable no-param-reassign */ +const normalizeConfig = config => { + return Object.assign({}, config, { + cliOptions: normalizeCliOptions(config.cliOptions || {}), + }); +}; + module.exports = normalizeConfig; From 13994ce031227c9880f175180ace9de9e5a34acd Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Tue, 26 Sep 2017 16:27:18 -0500 Subject: [PATCH 7/8] Use better defaults --- src/utils/normalizeConfig.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/utils/normalizeConfig.js b/src/utils/normalizeConfig.js index aa1a67a..31b89c0 100644 --- a/src/utils/normalizeConfig.js +++ b/src/utils/normalizeConfig.js @@ -33,17 +33,17 @@ const BASE_CONFIG = { }, noEslintrc: { name: 'useEslintrc', - default: true, + default: false, transform: negate, }, noIgnore: { name: 'ignore', - default: true, + default: false, transform: negate, }, noInlineConfig: { name: 'allowInlineConfig', - default: true, + default: false, transform: negate, }, parser: { @@ -78,15 +78,11 @@ const normalizeCliOptions = rawConfig => default: defaultValue, } = BASE_CONFIG[key]; - if (rawConfig[key]) { - config[name] = transform(rawConfig[key]); - } - - if (config[name] === undefined) { - config[name] = defaultValue; - } + const value = rawConfig[key] !== undefined ? rawConfig[key] : defaultValue; - return config; + return Object.assign({}, config, { + [name]: transform(value), + }); }, {}); /* eslint-enable no-param-reassign */ From c0dbd4d0ad53b1d4d04a859f2f2dd5f65d4b385b Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Tue, 26 Sep 2017 16:30:23 -0500 Subject: [PATCH 8/8] Small readme changes --- .eslintignore | 1 + README.md | 71 +++++++++++++++++++++------------------------------ 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/.eslintignore b/.eslintignore index ee93496..b413a02 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ **/node_modules build +**/coverage/ integrationTests/__fixtures__/skipped/__eslint__/file.js \ No newline at end of file diff --git a/README.md b/README.md index 78565c9..e66b3bd 100644 --- a/README.md +++ b/README.md @@ -58,54 +58,41 @@ In `package.json` ```json { "jest-runner-eslint": { - "cliOptions": {} + "cliOptions": { + // Options here + } } } ``` -### cliOptions - -jest-runner-eslint maps a lot of ESLint CLI arguments to config options. For example `--fix` is `cliOptions.fix` - -|cliOption|description|default|example -|-----|-----|-----|-----| -|cacheLocation||`.eslintcache`|`"cacheLocation": "/path/to/cache"` -|config||`null`|`"config": "/path/to/config"` -|env||`null`|`"env": "mocha"` or `"env": ["mocha", "other"]` -|ext||`[".js"]`|`"ext": ".jsx"` or `"ext": [".jsx", ".ts"]` -|fix||`false`|`"fix": true` -|global||`[]`|`"global": "it"` or `"global": ["it", "describe"]` -|ignorePath||`null`|`"ignorePath": "/path/to/ignore"` -|noEslintrc||`false`|`"noEslintrc": true` -|noIgnore||`false`|`"noIgnore": true` -|noInlineConfig||`false`|`"noInlineConfig": true` -|parser||`espree`|`"parser": "flow"` -|parserOptions||`{}`|`"parserOptions": { "myOption": true }` -|plugin||`[]`|`"plugin": "prettier"` or `"plugin": ["pettier", "other"]` -|rule||`null`|`"rule": "'quotes: [2, double]'"` or `"rule": ["quotes: [2, double]", "no-console: 2"]` -|rulesdir||`[]`|`"rulesdir": "/path/to/rules/dir"` or `"env": ["/path/to/rules/dir", "/path/to/other"]` - - -#### `noInlineConfig`: boolean - -```json -"cliOptions": { - "noInlineConfig": true +or in `jest-runner-eslint.config.js` +```js +module.exports = { + cliOptions: { + // Options here + } } ``` -#### `env`: string|Array\ -Single value -```json -"cliOptions": { - "env": "mocha" -} -``` +### cliOptions -Multiple values -```json -"cliOptions": { - "env": ["mocha", "other"] -} -``` +jest-runner-eslint maps a lot of ESLint CLI arguments to config options. For example `--fix` is `cliOptions.fix` + +|option|default|example +|-----|-----|-----| +|cacheLocation|`.eslintcache`|`"cacheLocation": "/path/to/cache"` +|config|`null`|`"config": "/path/to/config"` +|env|`null`|`"env": "mocha"` or `"env": ["mocha", "other"]` +|ext|`[".js"]`|`"ext": ".jsx"` or `"ext": [".jsx", ".ts"]` +|fix|`false`|`"fix": true` +|global|`[]`|`"global": "it"` or `"global": ["it", "describe"]` +|ignorePath|`null`|`"ignorePath": "/path/to/ignore"` +|noEslintrc|`false`|`"noEslintrc": true` +|noIgnore|`false`|`"noIgnore": true` +|noInlineConfig|`false`|`"noInlineConfig": true` +|parser|`espree`|`"parser": "flow"` +|parserOptions|`{}`|`"parserOptions": { "myOption": true }` +|plugin|`[]`|`"plugin": "prettier"` or `"plugin": ["pettier", "other"]` +|rule|`null`|`"rule": "'quotes: [2, double]'"` or `"rule": ["quotes: [2, double]", "no-console: 2"]` +|rulesdir|`[]`|`"rulesdir": "/path/to/rules/dir"` or `"env": ["/path/to/rules/dir", "/path/to/other"]`