Skip to content

Commit d8e05d8

Browse files
nzakasmdjermanovic
andauthored
feat: Add globalIgnores helper function (#159)
Co-authored-by: Milos Djermanovic <[email protected]>
1 parent dd5b3b5 commit d8e05d8

File tree

7 files changed

+158
-20
lines changed

7 files changed

+158
-20
lines changed

packages/config-helpers/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ export default defineConfig([
5454
]);
5555
```
5656

57+
### `globalIgnores()`
58+
59+
The `globalIgnores()` function allows you to specify patterns for files and directories that should be globally ignored by ESLint. This is useful for excluding files that you don't want to lint, such as build directories or third-party libraries. Here's an example:
60+
61+
```js
62+
// eslint.config.js
63+
import { defineConfig, globalIgnores } from "@eslint/config-helpers";
64+
65+
export default defineConfig([
66+
{
67+
files: ["src/**/*.js"],
68+
rules: {
69+
semi: "error",
70+
"prefer-const": "error",
71+
},
72+
},
73+
globalIgnores(["node_modules/", "dist/", "coverage/"]),
74+
]);
75+
```
76+
5777
## License
5878

5979
Apache 2.0

packages/config-helpers/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
"test": "tests"
2626
},
2727
"scripts": {
28+
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
2829
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
29-
"build": "rollup -c && tsc -p tsconfig.esm.json && npm run build:cts",
30+
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
3031
"test:jsr": "npx jsr@latest publish --dry-run",
3132
"test": "mocha tests/*.js",
3233
"test:coverage": "c8 npm test",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @fileoverview Global ignores helper function.
3+
* @author Nicholas C. Zakas
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Type Definitions
8+
//-----------------------------------------------------------------------------
9+
10+
/** @typedef {import("eslint").Linter.Config} Config */
11+
12+
//-----------------------------------------------------------------------------
13+
// Helpers
14+
//-----------------------------------------------------------------------------
15+
16+
let globalIgnoreCount = 0;
17+
18+
//-----------------------------------------------------------------------------
19+
// Exports
20+
//-----------------------------------------------------------------------------
21+
22+
/**
23+
* Creates a global ignores config with the given patterns.
24+
* @param {string[]} ignorePatterns The ignore patterns.
25+
* @param {string} [name] The name of the global ignores config.
26+
* @returns {Config} The global ignores config.
27+
*/
28+
export function globalIgnores(ignorePatterns, name) {
29+
if (!Array.isArray(ignorePatterns)) {
30+
throw new TypeError("ignorePatterns must be an array");
31+
}
32+
33+
if (ignorePatterns.length === 0) {
34+
throw new TypeError("ignorePatterns must contain at least one pattern");
35+
}
36+
37+
const id = globalIgnoreCount++;
38+
39+
return {
40+
name: name || `globalIgnores ${id}`,
41+
ignores: ignorePatterns,
42+
};
43+
}

packages/config-helpers/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
* @fileoverview Main entrypoint for the package.
33
*/
44

5-
export * from "./define-config.js";
5+
export { defineConfig } from "./define-config.js";
6+
export { globalIgnores } from "./global-ignores.js";
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @fileoverview Tests for globalIgnores()
3+
* @author Nicholas C. Zakas
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
10+
import assert from "node:assert";
11+
import { globalIgnores } from "../src/global-ignores.js";
12+
13+
//-----------------------------------------------------------------------------
14+
// Tests
15+
//-----------------------------------------------------------------------------
16+
17+
describe("globalIgnores", () => {
18+
it("should create config with custom name", () => {
19+
const result = globalIgnores(["*.test.js"], "custom-name");
20+
assert.deepStrictEqual(result, {
21+
name: "custom-name",
22+
ignores: ["*.test.js"],
23+
});
24+
});
25+
26+
it("should create config with auto-generated name", () => {
27+
const result = globalIgnores(["*.test.js"]);
28+
assert.strictEqual(result.name.startsWith("globalIgnores "), true);
29+
assert.deepStrictEqual(result.ignores, ["*.test.js"]);
30+
});
31+
32+
it("should increment auto-generated names", () => {
33+
const result1 = globalIgnores(["*.test.js"]);
34+
const result2 = globalIgnores(["*.spec.js"]);
35+
assert.notStrictEqual(result1.name, result2.name);
36+
});
37+
38+
it("should throw error for non-array input", () => {
39+
assert.throws(
40+
() => {
41+
globalIgnores("*.test.js");
42+
},
43+
{
44+
name: "TypeError",
45+
message: "ignorePatterns must be an array",
46+
},
47+
);
48+
});
49+
50+
it("should throw error for empty array", () => {
51+
assert.throws(
52+
() => {
53+
globalIgnores([]);
54+
},
55+
{
56+
name: "TypeError",
57+
message: "ignorePatterns must contain at least one pattern",
58+
},
59+
);
60+
});
61+
});

packages/config-helpers/tests/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ describe("index", () => {
1717
it("should export defineConfig()", () => {
1818
assert.strictEqual(typeof api.defineConfig, "function");
1919
});
20+
21+
it("should export globalIgnores()", () => {
22+
assert.strictEqual(typeof api.globalIgnores, "function");
23+
});
2024
});

packages/config-helpers/tests/types/types.test.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Imports
88
//-----------------------------------------------------------------------------
99

10-
import { defineConfig } from "@eslint/config-helpers";
10+
import { defineConfig, globalIgnores } from "@eslint/config-helpers";
1111

1212
//-----------------------------------------------------------------------------
1313
// Type Checking
@@ -17,7 +17,7 @@ defineConfig({});
1717
defineConfig({}, {});
1818
defineConfig([]);
1919
defineConfig([], {});
20-
defineConfig([], []);
20+
defineConfig([globalIgnores(["node_modules"])], []);
2121
defineConfig([{}]);
2222
defineConfig({
2323
extends: [],
@@ -43,23 +43,31 @@ defineConfig({
4343
],
4444
});
4545

46-
defineConfig({
47-
settings: {
48-
react: {
49-
version: "detect",
46+
defineConfig([
47+
{
48+
settings: {
49+
react: {
50+
version: "detect",
51+
},
5052
},
5153
},
52-
});
54+
]);
5355

54-
defineConfig({
55-
extends: [
56-
[
57-
[{ rules: { "no-alert": "warn" } }],
58-
{ rules: { "no-debugger": "error" } },
59-
],
60-
[
61-
{ rules: { "no-eval": "error" } },
62-
{ rules: { "no-implied-eval": "error" } },
56+
defineConfig(
57+
{
58+
extends: [
59+
[
60+
[{ rules: { "no-alert": "warn" } }],
61+
{ rules: { "no-debugger": "error" } },
62+
],
63+
[
64+
{ rules: { "no-eval": "error" } },
65+
{ rules: { "no-implied-eval": "error" } },
66+
],
6367
],
64-
],
65-
});
68+
},
69+
globalIgnores(["node_modules"]),
70+
);
71+
72+
globalIgnores(["node_modules"]);
73+
globalIgnores(["dist", "build"], "my name");

0 commit comments

Comments
 (0)