diff --git a/.all-contributorsrc b/.all-contributorsrc index c7f04f4..9a77f61 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", "doc", "test" + ] } ] } diff --git a/README.md b/README.md index 395347b..8e7530a 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: @@ -39,6 +47,8 @@ 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`. +You may specify any [config format supported by ESLint](http://eslint.org/docs/user-guide/configuring). + ### Absolute Path You can also provide an absolute path: @@ -47,6 +57,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: @@ -72,4 +84,3 @@ Special thanks to [@mgol](https://github.com/mgol) who created the original scri ## LICENSE MIT - diff --git a/bin.js b/bin.js index 2640074..661c9b7 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,41 @@ if (newRules.length) { process.exit(1) } -function getConfig() { +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) { // 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 resolvePackagesMain(process.cwd(), 'package.json') } } +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: file, + }) + var config = cliEngine.getConfigForFile(file) + return config +} + +function getRules() { + var configFile = getConfigFile() + var config = getConfig(configFile) + var rules = Object.keys(config.rules) + return rules +} 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": { diff --git a/test/bin.js b/test/bin.js index 8b2b61c..d855f8d 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 specifiedFile = './fixtures/.eslintrc' +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' 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/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 new file mode 100644 index 0000000..3c2fce3 --- /dev/null +++ b/test/fixtures/no-path/package.json @@ -0,0 +1,12 @@ +{ + "name": "fake-project", + "version": "0.0.0", + "description": "Simulating a project's package.json.", + "private": true, + "main": "./index.js", + "eslintConfig": { + "rules": { + "no-console": [2] + } + } +}