Skip to content

Commit

Permalink
refactor(code): using constructor
Browse files Browse the repository at this point in the history
for encapsulation and state

BREAKING CHANGE: the API has changed
from findNewRules(currentRules, pluginRules)
to getRuleFinder('path/to/eslint-config').getUnusedRules()
  • Loading branch information
sarbbottam committed Mar 28, 2016
1 parent 1eed148 commit ea617cf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 89 deletions.
60 changes: 0 additions & 60 deletions bin-utils.js

This file was deleted.

11 changes: 4 additions & 7 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

// 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 binUtils = require('./bin-utils')
var findNewRules = require('./index')
var RuleFinder = require('./rule-finder')
var specifiedFile = process.argv[2]
var ruleFinder = new RuleFinder(specifiedFile);

var config = binUtils.getConfig(process.argv[2])
var currentRules = binUtils.getCurrentRules(config)
var pluginRules = binUtils.getPluginRules(config)

var newRules = findNewRules(currentRules, pluginRules)
var newRules = ruleFinder.getNewRules()

if (newRules.length) {
console.log('New rules to add to the config: ' + newRules.join(', ') + '.') // eslint-disable-line no-console
Expand Down
19 changes: 0 additions & 19 deletions index.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]>",
Expand Down Expand Up @@ -78,4 +78,4 @@
"url": "https://github.com/kentcdodds/eslint-find-new-rules/issues"
},
"homepage": "https://github.com/kentcdodds/eslint-find-new-rules#readme"
}
}
98 changes: 98 additions & 0 deletions rule-finder.js
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

0 comments on commit ea617cf

Please sign in to comment.