Skip to content

Commit 4c797d8

Browse files
committed
ES2015ify
Two less dependencies \o/
1 parent 040d8be commit 4c797d8

File tree

9 files changed

+139
-152
lines changed

9 files changed

+139
-152
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
* text=auto
2+
*.js text eol=lf
23
*.ai binary

cli.js

Lines changed: 61 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
#!/usr/bin/env node
22
/* eslint-disable import/order */
33
'use strict';
4-
var debug = require('debug')('xo');
4+
const debug = require('debug')('xo');
55

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

10-
var localCLI = resolveCwd('xo/cli');
10+
const localCLI = resolveCwd('xo/cli');
1111

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

18-
var path = require('path');
19-
var spawn = require('child_process').spawn;
20-
var updateNotifier = require('update-notifier');
21-
var getStdin = require('get-stdin');
22-
var meow = require('meow');
23-
var formatterPretty = require('eslint-formatter-pretty');
24-
var xo = require('./');
25-
26-
var cli = meow({
27-
help: [
28-
'Usage',
29-
' $ xo [<file|glob> ...]',
30-
'',
31-
'Options',
32-
' --init Add XO to your project',
33-
' --fix Automagically fix issues',
34-
' --reporter Reporter to use',
35-
' --stdin Validate code from stdin',
36-
' --esnext Enforce ES2015+ rules',
37-
' --env Environment preset [Can be set multiple times]',
38-
' --global Global variable [Can be set multiple times]',
39-
' --ignore Additional paths to ignore [Can be set multiple times]',
40-
' --space Use space indent instead of tabs [Default: 2]',
41-
' --no-semicolon Prevent use of semicolons',
42-
' --plugin Include third-party plugins [Can be set multiple times]',
43-
' --extend Extend defaults with a custom config [Can be set multiple times]',
44-
' --open Open files with issues in your editor',
45-
' --quiet Show only errors and no warnings',
46-
'',
47-
'Examples',
48-
' $ xo',
49-
' $ xo index.js',
50-
' $ xo *.js !foo.js',
51-
' $ xo --esnext --space',
52-
' $ xo --env=node --env=mocha',
53-
' $ xo --init --esnext',
54-
' $ xo --plugin=react',
55-
'',
56-
'Tips',
57-
' Put options in package.json instead of using flags so other tools can read it.'
58-
]
59-
}, {
18+
const path = require('path');
19+
const spawn = require('child_process').spawn;
20+
const updateNotifier = require('update-notifier');
21+
const getStdin = require('get-stdin');
22+
const meow = require('meow');
23+
const formatterPretty = require('eslint-formatter-pretty');
24+
const xo = require('./');
25+
26+
const cli = meow(`
27+
Usage
28+
$ xo [<file|glob> ...]
29+
30+
Options
31+
--init Add XO to your project
32+
--fix Automagically fix issues
33+
--reporter Reporter to use
34+
--stdin Validate code from stdin
35+
--esnext Enforce ES2015+ rules
36+
--env Environment preset [Can be set multiple times]
37+
--global Global variable [Can be set multiple times]
38+
--ignore Additional paths to ignore [Can be set multiple times]
39+
--space Use space indent instead of tabs [Default: 2]
40+
--no-semicolon Prevent use of semicolons
41+
--plugin Include third-party plugins [Can be set multiple times]
42+
--extend Extend defaults with a custom config [Can be set multiple times]
43+
--open Open files with issues in your editor
44+
--quiet Show only errors and no warnings
45+
46+
Examples
47+
$ xo
48+
$ xo index.js
49+
$ xo *.js !foo.js
50+
$ xo --esnext --space
51+
$ xo --env=node --env=mocha
52+
$ xo --init --esnext
53+
$ xo --plugin=react
54+
55+
Tips
56+
Put options in package.json instead of using flags so other tools can read it.
57+
`, {
6058
string: [
6159
'_'
6260
],
@@ -71,8 +69,8 @@ var cli = meow({
7169

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

74-
var input = cli.input;
75-
var opts = cli.flags;
72+
const input = cli.input;
73+
const opts = cli.flags;
7674

7775
function log(report) {
7876
// legacy
@@ -81,7 +79,7 @@ function log(report) {
8179
opts.reporter = 'compact';
8280
}
8381

84-
var reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty;
82+
const reporter = opts.reporter ? xo.getFormatter(opts.reporter) : formatterPretty;
8583

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

95-
var editor = process.env.EDITOR;
93+
const editor = process.env.EDITOR;
9694

9795
if (!editor) {
98-
console.log([
99-
'',
100-
'`open` option was used, but your $EDITOR environment variable is empty.',
101-
'Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:',
102-
'',
103-
' export EDITOR=atom',
104-
''
105-
].join('\n'));
96+
console.log(`
97+
\`open\` option was used, but your $EDITOR environment variable is empty.
98+
Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:
99+
100+
export EDITOR=atom
101+
`);
106102
return;
107103
}
108104

109-
var executableName = editor.split(path.sep).pop();
105+
const executableName = editor.split(path.sep).pop();
110106

111107
function lineColumn(message) {
112-
return message.line + ':' + message.column;
108+
return `${message.line}:${message.column}`;
113109
}
114110

115-
var args = [];
111+
const args = [];
116112

117113
report.results
118-
.filter(function (file) {
119-
return file.errorCount > 0;
120-
})
121-
.forEach(function (file) {
114+
.filter(file => file.errorCount > 0)
115+
.forEach(file => {
122116
// Sublime Text and Atom support opening file at exact position
123117
if (['subl', 'atom'].indexOf(executableName) >= 0) {
124118
args.push(file.filePath + ':' + lineColumn(file.messages[0]));
@@ -155,7 +149,7 @@ if (input[0] === '-') {
155149
if (opts.init) {
156150
require('xo-init')();
157151
} else if (opts.stdin) {
158-
getStdin().then(function (str) {
152+
getStdin().then(str => {
159153
if (opts.fix) {
160154
console.error('The `fix` option is not supported on stdin');
161155
process.exit(1);
@@ -169,7 +163,7 @@ if (opts.init) {
169163
log(xo.lintText(str, opts));
170164
});
171165
} else {
172-
xo.lintFiles(input, opts).then(function (report) {
166+
xo.lintFiles(input, opts).then(report => {
173167
if (opts.fix) {
174168
xo.outputFixes(report);
175169
}

index.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,79 @@
11
'use strict';
2-
var path = require('path');
3-
var eslint = require('eslint');
4-
var globby = require('globby');
5-
var optionsManager = require('./options-manager');
2+
const path = require('path');
3+
const eslint = require('eslint');
4+
const globby = require('globby');
5+
const optionsManager = require('./options-manager');
66

7-
exports.lintText = function (str, opts) {
7+
exports.lintText = (str, opts) => {
88
opts = optionsManager.preprocess(opts);
99

1010
if (opts.overrides && opts.overrides.length) {
11-
var overrides = opts.overrides;
11+
const overrides = opts.overrides;
1212
delete opts.overrides;
1313

14-
var filename = path.relative(opts.cwd, opts.filename);
15-
var foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
14+
const filename = path.relative(opts.cwd, opts.filename);
15+
const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
1616
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
1717
}
1818

1919
opts = optionsManager.buildConfig(opts);
2020

21-
var engine = new eslint.CLIEngine(opts);
22-
var report = engine.executeOnText(str, opts.filename);
21+
const engine = new eslint.CLIEngine(opts);
22+
const report = engine.executeOnText(str, opts.filename);
2323

2424
return processReport(report, opts);
2525
};
2626

27-
exports.lintFiles = function (patterns, opts) {
27+
exports.lintFiles = (patterns, opts) => {
2828
opts = optionsManager.preprocess(opts);
2929

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

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

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

45-
var overrides = opts.overrides;
45+
const overrides = opts.overrides;
4646
delete opts.overrides;
4747

48-
var grouped = optionsManager.groupConfigs(paths, opts, overrides);
48+
const grouped = optionsManager.groupConfigs(paths, opts, overrides);
4949

50-
return mergeReports(grouped.map(function (data) {
51-
return runEslint(data.paths, data.opts);
52-
}));
50+
return mergeReports(grouped.map(data => runEslint(data.paths, data.opts)));
5351
});
5452
};
5553

5654
function mergeReports(reports) {
5755
// merge multiple reports into a single report
58-
var results = [];
59-
var errorCount = 0;
60-
var warningCount = 0;
56+
let results = [];
57+
let errorCount = 0;
58+
let warningCount = 0;
6159

62-
reports.forEach(function (report) {
60+
reports.forEach(report => {
6361
results = results.concat(report.results);
6462
errorCount += report.errorCount;
6563
warningCount += report.warningCount;
6664
});
6765

6866
return {
69-
errorCount: errorCount,
70-
warningCount: warningCount,
71-
results: results
67+
errorCount,
68+
warningCount,
69+
results
7270
};
7371
}
7472

7573
function runEslint(paths, opts) {
76-
var config = optionsManager.buildConfig(opts);
77-
var engine = new eslint.CLIEngine(config);
78-
var report = engine.executeOnFiles(paths, config);
74+
const config = optionsManager.buildConfig(opts);
75+
const engine = new eslint.CLIEngine(config);
76+
const report = engine.executeOnFiles(paths, config);
7977

8078
return processReport(report, opts);
8179
}

0 commit comments

Comments
 (0)