Skip to content

Commit

Permalink
ES2015ify
Browse files Browse the repository at this point in the history
Two less dependencies \o/
  • Loading branch information
sindresorhus committed Sep 26, 2016
1 parent 040d8be commit 4c797d8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -1,2 +1,3 @@
* text=auto
*.js text eol=lf
*.ai binary
128 changes: 61 additions & 67 deletions cli.js
@@ -1,62 +1,60 @@
#!/usr/bin/env node
/* eslint-disable import/order */
'use strict';
var debug = require('debug')('xo');
const debug = require('debug')('xo');

// Prefer the local installation of XO.
var resolveCwd = require('resolve-cwd');
var hasFlag = require('has-flag');
const resolveCwd = require('resolve-cwd');
const hasFlag = require('has-flag');

var localCLI = resolveCwd('xo/cli');
const localCLI = resolveCwd('xo/cli');

if (!hasFlag('no-local') && localCLI && localCLI !== __filename) {
debug('Using local install of XO.');
require(localCLI);
return;
}

var path = require('path');
var spawn = require('child_process').spawn;
var updateNotifier = require('update-notifier');
var getStdin = require('get-stdin');
var meow = require('meow');
var formatterPretty = require('eslint-formatter-pretty');
var xo = require('./');

var cli = meow({
help: [
'Usage',
' $ xo [<file|glob> ...]',
'',
'Options',
' --init Add XO to your project',
' --fix Automagically fix issues',
' --reporter Reporter to use',
' --stdin Validate code from stdin',
' --esnext Enforce ES2015+ rules',
' --env Environment preset [Can be set multiple times]',
' --global Global variable [Can be set multiple times]',
' --ignore Additional paths to ignore [Can be set multiple times]',
' --space Use space indent instead of tabs [Default: 2]',
' --no-semicolon Prevent use of semicolons',
' --plugin Include third-party plugins [Can be set multiple times]',
' --extend Extend defaults with a custom config [Can be set multiple times]',
' --open Open files with issues in your editor',
' --quiet Show only errors and no warnings',
'',
'Examples',
' $ xo',
' $ xo index.js',
' $ xo *.js !foo.js',
' $ xo --esnext --space',
' $ xo --env=node --env=mocha',
' $ xo --init --esnext',
' $ xo --plugin=react',
'',
'Tips',
' Put options in package.json instead of using flags so other tools can read it.'
]
}, {
const path = require('path');
const spawn = require('child_process').spawn;
const updateNotifier = require('update-notifier');
const getStdin = require('get-stdin');
const meow = require('meow');
const formatterPretty = require('eslint-formatter-pretty');
const xo = require('./');

const cli = meow(`
Usage
$ xo [<file|glob> ...]
Options
--init Add XO to your project
--fix Automagically fix issues
--reporter Reporter to use
--stdin Validate code from stdin
--esnext Enforce ES2015+ rules
--env Environment preset [Can be set multiple times]
--global Global variable [Can be set multiple times]
--ignore Additional paths to ignore [Can be set multiple times]
--space Use space indent instead of tabs [Default: 2]
--no-semicolon Prevent use of semicolons
--plugin Include third-party plugins [Can be set multiple times]
--extend Extend defaults with a custom config [Can be set multiple times]
--open Open files with issues in your editor
--quiet Show only errors and no warnings
Examples
$ xo
$ xo index.js
$ xo *.js !foo.js
$ xo --esnext --space
$ xo --env=node --env=mocha
$ xo --init --esnext
$ xo --plugin=react
Tips
Put options in package.json instead of using flags so other tools can read it.
`, {
string: [
'_'
],
Expand All @@ -71,8 +69,8 @@ var cli = meow({

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

var input = cli.input;
var opts = cli.flags;
const input = cli.input;
const opts = cli.flags;

function log(report) {
// legacy
Expand All @@ -81,7 +79,7 @@ function log(report) {
opts.reporter = 'compact';
}

var reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty;
const reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty;

process.stdout.write(reporter(report.results));
process.exit(report.errorCount === 0 ? 0 : 1);
Expand All @@ -92,33 +90,29 @@ function open(report) {
return;
}

var editor = process.env.EDITOR;
const editor = process.env.EDITOR;

if (!editor) {
console.log([
'',
'`open` option was used, but your $EDITOR environment variable is empty.',
'Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:',
'',
' export EDITOR=atom',
''
].join('\n'));
console.log(`
\`open\` option was used, but your $EDITOR environment variable is empty.
Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:
export EDITOR=atom
`);
return;
}

var executableName = editor.split(path.sep).pop();
const executableName = editor.split(path.sep).pop();

function lineColumn(message) {
return message.line + ':' + message.column;
return `${message.line}:${message.column}`;
}

var args = [];
const args = [];

report.results
.filter(function (file) {
return file.errorCount > 0;
})
.forEach(function (file) {
.filter(file => file.errorCount > 0)
.forEach(file => {
// Sublime Text and Atom support opening file at exact position
if (['subl', 'atom'].indexOf(executableName) >= 0) {
args.push(file.filePath + ':' + lineColumn(file.messages[0]));
Expand Down Expand Up @@ -155,7 +149,7 @@ if (input[0] === '-') {
if (opts.init) {
require('xo-init')();
} else if (opts.stdin) {
getStdin().then(function (str) {
getStdin().then(str => {
if (opts.fix) {
console.error('The `fix` option is not supported on stdin');
process.exit(1);
Expand All @@ -169,7 +163,7 @@ if (opts.init) {
log(xo.lintText(str, opts));
});
} else {
xo.lintFiles(input, opts).then(function (report) {
xo.lintFiles(input, opts).then(report => {
if (opts.fix) {
xo.outputFixes(report);
}
Expand Down
56 changes: 27 additions & 29 deletions index.js
@@ -1,81 +1,79 @@
'use strict';
var path = require('path');
var eslint = require('eslint');
var globby = require('globby');
var optionsManager = require('./options-manager');
const path = require('path');
const eslint = require('eslint');
const globby = require('globby');
const optionsManager = require('./options-manager');

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

if (opts.overrides && opts.overrides.length) {
var overrides = opts.overrides;
const overrides = opts.overrides;
delete opts.overrides;

var filename = path.relative(opts.cwd, opts.filename);
var foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
const filename = path.relative(opts.cwd, opts.filename);
const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
}

opts = optionsManager.buildConfig(opts);

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

return processReport(report, opts);
};

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

if (patterns.length === 0) {
patterns = '**/*.{js,jsx}';
}

return globby(patterns, {ignore: opts.ignores}).then(function (paths) {
return globby(patterns, {ignore: opts.ignores}).then(paths => {
// when users are silly and don't specify an extension in the glob pattern
paths = paths.filter(function (x) {
var ext = path.extname(x);
paths = paths.filter(x => {
const ext = path.extname(x);
return ext === '.js' || ext === '.jsx';
});

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

var overrides = opts.overrides;
const overrides = opts.overrides;
delete opts.overrides;

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

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

function mergeReports(reports) {
// merge multiple reports into a single report
var results = [];
var errorCount = 0;
var warningCount = 0;
let results = [];
let errorCount = 0;
let warningCount = 0;

reports.forEach(function (report) {
reports.forEach(report => {
results = results.concat(report.results);
errorCount += report.errorCount;
warningCount += report.warningCount;
});

return {
errorCount: errorCount,
warningCount: warningCount,
results: results
errorCount,
warningCount,
results
};
}

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

return processReport(report, opts);
}
Expand Down

0 comments on commit 4c797d8

Please sign in to comment.