From 18b8aae8361aff42ae0df82118fd625e00f9b3f5 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Wed, 16 Mar 2016 18:03:08 +0100 Subject: [PATCH 01/13] feat(lib): Support extends and plugins Collect configured rules using the eslint api methods, taking extending configs and plugins into account. --- bin.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/bin.js b/bin.js index 2640074..be20399 100755 --- a/bin.js +++ b/bin.js @@ -5,10 +5,11 @@ // Prints rules recognized by ESLint that don't appear in the given config // preset. It helps with upgrading the preset when new ESLint gets released. var path = require('path') +var eslint = require('eslint') var isAbsolute = require('path-is-absolute') var findNewRules = require('./index') -var currentRules = Object.keys(getConfig().rules) +var currentRules = getRules() var newRules = findNewRules(currentRules) if (newRules.length) { @@ -16,18 +17,35 @@ if (newRules.length) { process.exit(1) } -function getConfig() { +function getConfigFile() { var specifiedFile = process.argv[2] if (specifiedFile) { // this is being called like: eslint-find-new-rules eslint-config-mgol if (isAbsolute(specifiedFile)) { - return require(specifiedFile) + return specifiedFile } else { - return require(path.join(process.cwd(), specifiedFile)) + return path.join(process.cwd(), specifiedFile) } } else { // this is not being called with an arg. Use the package.json `main` - return require(process.cwd()) + return path.join(process.cwd(), 'package.json') } } +function getConfig(configFile) { + var cliEngine = new eslint.CLIEngine({ + // ignore any config applicable depending on the location on the filesystem + useEslintrc: false, + // point to the particular config + configFile, + }) + var config = cliEngine.getConfigForFile(configFile) + return config +} + +function getRules() { + var configFile = getConfigFile() + var config = getConfig(configFile) + var rules = Object.keys(config.rules) + return rules +} From cecadda34b23ca229192ec020146a670a23113ec Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 11:32:22 +0100 Subject: [PATCH 02/13] test(tests): Test extending configs --- test/bin.js | 12 ++++++++++-- test/fixtures/{.eslintrc => .eslintrc.js} | 2 +- test/fixtures/base-config.yml | 5 +++++ test/fixtures/extending-config.json | 6 ++++++ test/fixtures/no-path/package.json | 11 +++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) rename test/fixtures/{.eslintrc => .eslintrc.js} (61%) create mode 100644 test/fixtures/base-config.yml create mode 100644 test/fixtures/extending-config.json create mode 100644 test/fixtures/no-path/package.json diff --git a/test/bin.js b/test/bin.js index 8b2b61c..87bbdec 100644 --- a/test/bin.js +++ b/test/bin.js @@ -6,8 +6,10 @@ let rules = [] const processCwd = process.cwd const processExit = process.exit const consoleLog = console.log // eslint-disable-line no-console +const noSpecifiedFile = './fixtures/no-path' const specifiedFile = './fixtures/.eslintrc' const mainFile = path.join(process.cwd(), specifiedFile) +const extendingFile = './fixtures/extending-config.json' const indexStub = { './index': () => rules, } @@ -38,7 +40,7 @@ test('no new rule and relative path', () => { }) test('no new rule and no path', () => { - process.cwd = () => mainFile + process.cwd = () => noSpecifiedFile proxyquire('../bin', indexStub) }) @@ -56,6 +58,12 @@ test('new rule and relative path', () => { test('new rule and no path', () => { rules = ['foo', 'bar', 'baz'] - process.cwd = () => mainFile + process.cwd = () => noSpecifiedFile + proxyquire('../bin', indexStub) +}) + +test('new rule (extending)', () => { + rules = ['comma-dangle', 'no-console', 'no-alert'] + process.argv[2] = extendingFile proxyquire('../bin', indexStub) }) diff --git a/test/fixtures/.eslintrc b/test/fixtures/.eslintrc.js similarity index 61% rename from test/fixtures/.eslintrc rename to test/fixtures/.eslintrc.js index e33e9b5..474b5cd 100644 --- a/test/fixtures/.eslintrc +++ b/test/fixtures/.eslintrc.js @@ -1,5 +1,5 @@ module.exports = { rules: { - foo: true, + "no-console": [2], } } diff --git a/test/fixtures/base-config.yml b/test/fixtures/base-config.yml new file mode 100644 index 0000000..0509a62 --- /dev/null +++ b/test/fixtures/base-config.yml @@ -0,0 +1,5 @@ +rules: + no-console: + - 2 + no-alert: + - 2 diff --git a/test/fixtures/extending-config.json b/test/fixtures/extending-config.json new file mode 100644 index 0000000..70e88bb --- /dev/null +++ b/test/fixtures/extending-config.json @@ -0,0 +1,6 @@ +{ + "extends": "./base-config.yml", + "rules": { + "comma-dangle": [2, "never"] + } +} diff --git a/test/fixtures/no-path/package.json b/test/fixtures/no-path/package.json new file mode 100644 index 0000000..78b16f8 --- /dev/null +++ b/test/fixtures/no-path/package.json @@ -0,0 +1,11 @@ +{ + "name": "fake-project", + "version": "0.0.0", + "description": "Simulating a project's package.json.", + "private": true, + "eslintConfig": { + "rules": { + "no-console": [2] + } + } +} From 5a8859a4369714c9758c410a274e8f86e831f2dd Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 11:32:22 +0100 Subject: [PATCH 03/13] test(tests): Test extending configs --- test/bin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bin.js b/test/bin.js index 87bbdec..0ef7fd5 100644 --- a/test/bin.js +++ b/test/bin.js @@ -7,7 +7,7 @@ const processCwd = process.cwd const processExit = process.exit const consoleLog = console.log // eslint-disable-line no-console const noSpecifiedFile = './fixtures/no-path' -const specifiedFile = './fixtures/.eslintrc' +const specifiedFile = './fixtures/.eslintrc.js' const mainFile = path.join(process.cwd(), specifiedFile) const extendingFile = './fixtures/extending-config.json' const indexStub = { From c1205f97fc95c2266f442c3d794e16574027ca44 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 11:46:03 +0100 Subject: [PATCH 04/13] fix(lib): Assure comp. with Node 0.10 --- bin.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin.js b/bin.js index be20399..50cf47b 100755 --- a/bin.js +++ b/bin.js @@ -32,14 +32,14 @@ function getConfigFile() { } } -function getConfig(configFile) { +function getConfig(file) { var cliEngine = new eslint.CLIEngine({ // ignore any config applicable depending on the location on the filesystem useEslintrc: false, // point to the particular config - configFile, + configFile: file, }) - var config = cliEngine.getConfigForFile(configFile) + var config = cliEngine.getConfigForFile(file) return config } From 297c510c1209bc1e0978811b2feb8f8b0a6ec8a5 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 12:09:16 +0100 Subject: [PATCH 05/13] docs(contributors): added ta2edchimp to the list --- .all-contributorsrc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index c7f04f4..f826203 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -29,6 +29,15 @@ "contributions": [ "test" ] + }, + { + "login": "ta2edchimp", + "name": "Andreas Windt", + "avatar_url": "https://avatars1.githubusercontent.com/u/262436?v=3", + "html_url": "https://twitter.com/ta2edchimp", + "contributions": [ + "code", "test" + ] } ] } From 5277e663eb3684829230af99823d5c4b605fbdfa Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 19:00:53 +0100 Subject: [PATCH 06/13] chore(dependencies): Add eslint as a peer dependency --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index ffbc0c0..29972cf 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,9 @@ "semantic-release": "4.3.5", "validate-commit-msg": "2.4.0" }, + "peerDependencies": { + "eslint": "^2.0.0" + }, "eslintConfig": { "extends": "kentcdodds", "parserOptions": { From 14a9de7b36242c88e57be175d7ec675811a141e6 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 19:04:18 +0100 Subject: [PATCH 07/13] fix(lib): Restore original behavior when invoking without any argument Restore the module's original behavior of interpreting the current package's main script as source of an eslint config, rather than taking the local package.json's eslintConfig property into account. --- bin.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin.js b/bin.js index 50cf47b..661c9b7 100755 --- a/bin.js +++ b/bin.js @@ -17,6 +17,12 @@ if (newRules.length) { process.exit(1) } +function resolvePackagesMain(cwd, packageFile) { + var packageFilePath = path.join(cwd, packageFile) + var packageJson = require(packageFilePath) + return packageJson.main +} + function getConfigFile() { var specifiedFile = process.argv[2] if (specifiedFile) { @@ -28,7 +34,7 @@ function getConfigFile() { } } else { // this is not being called with an arg. Use the package.json `main` - return path.join(process.cwd(), 'package.json') + return resolvePackagesMain(process.cwd(), 'package.json') } } From ffb0f16d46bc207821c44b3363ea011dc2dfdfa6 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 19:05:22 +0100 Subject: [PATCH 08/13] fix(tests): Alter tests to reflect the module's originally intended behavior --- test/bin.js | 2 +- test/fixtures/no-path/index.js | 5 +++++ test/fixtures/no-path/package.json | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/no-path/index.js diff --git a/test/bin.js b/test/bin.js index 0ef7fd5..d855f8d 100644 --- a/test/bin.js +++ b/test/bin.js @@ -6,7 +6,7 @@ let rules = [] const processCwd = process.cwd const processExit = process.exit const consoleLog = console.log // eslint-disable-line no-console -const noSpecifiedFile = './fixtures/no-path' +const noSpecifiedFile = path.resolve(process.cwd(), './fixtures/no-path') const specifiedFile = './fixtures/.eslintrc.js' const mainFile = path.join(process.cwd(), specifiedFile) const extendingFile = './fixtures/extending-config.json' diff --git a/test/fixtures/no-path/index.js b/test/fixtures/no-path/index.js new file mode 100644 index 0000000..e4b82ae --- /dev/null +++ b/test/fixtures/no-path/index.js @@ -0,0 +1,5 @@ +module.exports = { + rules: { + 'no-alert': [2], + }, +} diff --git a/test/fixtures/no-path/package.json b/test/fixtures/no-path/package.json index 78b16f8..3c2fce3 100644 --- a/test/fixtures/no-path/package.json +++ b/test/fixtures/no-path/package.json @@ -3,6 +3,7 @@ "version": "0.0.0", "description": "Simulating a project's package.json.", "private": true, + "main": "./index.js", "eslintConfig": { "rules": { "no-console": [2] From 212dc8512d91d6d9bd85ef18b0fe522963f5df92 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 19:21:08 +0100 Subject: [PATCH 09/13] docs(usage): Clarify supported file types --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 395347b..5c83fe3 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ eslint-find-new-rules ./index.js This is resolved relative to the `process.cwd()` which, in the context of npm scripts is always the location of your `package.json`. +#### Supported file types + +You may specify any [config file originally supported by ESLint](http://eslint.org/docs/user-guide/configuring) (`.eslintrc.*` or a `package.json` containing an `eslintConfig` field) as well as any JavaScript, JSON or YAML file containing a proper eslint configuration. + ### Absolute Path You can also provide an absolute path: @@ -72,4 +76,3 @@ Special thanks to [@mgol](https://github.com/mgol) who created the original scri ## LICENSE MIT - From e843a84bec014fbc5e5ec92ccdc4572eb71b332d Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 19:31:53 +0100 Subject: [PATCH 10/13] docs(usage): Add note about location restrictions of config files to be tested --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5c83fe3..2bf25dc 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ You can also provide an absolute path: eslint-find-new-rules ~/Developer/eslint-config-kentcdodds/index.js ``` +**Please note** that any tested ESLint config file must reside below your project's root. + ### Default to `main` It will also default to the `main` in your `package.json`, so you can omit the argument altogether: From 3fb35abe9364ac19007c8bd7e13c3d2cacf9b7b3 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 19:35:16 +0100 Subject: [PATCH 11/13] docs(install): Add instruction on how to install locally as dev dependency --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 2bf25dc..a334154 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,14 @@ to identify built-in ESLint rules that you're not explicitly configuring. [![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors) +## Installation + +Simply install locally as a development dependency to your project's package: + +``` +npm install --save-dev eslint-find-new-rules +``` + ## Usage The intended usage is as an npm script: From 7bb5d070797c2d0ea6c61e57c770d2423ba2d28a Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 20:12:13 +0100 Subject: [PATCH 12/13] docs(usage): Clarify supported config format --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index a334154..8e7530a 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,7 @@ eslint-find-new-rules ./index.js This is resolved relative to the `process.cwd()` which, in the context of npm scripts is always the location of your `package.json`. -#### Supported file types - -You may specify any [config file originally supported by ESLint](http://eslint.org/docs/user-guide/configuring) (`.eslintrc.*` or a `package.json` containing an `eslintConfig` field) as well as any JavaScript, JSON or YAML file containing a proper eslint configuration. +You may specify any [config format supported by ESLint](http://eslint.org/docs/user-guide/configuring). ### Absolute Path From 519b643d5c544f3bd26befe063daf9837d661891 Mon Sep 17 00:00:00 2001 From: ta2edchimp Date: Sat, 19 Mar 2016 20:15:37 +0100 Subject: [PATCH 13/13] docs(contributors): Added ta2edchimp as contributor to doc --- .all-contributorsrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index f826203..9a77f61 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -36,7 +36,7 @@ "avatar_url": "https://avatars1.githubusercontent.com/u/262436?v=3", "html_url": "https://twitter.com/ta2edchimp", "contributions": [ - "code", "test" + "code", "doc", "test" ] } ]