From 48baacc6ab46a66d88cbdeaa3b0e67f66d5c3c44 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 7 Aug 2021 18:01:43 +0800 Subject: [PATCH] Correctly use `ignores` config, Lint `xo` with `xo` (#584) --- index.js | 3 +- lib/options-manager.js | 7 ++-- package.json | 17 +++----- test/fixtures/nested-ignores/a.js | 1 + test/fixtures/nested-ignores/b.js | 1 + test/fixtures/nested-ignores/child/a.js | 1 + test/fixtures/nested-ignores/child/b.js | 1 + .../nested-ignores/child/package.json | 8 ++++ test/fixtures/nested-ignores/package.json | 8 ++++ test/ignores.js | 40 +++++++++++++++++++ 10 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/nested-ignores/a.js create mode 100644 test/fixtures/nested-ignores/b.js create mode 100644 test/fixtures/nested-ignores/child/a.js create mode 100644 test/fixtures/nested-ignores/child/b.js create mode 100644 test/fixtures/nested-ignores/child/package.json create mode 100644 test/fixtures/nested-ignores/package.json create mode 100644 test/ignores.js diff --git a/index.js b/index.js index d13017e2..e3757bff 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,6 @@ import { getIgnores, mergeWithFileConfig, buildConfig, - mergeOptions, } from './lib/options-manager.js'; /** Merge multiple reports into a single report */ @@ -152,7 +151,7 @@ const lintFiles = async (patterns, inputOptions = {}) => { inputOptions = normalizeOptions(inputOptions); inputOptions.cwd = path.resolve(inputOptions.cwd || process.cwd()); - const files = await globFiles(patterns, mergeOptions(inputOptions)); + const files = await globFiles(patterns, mergeWithFileConfig(inputOptions).options); const reports = await pMap( files, diff --git a/lib/options-manager.js b/lib/options-manager.js index 3590d117..f2b69b42 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -540,12 +540,13 @@ const gatherImportResolvers = options => { }; export { - findApplicableOverrides, - mergeWithPrettierConfig, normalizeOptions, getIgnores, mergeWithFileConfig, buildConfig, + + // For tests applyOverrides, - mergeOptions, + findApplicableOverrides, + mergeWithPrettierConfig, }; diff --git a/package.json b/package.json index 0bfc5558..242ac35a 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "node": ">=12.20" }, "scripts": { - "test": "eslint --quiet . --ext .js,.cjs && nyc ava" + "test": "node cli.js && nyc ava" }, "files": [ "config", @@ -106,18 +106,13 @@ "temp-write": "^5.0.0", "webpack": "^5.48.0" }, - "eslintConfig": { - "extends": [ - "eslint-config-xo", - "./config/plugins.cjs", - "./config/overrides.cjs" + "xo": { + "ignores": [ + "test/fixtures", + "test/temp", + "coverage" ] }, - "eslintIgnore": [ - "test/fixtures", - "test/temp", - "coverage" - ], "ava": { "files": [ "!test/temp" diff --git a/test/fixtures/nested-ignores/a.js b/test/fixtures/nested-ignores/a.js new file mode 100644 index 00000000..b00bfec5 --- /dev/null +++ b/test/fixtures/nested-ignores/a.js @@ -0,0 +1 @@ +console.log('semicolon') diff --git a/test/fixtures/nested-ignores/b.js b/test/fixtures/nested-ignores/b.js new file mode 100644 index 00000000..b00bfec5 --- /dev/null +++ b/test/fixtures/nested-ignores/b.js @@ -0,0 +1 @@ +console.log('semicolon') diff --git a/test/fixtures/nested-ignores/child/a.js b/test/fixtures/nested-ignores/child/a.js new file mode 100644 index 00000000..b00bfec5 --- /dev/null +++ b/test/fixtures/nested-ignores/child/a.js @@ -0,0 +1 @@ +console.log('semicolon') diff --git a/test/fixtures/nested-ignores/child/b.js b/test/fixtures/nested-ignores/child/b.js new file mode 100644 index 00000000..b00bfec5 --- /dev/null +++ b/test/fixtures/nested-ignores/child/b.js @@ -0,0 +1 @@ +console.log('semicolon') diff --git a/test/fixtures/nested-ignores/child/package.json b/test/fixtures/nested-ignores/child/package.json new file mode 100644 index 00000000..f0a313fe --- /dev/null +++ b/test/fixtures/nested-ignores/child/package.json @@ -0,0 +1,8 @@ +{ + "xo": { + "ignores": [ + "b.js", + "**/b.js" + ] + } +} diff --git a/test/fixtures/nested-ignores/package.json b/test/fixtures/nested-ignores/package.json new file mode 100644 index 00000000..f852068c --- /dev/null +++ b/test/fixtures/nested-ignores/package.json @@ -0,0 +1,8 @@ +{ + "xo": { + "ignores": [ + "a.js", + "**/a.js" + ] + } +} diff --git a/test/ignores.js b/test/ignores.js new file mode 100644 index 00000000..d6f98bb9 --- /dev/null +++ b/test/ignores.js @@ -0,0 +1,40 @@ +import path from 'node:path'; +import test from 'ava'; +import createEsmUtils from 'esm-utils'; +import {globby} from 'globby'; +import xo from '../index.js'; + +const {__dirname} = createEsmUtils(import.meta); + +test('Should pickup "ignores" config', async t => { + const cwd = path.join(__dirname, 'fixtures/nested-ignores'); + + t.deepEqual( + await globby(['**/*.js'], {cwd}), + ['a.js', 'b.js', 'child/a.js', 'child/b.js'], + 'Should has 4 js files.', + ); + + // Should not match + // `a.js` (ignored by config in current directory) + // `child/a.js` (ignored by config in current directory) + // `child/b.js` (ignored by config in child directory) + const result = await xo.lintFiles('.', {cwd}); + const files = result.results.map(({filePath}) => filePath); + t.deepEqual(files, [path.join(cwd, 'b.js')], 'Should only report on `b.js`.'); +}); + +test('Should ignore "ignores" config in parent', async t => { + const cwd = path.join(__dirname, 'fixtures/nested-ignores/child'); + + t.deepEqual( + await globby(['**/*.js'], {cwd}), + ['a.js', 'b.js'], + 'Should has 2 js files.', + ); + + // Should only match `a.js` even it's ignored in parent + const result = await xo.lintFiles('.', {cwd}); + const files = result.results.map(({filePath}) => filePath); + t.deepEqual(files, [path.join(cwd, 'a.js')], 'Should only report on `a.js`.'); +});