Skip to content

Commit d862334

Browse files
author
Kent C. Dodds
committed
Merge pull request #18 from sarbbottam/plugin-rules
feat(plugin): support plugin rules
2 parents e6b6803 + 1ff2cfa commit d862334

File tree

7 files changed

+95
-49
lines changed

7 files changed

+95
-49
lines changed

bin-utils.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var path = require('path')
2+
var isAbsolute = require('path-is-absolute')
3+
var eslint = require('eslint')
4+
5+
function resolvePackagesMain(cwd, packageFile) {
6+
var packageFilePath = path.join(cwd, packageFile)
7+
var packageJson = require(packageFilePath)
8+
return packageJson.main
9+
}
10+
11+
function getConfigFile(specifiedFile) {
12+
//var specifiedFile = process.argv[2]
13+
if (specifiedFile) {
14+
// this is being called like: eslint-find-new-rules eslint-config-mgol
15+
if (isAbsolute(specifiedFile)) {
16+
return specifiedFile
17+
} else {
18+
return path.join(process.cwd(), specifiedFile)
19+
}
20+
} else {
21+
// this is not being called with an arg. Use the package.json `main`
22+
return resolvePackagesMain(process.cwd(), 'package.json')
23+
}
24+
}
25+
26+
function getConfig(specifiedFile) {
27+
var configFile = getConfigFile(specifiedFile)
28+
var cliEngine = new eslint.CLIEngine({
29+
// ignore any config applicable depending on the location on the filesystem
30+
useEslintrc: false,
31+
// point to the particular config
32+
configFile: configFile, // eslint-disable-line object-shorthand
33+
})
34+
return cliEngine.getConfigForFile()
35+
}
36+
37+
function mapPluginRuleNames(plugin) {
38+
return function mapPluginNames(rule) {
39+
return plugin + '/' + rule
40+
}
41+
}
42+
43+
function getCurrentRules(conf) {
44+
var rules = Object.keys(conf.rules)
45+
return rules
46+
}
47+
48+
function getPluginRules(conf) {
49+
var rules = []
50+
var plugins = conf.plugins
51+
if (plugins) {
52+
plugins.forEach(function normalizePluginRule(plugin) {
53+
var thisPluginsConfig = require('eslint-plugin-' + plugin)
54+
var thisPluginsRules = thisPluginsConfig.rules
55+
/* istanbul ignore next */
56+
if (typeof thisPluginsRules === 'object') {
57+
rules = rules.concat(Object.keys(thisPluginsRules).map(mapPluginRuleNames(plugin)))
58+
}
59+
})
60+
}
61+
return rules
62+
}
63+
64+
module.exports = {
65+
getConfig: getConfig, // eslint-disable-line object-shorthand
66+
getCurrentRules: getCurrentRules, // eslint-disable-line object-shorthand
67+
getPluginRules: getPluginRules, // eslint-disable-line object-shorthand
68+
}

bin.js

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,16 @@
44

55
// Prints rules recognized by ESLint that don't appear in the given config
66
// preset. It helps with upgrading the preset when new ESLint gets released.
7-
var path = require('path')
8-
var eslint = require('eslint')
9-
var isAbsolute = require('path-is-absolute')
7+
var binUtils = require('./bin-utils')
108
var findNewRules = require('./index')
119

12-
var currentRules = getRules()
13-
var newRules = findNewRules(currentRules)
10+
var config = binUtils.getConfig(process.argv[2])
11+
var currentRules = binUtils.getCurrentRules(config)
12+
var pluginRules = binUtils.getPluginRules(config)
13+
14+
var newRules = findNewRules(currentRules, pluginRules)
1415

1516
if (newRules.length) {
1617
console.log('New rules to add to the config: ' + newRules.join(', ') + '.') // eslint-disable-line no-console
1718
process.exit(1)
1819
}
19-
20-
function resolvePackagesMain(cwd, packageFile) {
21-
var packageFilePath = path.join(cwd, packageFile)
22-
var packageJson = require(packageFilePath)
23-
return packageJson.main
24-
}
25-
26-
function getConfigFile() {
27-
var specifiedFile = process.argv[2]
28-
if (specifiedFile) {
29-
// this is being called like: eslint-find-new-rules eslint-config-mgol
30-
if (isAbsolute(specifiedFile)) {
31-
return specifiedFile
32-
} else {
33-
return path.join(process.cwd(), specifiedFile)
34-
}
35-
} else {
36-
// this is not being called with an arg. Use the package.json `main`
37-
return resolvePackagesMain(process.cwd(), 'package.json')
38-
}
39-
}
40-
41-
function getConfig(file) {
42-
var cliEngine = new eslint.CLIEngine({
43-
// ignore any config applicable depending on the location on the filesystem
44-
useEslintrc: false,
45-
// point to the particular config
46-
configFile: file,
47-
})
48-
var config = cliEngine.getConfigForFile(file)
49-
return config
50-
}
51-
52-
function getRules() {
53-
var configFile = getConfigFile()
54-
var config = getConfig(configFile)
55-
var rules = Object.keys(config.rules)
56-
return rules
57-
}

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ var difference = require('lodash.difference')
33

44
module.exports = findNewRules
55

6-
function findNewRules(currentRules) {
6+
function findNewRules(currentRules, pluginRules) {
77
var allRules = fs
88
.readdirSync('./node_modules/eslint/lib/rules')
99
.map(function removeJsFromFilename(filename) {
1010
return filename.replace(/\.js$/, '')
1111
})
1212

13+
if (pluginRules) {
14+
allRules = allRules.concat(pluginRules)
15+
}
16+
1317
return difference(allRules, currentRules)
1418
}
1519

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"cover": "nyc --reporter=lcov --reporter=text ava",
88
"test": "ava",
9-
"lint": "eslint .",
9+
"lint": "eslint --ignore-pattern test/fixtures .",
1010
"check-kentcdodds": "./bin.js eslint-config-kentcdodds",
1111
"update-contributors": "all-contributors generate",
1212
"commit": "git-cz",

test/bin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ const mainFile = path.join(process.cwd(), specifiedFile)
1212
const extendingFile = './fixtures/extending-config.json'
1313
const indexStub = {
1414
'./index': () => rules,
15+
'eslint-plugin-react': {
16+
rules: {
17+
'jsx-uses-react': true,
18+
'no-multi-comp': true,
19+
'prop-types': true,
20+
},
21+
'@noCallThru': true,
22+
'@global': true,
23+
},
1524
}
1625
function outputStatement() {
1726
return `New rules to add to the config: ${rules.join(', ')}.`

test/fixtures/.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
rules: {
33
"no-console": [2],
4-
}
4+
},
5+
"plugins": [
6+
"react"
7+
]
58
}

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const findNewRules = proxyquire('../index', {
88
})
99

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

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

0 commit comments

Comments
 (0)