Skip to content

Commit 3e18175

Browse files
authored
feat: add support for custom name parameter to includeIgnoreFile (#211)
1 parent cb858ff commit 3e18175

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

packages/compat/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ module.exports = [
145145

146146
### Including Ignore Files
147147

148-
If you were using an alternate ignore file in ESLint v8.x, such as using `--ignore-path .gitignore` on the command line, you can include those patterns programmatically in your config file using the `includeIgnoreFile()` function. For example:
148+
If you were using an alternate ignore file in ESLint v8.x, such as using `--ignore-path .gitignore` on the command line, you can include those patterns programmatically in your config file using the `includeIgnoreFile()` function.
149+
150+
The `includeIgnoreFile()` function also accepts a second optional `name` parameter that allows you to set a custom name for this configuration object. If not specified, it defaults to `"Imported .gitignore patterns"`. For example:
149151

150152
```js
151153
// eslint.config.js - ESM example
@@ -158,7 +160,7 @@ const __dirname = path.dirname(__filename);
158160
const gitignorePath = path.resolve(__dirname, ".gitignore");
159161

160162
export default [
161-
includeIgnoreFile(gitignorePath),
163+
includeIgnoreFile(gitignorePath, "Imported .gitignore patterns"), // second argument is optional.
162164
{
163165
// your overrides
164166
},
@@ -174,7 +176,7 @@ const path = require("node:path");
174176
const gitignorePath = path.resolve(__dirname, ".gitignore");
175177

176178
module.exports = [
177-
includeIgnoreFile(gitignorePath),
179+
includeIgnoreFile(gitignorePath, "Imported .gitignore patterns"), // second argument is optional.
178180
{
179181
// your overrides
180182
},

packages/compat/src/ignore-file.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ export function convertIgnorePatternToMinimatch(pattern) {
6868
/**
6969
* Reads an ignore file and returns an object with the ignore patterns.
7070
* @param {string} ignoreFilePath The absolute path to the ignore file.
71+
* @param {string} [name] The name of the ignore file config.
7172
* @returns {FlatConfig} An object with an `ignores` property that is an array of ignore patterns.
7273
* @throws {Error} If the ignore file path is not an absolute path.
7374
*/
74-
export function includeIgnoreFile(ignoreFilePath) {
75+
export function includeIgnoreFile(ignoreFilePath, name) {
7576
if (!path.isAbsolute(ignoreFilePath)) {
7677
throw new Error("The ignore file location must be an absolute path.");
7778
}
@@ -80,7 +81,7 @@ export function includeIgnoreFile(ignoreFilePath) {
8081
const lines = ignoreFile.split(/\r?\n/u);
8182

8283
return {
83-
name: "Imported .gitignore patterns",
84+
name: name || "Imported .gitignore patterns",
8485
ignores: lines
8586
.map(line => line.trim())
8687
.filter(line => line && !line.startsWith("#"))

packages/compat/tests/ignore-file.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,28 @@ describe("@eslint/compat", () => {
8888
],
8989
});
9090
});
91+
92+
it("should return an object with a custom name", () => {
93+
const ignoreFilePath = fileURLToPath(
94+
new URL(
95+
"../tests/fixtures/ignore-files/gitignore1.txt",
96+
import.meta.url,
97+
),
98+
);
99+
const result = includeIgnoreFile(ignoreFilePath, "Custom Name");
100+
assert.deepStrictEqual(result, {
101+
name: "Custom Name",
102+
ignores: [
103+
"**/node_modules",
104+
"!fixtures/node_modules",
105+
"dist",
106+
"**/*.log",
107+
"**/.cache/",
108+
".vuepress/dist",
109+
"*/foo.js",
110+
"dir/**/*",
111+
],
112+
});
113+
});
91114
});
92115
});

0 commit comments

Comments
 (0)