Skip to content

Commit

Permalink
Merge pull request #18 from sarbbottam/plugin-rules
Browse files Browse the repository at this point in the history
feat(plugin): support plugin rules
  • Loading branch information
Kent C. Dodds committed Mar 20, 2016
2 parents e6b6803 + 1ff2cfa commit d862334
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 49 deletions.
68 changes: 68 additions & 0 deletions bin-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var path = require('path')
var isAbsolute = require('path-is-absolute')
var eslint = require('eslint')

function resolvePackagesMain(cwd, packageFile) {
var packageFilePath = path.join(cwd, packageFile)
var packageJson = require(packageFilePath)
return packageJson.main
}

function getConfigFile(specifiedFile) {
//var specifiedFile = process.argv[2]
if (specifiedFile) {
// this is being called like: eslint-find-new-rules eslint-config-mgol
if (isAbsolute(specifiedFile)) {
return specifiedFile
} else {
return path.join(process.cwd(), specifiedFile)
}
} else {
// this is not being called with an arg. Use the package.json `main`
return resolvePackagesMain(process.cwd(), 'package.json')
}
}

function getConfig(specifiedFile) {
var configFile = getConfigFile(specifiedFile)
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, // eslint-disable-line object-shorthand
})
return cliEngine.getConfigForFile()
}

function mapPluginRuleNames(plugin) {
return function mapPluginNames(rule) {
return plugin + '/' + rule
}
}

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

function getPluginRules(conf) {
var rules = []
var plugins = conf.plugins
if (plugins) {
plugins.forEach(function normalizePluginRule(plugin) {
var thisPluginsConfig = require('eslint-plugin-' + plugin)
var thisPluginsRules = thisPluginsConfig.rules
/* istanbul ignore next */
if (typeof thisPluginsRules === 'object') {
rules = rules.concat(Object.keys(thisPluginsRules).map(mapPluginRuleNames(plugin)))
}
})
}
return rules
}

module.exports = {
getConfig: getConfig, // eslint-disable-line object-shorthand
getCurrentRules: getCurrentRules, // eslint-disable-line object-shorthand
getPluginRules: getPluginRules, // eslint-disable-line object-shorthand
}
50 changes: 6 additions & 44 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,16 @@

// 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 binUtils = require('./bin-utils')
var findNewRules = require('./index')

var currentRules = getRules()
var newRules = findNewRules(currentRules)
var config = binUtils.getConfig(process.argv[2])
var currentRules = binUtils.getCurrentRules(config)
var pluginRules = binUtils.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
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) {
// this is being called like: eslint-find-new-rules eslint-config-mgol
if (isAbsolute(specifiedFile)) {
return specifiedFile
} else {
return path.join(process.cwd(), specifiedFile)
}
} else {
// this is not being called with an arg. Use the package.json `main`
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
}
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)
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"cover": "nyc --reporter=lcov --reporter=text ava",
"test": "ava",
"lint": "eslint .",
"lint": "eslint --ignore-pattern test/fixtures .",
"check-kentcdodds": "./bin.js eslint-config-kentcdodds",
"update-contributors": "all-contributors generate",
"commit": "git-cz",
Expand Down
9 changes: 9 additions & 0 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ const mainFile = path.join(process.cwd(), specifiedFile)
const extendingFile = './fixtures/extending-config.json'
const indexStub = {
'./index': () => rules,
'eslint-plugin-react': {
rules: {
'jsx-uses-react': true,
'no-multi-comp': true,
'prop-types': true,
},
'@noCallThru': true,
'@global': true,
},
}
function outputStatement() {
return `New rules to add to the config: ${rules.join(', ')}.`
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 d862334

Please sign in to comment.