-
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.
Merge pull request #18 from sarbbottam/plugin-rules
feat(plugin): support plugin rules
- Loading branch information
Showing
7 changed files
with
95 additions
and
49 deletions.
There are no files selected for viewing
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,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 | ||
} |
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
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
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module.exports = { | ||
rules: { | ||
"no-console": [2], | ||
} | ||
}, | ||
"plugins": [ | ||
"react" | ||
] | ||
} |
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