-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for encapsulation and state BREAKING CHANGE: the API has changed from findNewRules(currentRules, pluginRules) to getRuleFinder('path/to/eslint-config').getUnusedRules()
- Loading branch information
1 parent
1eed148
commit ea617cf
Showing
5 changed files
with
105 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "eslint-find-new-rules", | ||
"version": "0.0.0-semantically-released", | ||
"description": "Find built-in ESLint rules you don't have in your custom config.", | ||
"main": "index.js", | ||
"main": "src/rule-finder.js", | ||
"scripts": { | ||
"cover": "nyc --reporter=lcov --reporter=text ava", | ||
"test": "ava", | ||
|
@@ -16,7 +16,7 @@ | |
"report-coverage": "cat ./coverage/lcov.info | node_modules/.bin/codecov" | ||
}, | ||
"bin": { | ||
"eslint-find-new-rules": "bin.js" | ||
"eslint-find-new-rules": "src/bin.js" | ||
}, | ||
"keywords": [], | ||
"author": "Michał Gołębiowski <[email protected]>", | ||
|
@@ -78,4 +78,4 @@ | |
"url": "https://github.com/kentcdodds/eslint-find-new-rules/issues" | ||
}, | ||
"homepage": "https://github.com/kentcdodds/eslint-find-new-rules#readme" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
var path = require('path') | ||
var fs = require('fs') | ||
|
||
var eslint = require('eslint') | ||
var isAbsolute = require('path-is-absolute') | ||
var difference = require('lodash.difference') | ||
|
||
function _getConfigFile(specifiedFile) { | ||
if (specifiedFile) { | ||
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 require(path.join(process.cwd(), 'package.json')).main | ||
} | ||
} | ||
|
||
function _getConfig(configFile) { | ||
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 _getCurrentRules() { | ||
var config = this.getConfig() | ||
return Object.keys(config.rules) | ||
} | ||
|
||
function _getPluginRules() { | ||
var config = this.getConfig() | ||
var pluginRules = [] | ||
var plugins = config.plugins | ||
if (plugins) { | ||
plugins.forEach(function getPluginRule(plugin) { | ||
var pluginConfig = require('eslint-plugin-' + plugin) | ||
var rules = pluginConfig.rules | ||
pluginRules = pluginRules.concat( | ||
Object.keys(rules).map(function normalizePluginRule(rule) { | ||
return plugin + '/' + rule | ||
}) | ||
) | ||
}) | ||
} | ||
return pluginRules | ||
} | ||
|
||
function _getAllRules() { | ||
var pluginRules = this.getPluginRules() | ||
var allRules = fs | ||
.readdirSync('./node_modules/eslint/lib/rules') | ||
.map(function removeJsFromFilename(filename) { | ||
return filename.replace(/\.js$/, '') | ||
}) | ||
|
||
allRules = allRules.concat(pluginRules) | ||
|
||
return allRules | ||
} | ||
|
||
var RuleFinder = function(specifiedFile) { | ||
var configFile = _getConfigFile(specifiedFile) | ||
var config = _getConfig(configFile) | ||
var currentRules | ||
var pluginRules | ||
var allRules | ||
|
||
this.getConfig = function() { | ||
return config | ||
} | ||
|
||
currentRules = _getCurrentRules.call(this) | ||
this.getCurrentRules = function() { | ||
return currentRules | ||
} | ||
|
||
pluginRules = _getPluginRules.call(this) | ||
this.getPluginRules = function() { | ||
return pluginRules | ||
} | ||
|
||
allRules = _getAllRules.call(this) | ||
this.getAllRules = function() { | ||
return allRules | ||
} | ||
} | ||
|
||
RuleFinder.prototype.getNewRules = function() { | ||
return difference(this.getAllRules(), this.getCurrentRules()) | ||
} | ||
|
||
module.exports = RuleFinder |