Skip to content

Commit

Permalink
Minor code style tweaks (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil authored and sindresorhus committed Mar 20, 2017
1 parent 27c65a2 commit d5d18f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
10 changes: 3 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 => {
Expand Down
79 changes: 38 additions & 41 deletions options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DEFAULT_CONFIG = {
}
};

function normalizeOpts(opts) {
const normalizeOpts = opts => {
opts = Object.assign({}, opts);

// Alias to help humans
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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 = [];

Expand All @@ -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);
Expand Down

0 comments on commit d5d18f4

Please sign in to comment.