Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: xojs/xo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a64ffc44aeb61224f4dd9820d76c82d06d82b981
Choose a base ref
...
head repository: xojs/xo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 41f0484a337bb53d3075194223a1d69eae6c05b2
Choose a head ref
Loading
Showing with 2,465 additions and 1,004 deletions.
  1. +26 −0 .github/workflows/main.yml
  2. +0 −10 .travis.yml
  3. +79 −42 cli-main.js
  4. +0 −1 cli.js
  5. +227 −56 config/plugins.js
  6. +139 −84 index.js
  7. +153 −0 lib/constants.js
  8. +4 −6 lib/open-report.js
  9. +462 −227 lib/options-manager.js
  10. +1 −1 license
  11. +82 −50 package.json
  12. +137 −105 readme.md
  13. +58 −28 test/cli-main.js
  14. +3 −0 test/fixtures/config-files/xo-config/.xo-config
  15. +5 −0 test/fixtures/config-files/xo-config/file.js
  16. +3 −0 test/fixtures/config-files/xo-config_cjs/.xo-config.cjs
  17. +5 −0 test/fixtures/config-files/xo-config_cjs/file.js
  18. +3 −0 test/fixtures/config-files/xo-config_js/.xo-config.js
  19. +5 −0 test/fixtures/config-files/xo-config_js/file.js
  20. +3 −0 test/fixtures/config-files/xo-config_json/.xo-config.json
  21. +5 −0 test/fixtures/config-files/xo-config_json/file.js
  22. +5 −0 test/fixtures/config-files/xo_config_cjs/file.js
  23. +3 −0 test/fixtures/config-files/xo_config_cjs/xo.config.cjs
  24. +5 −0 test/fixtures/config-files/xo_config_js/file.js
  25. +3 −0 test/fixtures/config-files/xo_config_js/xo.config.js
  26. +3 −0 test/fixtures/custom-extension/one-space.unknown
  27. +1 −0 test/fixtures/eslintignore/.eslintignore
  28. +1 −0 test/fixtures/eslintignore/bar.js
  29. +1 −0 test/fixtures/eslintignore/foo.js
  30. +2 −2 test/fixtures/gitignore/test/bar.js
  31. +1 −1 test/fixtures/gitignore/test/foo.js
  32. 0 test/fixtures/ignores/{test/fixtures → dist}/linter-error.js
  33. +11 −7 test/fixtures/ignores/package.json
  34. +0 −5 test/fixtures/ignores/test/foo.js
  35. +3 −0 test/fixtures/nested-configs/child-override/.prettierrc
  36. +10 −0 test/fixtures/nested-configs/child-override/child-prettier-override/package.json
  37. +1 −0 test/fixtures/nested-configs/child-override/child-prettier-override/semicolon.js
  38. +10 −0 test/fixtures/nested-configs/child-override/package.json
  39. +3 −0 test/fixtures/nested-configs/child-override/two-spaces.js
  40. +5 −0 test/fixtures/nested-configs/child/package.json
  41. +1 −0 test/fixtures/nested-configs/child/semicolon.js
  42. +1 −0 test/fixtures/nested-configs/no-semicolon.js
  43. +5 −0 test/fixtures/nested-configs/package.json
  44. +1 −1 test/fixtures/nested/child-empty/package.json
  45. +1 −3 test/fixtures/nested/child-ignore/package.json
  46. +3 −3 test/fixtures/nested/child/package.json
  47. +2 −2 test/fixtures/nested/file.js
  48. +3 −3 test/fixtures/nested/package.json
  49. +20 −19 test/fixtures/overrides/package.json
  50. +2 −2 test/fixtures/overrides/test/bar.js
  51. +2 −2 test/fixtures/overrides/test/foo.js
  52. +2 −2 test/fixtures/project/file.js
  53. +2 −2 test/fixtures/project/node_modules/eslint-config-custom/package.json
  54. +3 −3 test/fixtures/project/package.json
  55. +1 −0 test/fixtures/typescript/child/extra-semicolon.ts
  56. +1 −0 test/fixtures/typescript/child/no-semicolon.ts
  57. +5 −0 test/fixtures/typescript/child/package.json
  58. +3 −0 test/fixtures/typescript/child/sub-child/four-spaces.ts
  59. +5 −0 test/fixtures/typescript/child/sub-child/package.json
  60. +6 −0 test/fixtures/typescript/child/tsconfig.json
  61. +5 −0 test/fixtures/typescript/package.json
  62. +3 −0 test/fixtures/typescript/two-spaces.tsx
  63. +2 −0 test/fixtures/webpack/no-config/file1.js
  64. +2 −0 test/fixtures/webpack/no-config/file2.js
  65. +1 −0 test/fixtures/webpack/no-config/file3.js
  66. +2 −0 test/fixtures/webpack/with-config/file1.js
  67. +2 −0 test/fixtures/webpack/with-config/file2.js
  68. +9 −0 test/fixtures/webpack/with-config/webpack.config.js
  69. +176 −13 test/lint-files.js
  70. +176 −83 test/lint-text.js
  71. +14 −8 test/open-report.js
  72. +510 −233 test/options-manager.js
  73. +26 −0 test/print-config.js
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.node-version == 14
with:
fail_ci_if_error: true
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

121 changes: 79 additions & 42 deletions cli-main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env node
'use strict';
const updateNotifier = require('update-notifier');
const getStdin = require('get-stdin');
const meow = require('meow');
const formatterPretty = require('eslint-formatter-pretty');
const semver = require('semver');
const openReport = require('./lib/open-report');
const xo = require('.');
const openReport = require('./lib/open-report.js');
const xo = require('./index.js');

