diff --git a/cli.js b/cli.js index 57add500..4a83b7b3 100755 --- a/cli.js +++ b/cli.js @@ -79,7 +79,7 @@ updateNotifier({pkg: cli.pkg}).notify(); const input = cli.input; const opts = cli.flags; -function log(report) { +const log = report => { // legacy // TODO: remove in 1.0.0 if (opts.compact) { @@ -92,7 +92,7 @@ function log(report) { process.exit(report.errorCount === 0 ? 0 : 1); } -function open(report) { +const open = report => { if (report.errorCount === 0) { return; } @@ -110,11 +110,7 @@ Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc: } const executableName = editor.split(path.sep).pop(); - - function lineColumn(message) { - return `${message.line}:${message.column}`; - } - + const lineColumn = message => `${message.line}:${message.column}`; const args = []; report.results diff --git a/index.js b/index.js index 7be1cb32..df24c965 100644 --- a/index.js +++ b/index.js @@ -27,8 +27,8 @@ exports.lintText = (str, opts) => { } if (opts.filename) { - const gitIgnores = optionsManager.getGitIgnores(opts); const filename = path.relative(opts.cwd, opts.filename); + const gitIgnores = optionsManager.getGitIgnores(opts); const glob = [filename].concat(gitIgnores); if (multimatch(glob, opts.ignores).length > 0) { @@ -51,15 +51,11 @@ exports.lintText = (str, opts) => { return processReport(report, opts); }; -exports.lintFiles = (pattern, opts) => { +exports.lintFiles = (patterns, opts) => { opts = optionsManager.preprocess(opts); - - if (pattern.length === 0) { - pattern = '**/*'; - } + patterns = patterns.length === 0 ? ['**/*'] : Array.isArray(patterns) ? patterns : [patterns]; const gitIgnores = optionsManager.getGitIgnores(opts); - const patterns = Array.isArray(pattern) ? pattern : [pattern]; const glob = patterns.concat(gitIgnores); return globby(glob, {ignore: opts.ignores, nodir: true}).then(paths => { diff --git a/options-manager.js b/options-manager.js index aec09a3a..8ab8ad12 100644 --- a/options-manager.js +++ b/options-manager.js @@ -42,7 +42,7 @@ const DEFAULT_CONFIG = { } }; -function normalizeOpts(opts) { +const normalizeOpts = opts => { opts = Object.assign({}, opts); // Alias to help humans @@ -75,25 +75,23 @@ function normalizeOpts(opts) { return opts; } -function mergeWithPkgConf(opts) { +const mergeWithPkgConf = opts => { opts = Object.assign({cwd: process.cwd()}, opts); const conf = pkgConf.sync('xo', {cwd: opts.cwd, skipOnFalse: true}); return Object.assign({}, conf, opts); } // Define the shape of deep properties for deepAssign -function emptyOptions() { - return { - rules: {}, - settings: {}, - globals: [], - envs: [], - plugins: [], - extends: [] - }; -} - -function buildConfig(opts) { +const emptyOptions = () => ({ + rules: {}, + settings: {}, + globals: [], + envs: [], + plugins: [], + extends: [] +}); + +const buildConfig = opts => { const config = deepAssign( emptyOptions(), DEFAULT_CONFIG, @@ -173,7 +171,7 @@ function buildConfig(opts) { // The hash value is a binary representation of which elements in the `overrides` array apply to the path. // // If overrides.length === 4, and only the first and third elements apply, then our hash is: 1010 (in binary) -function findApplicableOverrides(path, overrides) { +const findApplicableOverrides = (path, overrides) => { let hash = 0; const applicable = []; @@ -192,12 +190,14 @@ function findApplicableOverrides(path, overrides) { }; } -function mergeApplicableOverrides(baseOptions, applicableOverrides) { - return deepAssign.apply(null, [emptyOptions(), baseOptions].concat(applicableOverrides.map(normalizeOpts))); +const mergeApplicableOverrides = (baseOptions, applicableOverrides) => { + applicableOverrides = applicableOverrides.map(normalizeOpts); + const overrides = [emptyOptions(), baseOptions].concat(applicableOverrides); + return deepAssign.apply(null, overrides); } // Creates grouped sets of merged options together with the paths they apply to. -function groupConfigs(paths, baseOptions, overrides) { +const groupConfigs = (paths, baseOptions, overrides) => { const map = {}; const arr = []; @@ -220,35 +220,32 @@ function groupConfigs(paths, baseOptions, overrides) { return arr; } -function getIgnores(opts) { +const getIgnores = opts => { opts.ignores = DEFAULT_IGNORE.concat(opts.ignores || []); return opts; } -function getGitIgnores(opts) { - const gitignores = globby.sync('**/.gitignore', { +const getGitIgnores = opts => globby + .sync('**/.gitignore', { ignore: opts.ignores || [], cwd: opts.cwd || process.cwd() - }); - - return gitignores - .map(pathToGitignore => { - const patterns = parseGitignore(pathToGitignore); - const base = path.dirname(pathToGitignore); - - return patterns - .map(pattern => { - const negate = !pattern.startsWith('!'); - const patternPath = negate ? pattern : pattern.substr(1); - return {negate, pattern: path.join(base, patternPath)}; - }) - .sort(pattern => pattern.negate ? 1 : -1) - .map(item => item.negate ? `!${item.pattern}` : item.pattern); - }) - .reduce((a, b) => a.concat(b), []); -} - -function preprocess(opts) { + }) + .map(pathToGitignore => { + const patterns = parseGitignore(pathToGitignore); + const base = path.dirname(pathToGitignore); + + return patterns + .map(pattern => { + const negate = !pattern.startsWith('!'); + const patternPath = negate ? pattern : pattern.substr(1); + return {negate, pattern: path.join(base, patternPath)}; + }) + .sort(pattern => pattern.negate ? 1 : -1) + .map(item => item.negate ? `!${item.pattern}` : item.pattern); + }) + .reduce((a, b) => a.concat(b), []); + +const preprocess = opts => { opts = mergeWithPkgConf(opts); opts = normalizeOpts(opts); opts = getIgnores(opts);