Skip to content

Commit

Permalink
Various code style improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 13, 2018
1 parent 9e75826 commit 92f2870
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 178 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
*.ai binary
63 changes: 31 additions & 32 deletions cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,71 +112,70 @@ const cli = meow(`

updateNotifier({pkg: cli.pkg}).notify();

const {input, flags: opts} = cli;

// Make data types for `opts.space` match those of the API
// Check for string type because `xo --no-space` sets `opts.space` to `false`
if (typeof opts.space === 'string') {
if (/^\d+$/u.test(opts.space)) {
opts.space = parseInt(opts.space, 10);
} else if (opts.space === 'true') {
opts.space = true;
} else if (opts.space === 'false') {
opts.space = false;
const {input, flags: options} = cli;

// Make data types for `options.space` match those of the API
// Check for string type because `xo --no-space` sets `options.space` to `false`
if (typeof options.space === 'string') {
if (/^\d+$/u.test(options.space)) {
options.space = parseInt(options.space, 10);
} else if (options.space === 'true') {
options.space = true;
} else if (options.space === 'false') {
options.space = false;
} else {
if (opts.space !== '') {
// Assume `opts.space` was set to a filename when run as `xo --space file.js`
input.push(opts.space);
if (options.space !== '') {
// Assume `options.space` was set to a filename when run as `xo --space file.js`
input.push(options.space);
}
opts.space = true;
options.space = true;
}
}

const log = report => {
const reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty;

const reporter = options.reporter ? xo.getFormatter(options.reporter) : formatterPretty;
process.stdout.write(reporter(report.results));
process.exit(report.errorCount === 0 ? 0 : 1);
};

// `xo -` => `xo --stdin`
if (input[0] === '-') {
opts.stdin = true;
options.stdin = true;
input.shift();
}

if (opts.nodeVersion) {
if (opts.nodeVersion === 'false') {
opts.nodeVersion = false;
} else if (!semver.validRange(opts.nodeVersion)) {
if (options.nodeVersion) {
if (options.nodeVersion === 'false') {
options.nodeVersion = false;
} else if (!semver.validRange(options.nodeVersion)) {
console.error('The `node-engine` option must be a valid semver range (for example `>=6`)');
process.exit(1);
}
}

if (opts.init) {
if (options.init) {
require('xo-init')();
} else if (opts.stdin) {
getStdin().then(str => {
if (opts.fix) {
console.log(xo.lintText(str, opts).results[0].output);
} else if (options.stdin) {
getStdin().then(stdin => {
if (options.fix) {
console.log(xo.lintText(stdin, options).results[0].output);
return;
}

if (opts.open) {
if (options.open) {
console.error('The `open` option is not supported on stdin');
process.exit(1);
}

log(xo.lintText(str, opts));
log(xo.lintText(stdin, options));
});
} else {
xo.lintFiles(input, opts).then(report => {
if (opts.fix) {
xo.lintFiles(input, options).then(report => {
if (options.fix) {
xo.outputFixes(report);
}

if (opts.open) {
if (options.open) {
openReport(report);
}

Expand Down
1 change: 1 addition & 0 deletions config/overrides.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

module.exports = {
// Put rule overrides here
};
80 changes: 65 additions & 15 deletions config/plugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

module.exports = {
// Repeated here from eslint-config-xo in case some plugins set something different
parserOptions: {
Expand All @@ -8,7 +9,7 @@ module.exports = {
jsx: true
}
},
// -- end repeat
// -- End repeat
plugins: [
'no-use-extend-native',
'ava',
Expand All @@ -30,20 +31,37 @@ module.exports = {
rules: {
'no-use-extend-native/no-use-extend-native': 'error',
'promise/param-names': 'error',
'promise/no-return-wrap': ['error', {allowReject: true}],
'promise/no-return-wrap': [
'error',
{
allowReject: true
}
],
'promise/no-new-statics': 'error',
'promise/no-return-in-finally': 'error',
'promise/valid-params': 'error',

// TODO: Enable this when XO targets Node.js 8
// 'promise/prefer-await-to-then': 'error',

'import/default': 'error',
'import/export': 'error',
'import/extensions': ['error', {
js: 'never',
json: 'never',
jsx: 'never'
}],
'import/extensions': [
'error',
{
js: 'never',
json: 'never',
jsx: 'never'
}
],
'import/first': 'error',
'import/named': 'error',
'import/namespace': ['error', {allowComputed: true}],
'import/namespace': [
'error',
{
allowComputed: true
}
],
'import/no-absolute-path': 'error',
'import/no-webpack-loader-syntax': 'error',
'import/no-self-import': 'error',
Expand All @@ -69,11 +87,24 @@ module.exports = {
'import/no-mutable-exports': 'error',
'import/no-named-as-default-member': 'error',
'import/no-named-as-default': 'error',
'import/no-unresolved': ['error', {commonjs: true}],
'import/no-unresolved': [
'error',
{
commonjs: true
}
],
'import/order': 'error',
'import/no-unassigned-import': ['error', {
allow: ['babel-polyfill', '@babel/polyfill', 'babel-register', '@babel/register']
}],
'import/no-unassigned-import': [
'error',
{
allow: [
'babel-polyfill',
'@babel/polyfill',
'babel-register',
'@babel/register'
]
}
],

// Redundant with import/no-extraneous-dependencies
// 'node/no-extraneous-import': 'error',
Expand All @@ -87,8 +118,24 @@ module.exports = {

// Disabled because they're too annoying, see:
// https://github.com/mysticatea/eslint-plugin-node/issues/105
// 'node/no-unpublished-import': ['error', {allowModules: ['electron', 'atom']}],
// 'node/no-unpublished-require': ['error', {allowModules: ['electron', 'atom']}],
// 'node/no-unpublished-import': [
// 'error',
// {
// allowModules: [
// 'electron',
// 'atom'
// ]
// }
// ],
// 'node/no-unpublished-require': [
// 'error',
// {
// allowModules: [
// 'electron',
// 'atom'
// ]
// }
// ],

// Disabled as the rule doesn't allow to exclude compiled sources
// 'node/no-unsupported-features': 'error',
Expand All @@ -101,7 +148,10 @@ module.exports = {
'node/no-deprecated-api': 'error',

// Disabled because it causes too much churn and will be moot when we switch to ES2015 modules
// 'node/exports-style': ['error', 'module.exports']
// 'node/exports-style': [
// 'error',
// 'module.exports'
// ]

'node/prefer-global/buffer': [
'error',
Expand Down
71 changes: 38 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,45 @@ const mergeReports = reports => {
};
};

const processReport = (report, opts) => {
report.results = opts.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
const processReport = (report, options) => {
report.results = options.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
return report;
};

const runEslint = (paths, opts) => {
const config = optionsManager.buildConfig(opts);
const runEslint = (paths, options) => {
const config = optionsManager.buildConfig(options);
const engine = new eslint.CLIEngine(config);
const report = engine.executeOnFiles(paths, config);

return processReport(report, opts);
return processReport(report, options);
};

module.exports.lintText = (str, opts) => {
opts = optionsManager.preprocess(opts);
module.exports.lintText = (str, options) => {
options = optionsManager.preprocess(options);

if (opts.overrides && opts.overrides.length > 0) {
const {overrides} = opts;
delete opts.overrides;
if (options.overrides && options.overrides.length > 0) {
const {overrides} = options;
delete options.overrides;

const filename = path.relative(opts.cwd, opts.filename);
const filename = path.relative(options.cwd, options.filename);

const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
options = optionsManager.mergeApplicableOverrides(options, foundOverrides.applicable);
}

opts = optionsManager.buildConfig(opts);
options = optionsManager.buildConfig(options);
const defaultIgnores = optionsManager.getIgnores({}).ignores;

if (opts.ignores && !isEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
if (options.ignores && !isEqual(defaultIgnores, options.ignores) && typeof options.filename !== 'string') {
throw new Error('The `ignores` option requires the `filename` option to be defined.');
}

if (opts.filename) {
const filename = path.relative(opts.cwd, opts.filename);
if (options.filename) {
const filename = path.relative(options.cwd, options.filename);

if (multimatch(filename, opts.ignores).length > 0 ||
globby.gitignore.sync({cwd: opts.cwd, ignore: opts.ignores})(opts.filename)) {
if (
multimatch(filename, options.ignores).length > 0 ||
globby.gitignore.sync({cwd: options.cwd, ignore: options.ignores})(options.filename)
) {
return {
errorCount: 0,
warningCount: 0,
Expand All @@ -77,41 +78,45 @@ module.exports.lintText = (str, opts) => {
}
}

const engine = new eslint.CLIEngine(opts);
const report = engine.executeOnText(str, opts.filename);
const engine = new eslint.CLIEngine(options);
const report = engine.executeOnText(str, options.filename);

return processReport(report, opts);
return processReport(report, options);
};

module.exports.lintFiles = (patterns, opts) => {
opts = optionsManager.preprocess(opts);
module.exports.lintFiles = (patterns, options) => {
options = optionsManager.preprocess(options);

const isEmptyPatterns = patterns.length === 0;
const defaultPattern = `**/*.{${opts.extensions.join(',')}}`;
const defaultPattern = `**/*.{${options.extensions.join(',')}}`;

return globby(
isEmptyPatterns ? [defaultPattern] : arrify(patterns),
{ignore: opts.ignores, gitignore: true, cwd: opts.cwd}
{
ignore: options.ignores,
gitignore: true,
cwd: options.cwd
}
).then(paths => {
// Filter out unwanted file extensions
// For silly users that don't specify an extension in the glob pattern
if (!isEmptyPatterns) {
paths = paths.filter(filePath => {
const ext = path.extname(filePath).replace('.', '');
return opts.extensions.includes(ext);
return options.extensions.includes(ext);
});
}

if (!(opts.overrides && opts.overrides.length > 0)) {
return runEslint(paths, opts);
if (!(options.overrides && options.overrides.length > 0)) {
return runEslint(paths, options);
}

const {overrides} = opts;
delete opts.overrides;
const {overrides} = options;
delete options.overrides;

const grouped = optionsManager.groupConfigs(paths, opts, overrides);
const grouped = optionsManager.groupConfigs(paths, options, overrides);

return mergeReports(grouped.map(data => runEslint(data.paths, data.opts)));
return mergeReports(grouped.map(data => runEslint(data.paths, data.options)));
});
};

Expand Down
Loading

0 comments on commit 92f2870

Please sign in to comment.