Skip to content

Commit

Permalink
feat(plugin): support plugin rules
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
sarbbottam committed Mar 20, 2016
1 parent e6b6803 commit 256ff6e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
29 changes: 21 additions & 8 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ var eslint = require('eslint')
var isAbsolute = require('path-is-absolute')
var findNewRules = require('./index')

var currentRules = getRules()
var newRules = findNewRules(currentRules)
var configFile = getConfigFile()
var config = getConfig(configFile)

var currentRules = getCurrentRules(config)
var pluginRules = getPluginRules(config)

var newRules = findNewRules(currentRules, pluginRules)

if (newRules.length) {
console.log('New rules to add to the config: ' + newRules.join(', ') + '.') // eslint-disable-line no-console
Expand Down Expand Up @@ -45,13 +50,21 @@ function getConfig(file) {
// point to the particular config
configFile: file,
})
var config = cliEngine.getConfigForFile(file)
return config
return cliEngine.getConfigForFile(file)
}

function getCurrentRules(conf) {
var rules = Object.keys(conf.rules)
return rules
}

function getRules() {
var configFile = getConfigFile()
var config = getConfig(configFile)
var rules = Object.keys(config.rules)
function getPluginRules(conf) {
var rules = []
var plugins = conf.plugins
if (plugins) {
plugins.forEach(function normalizePluginRule(plugin) {
rules.concat(require('eslint-plugin-' + plugin).rules)
})
}
return rules
}
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ var difference = require('lodash.difference')

module.exports = findNewRules

function findNewRules(currentRules) {
function findNewRules(currentRules, pluginRules) {
var allRules = fs
.readdirSync('./node_modules/eslint/lib/rules')
.map(function removeJsFromFilename(filename) {
return filename.replace(/\.js$/, '')
})

if (pluginRules) {
allRules = allRules.concat(pluginRules)
}

return difference(allRules, currentRules)
}

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cz-conventional-changelog": "1.1.5",
"eslint": "2.4.0",
"eslint-config-kentcdodds": "6.0.0",
"eslint-plugin-react": "^4.2.3",
"ghooks": "1.0.3",
"npm-run-all": "1.5.3",
"nyc": "6.1.1",
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
rules: {
"no-console": [2],
}
},
"plugins": [
"react"
]
}
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const findNewRules = proxyquire('../index', {
})

test('returns the difference between what it finds in eslint/lib/rules and the rules array it is passed', t => {
const missingRules = findNewRules(['baz-thing', 'foo-rule'])
t.same(missingRules, ['bar-rule'])
const missingRules = findNewRules(['baz-thing', 'foo-rule'], ['react-foo'])
t.same(missingRules, ['bar-rule', 'react-foo'])
})

test('returns an empty array if there is no difference', t => {
Expand Down

0 comments on commit 256ff6e

Please sign in to comment.