Skip to content

Commit

Permalink
Merge pull request #4 from ta2edchimp/support-extends
Browse files Browse the repository at this point in the history
Support `extends`
  • Loading branch information
Kent C. Dodds committed Mar 19, 2016
2 parents fd8de57 + 519b643 commit 6cedc7f
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

## 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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -72,4 +84,3 @@ Special thanks to [@mgol](https://github.com/mgol) who created the original scri
## LICENSE

MIT

34 changes: 29 additions & 5 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,53 @@
// 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) {
console.log('New rules to add to the config: ' + newRules.join(', ') + '.') // eslint-disable-line no-console
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
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"semantic-release": "4.3.5",
"validate-commit-msg": "2.4.0"
},
"peerDependencies": {
"eslint": "^2.0.0"
},
"eslintConfig": {
"extends": "kentcdodds",
"parserOptions": {
Expand Down
14 changes: 11 additions & 3 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
})

Expand All @@ -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)
})
2 changes: 1 addition & 1 deletion test/fixtures/.eslintrc → test/fixtures/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
rules: {
foo: true,
"no-console": [2],
}
}
5 changes: 5 additions & 0 deletions test/fixtures/base-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rules:
no-console:
- 2
no-alert:
- 2
6 changes: 6 additions & 0 deletions test/fixtures/extending-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./base-config.yml",
"rules": {
"comma-dangle": [2, "never"]
}
}
5 changes: 5 additions & 0 deletions test/fixtures/no-path/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'no-alert': [2],
},
}
12 changes: 12 additions & 0 deletions test/fixtures/no-path/package.json
Original file line number Diff line number Diff line change
@@ -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]
}
}
}

0 comments on commit 6cedc7f

Please sign in to comment.