const cli = meow(`
Usage
$ xo [<file|glob> ...]
Options
--init Add XO to your project
--fix Automagically fix issues
--reporter Reporter to use
--env Environment preset [Can be set multiple times]
@@ -28,44 +26,46 @@ const cli = meow(`
--open Open files with issues in your editor
--quiet Show only errors and no warnings
--extension Additional extension to lint [Can be set multiple times]
--no-esnext Don't enforce ES2015+ rules
--cwd=<dir> Working directory for files
--stdin Validate/fix code from stdin
--stdin-filename Specify a filename for the --stdin option
--print-config Print the effective ESLint config for the given file
Examples
$ xo
$ xo index.js
$ xo *.js !foo.js
$ xo --space
$ xo --env=node --env=mocha
$ xo --init --space
$ xo --plugin=react
$ xo --plugin=html --extension=html
$ echo 'const x=true' | xo --stdin --fix
$ xo --print-config=index.js
Tips
Put options in package.json instead of using flags so other tools can read it.
- Add XO to your project with \`npm init xo\`.
- Put options in package.json instead of using flags so other tools can read it.
`, {
autoVersion: false,
booleanDefault: undefined,
flags: {
init: {
type: 'boolean'
},
fix: {
type: 'boolean'
},
reporter: {
type: 'string'
},
env: {
type: 'string'
type: 'string',
isMultiple: true
},
global: {
type: 'string'
type: 'string',
isMultiple: true
},
ignore: {
type: 'string'
type: 'string',
isMultiple: true
},
space: {
type: 'string'
@@ -80,10 +80,12 @@ const cli = meow(`
type: 'string'
},
plugin: {
type: 'string'
type: 'string',
isMultiple: true
},
extend: {
type: 'string'
type: 'string',
isMultiple: true
},
open: {
type: 'boolean'
@@ -92,33 +94,40 @@ const cli = meow(`
type: 'boolean'
},
extension: {
type: 'string'
},
esnext: {
type: 'boolean'
type: 'string',
isMultiple: true
},
cwd: {
type: 'string'
},
printConfig: {
type: 'string'
},
stdin: {
type: 'boolean'
},
stdinFilename: {
type: 'string',
alias: 'filename'
type: 'string'
}
}
});

updateNotifier({pkg: cli.pkg}).notify();
const {input, flags: options, showVersion} = cli;

const {input, flags: options} = cli;
// TODO: Fix this properly instead of the below workaround.
// Revert behavior of meow >8 to pre-8 (7.1.1) for flags using `isMultiple: true`.
// Otherwise, options defined in package.json can't be merged by lib/options-manager.js `mergeOptions()`.
for (const key in options) {
if (Array.isArray(options[key]) && options[key].length === 0) {
delete options[key];
}
}

// 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);
options.space = Number.parseInt(options.space, 10);
} else if (options.space === 'true') {
options.space = true;
} else if (options.space === 'false') {
@@ -133,10 +142,14 @@ if (typeof options.space === 'string') {
}
}

const log = report => {
const reporter = options.reporter ? xo.getFormatter(options.reporter) : formatterPretty;
if (process.env.GITHUB_ACTIONS && !options.fix && !options.reporter) {
options.quiet = true;
}

const log = async report => {
const reporter = options.reporter || process.env.GITHUB_ACTIONS ? await xo.getFormatter(options.reporter || 'compact') : formatterPretty;
process.stdout.write(reporter(report.results));
process.exit(report.errorCount === 0 ? 0 : 1);
process.exitCode = report.errorCount === 0 ? 0 : 1;
};

// `xo -` => `xo --stdin`
@@ -145,41 +158,65 @@ if (input[0] === '-') {
input.shift();
}

if (options.version) {
showVersion();
}

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`)');
console.error('The `--node-engine` flag must be a valid semver range (for example `>=6`)');
process.exit(1);
}
}

if (options.init) {
require('xo-init')();
} else if (options.stdin) {
getStdin().then(stdin => {
(async () => {
if (options.printConfig) {
if (input.length > 0) {
console.error('The `--print-config` flag must be used with exactly one filename');
process.exit(1);
}

if (options.stdin) {
console.error('The `--print-config` flag is not supported on stdin');
process.exit(1);
}

options.filePath = options.printConfig;
const config = await xo.getConfig(options);
console.log(JSON.stringify(config, undefined, '\t'));
} else if (options.stdin) {
const stdin = await getStdin();

if (options.stdinFilename) {
options.filePath = options.stdinFilename;
}

if (options.fix) {
console.log(xo.lintText(stdin, options).results[0].output);
const {results: [result]} = await xo.lintText(stdin, options);
// If there is no output, pass the stdin back out
process.stdout.write((result && result.output) || stdin);
return;
}

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

log(xo.lintText(stdin, options));
});
} else {
xo.lintFiles(input, options).then(report => {
await log(await xo.lintText(stdin, options));
} else {
const report = await xo.lintFiles(input, options);

if (options.fix) {
xo.outputFixes(report);
await xo.outputFixes(report);
}

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

log(report);
});
}
await log(report);
}
})();
1 change: 0 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
/* eslint-disable import/no-unassigned-import */
'use strict';
const resolveCwd = require('resolve-cwd');
const hasFlag = require('has-flag');
Loading