-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add includeIgnoreFile() method (#47)
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
- Loading branch information
1 parent
10d8200
commit b5f74ed
Showing
9 changed files
with
240 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @fileoverview Ignore file utilities for the compat package. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Imports | ||
//----------------------------------------------------------------------------- | ||
|
||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
//----------------------------------------------------------------------------- | ||
// Types | ||
//----------------------------------------------------------------------------- | ||
|
||
/** @typedef {import("eslint").Linter.FlatConfig} FlatConfig */ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Exports | ||
//----------------------------------------------------------------------------- | ||
|
||
/** | ||
* Converts an ESLint ignore pattern to a minimatch pattern. | ||
* @param {string} pattern The .eslintignore or .gitignore pattern to convert. | ||
* @returns {string} The converted pattern. | ||
*/ | ||
export function convertIgnorePatternToMinimatch(pattern) { | ||
const isNegated = pattern.startsWith("!"); | ||
const negatedPrefix = isNegated ? "!" : ""; | ||
const patternToTest = (isNegated ? pattern.slice(1) : pattern).trimEnd(); | ||
|
||
// special cases | ||
if (["", "**", "/**", "**/"].includes(patternToTest)) { | ||
return `${negatedPrefix}${patternToTest}`; | ||
} | ||
|
||
const firstIndexOfSlash = patternToTest.indexOf("/"); | ||
|
||
const matchEverywherePrefix = | ||
firstIndexOfSlash < 0 || firstIndexOfSlash === patternToTest.length - 1 | ||
? "**/" | ||
: ""; | ||
|
||
const patternWithoutLeadingSlash = | ||
firstIndexOfSlash === 0 ? patternToTest.slice(1) : patternToTest; | ||
|
||
const matchInsideSuffix = patternToTest.endsWith("/**") ? "/*" : ""; | ||
|
||
return `${negatedPrefix}${matchEverywherePrefix}${patternWithoutLeadingSlash}${matchInsideSuffix}`; | ||
} | ||
|
||
/** | ||
* Reads an ignore file and returns an object with the ignore patterns. | ||
* @param {string} ignoreFilePath The absolute path to the ignore file. | ||
* @returns {FlatConfig} An object with an `ignores` property that is an array of ignore patterns. | ||
* @throws {Error} If the ignore file path is not an absolute path. | ||
*/ | ||
export function includeIgnoreFile(ignoreFilePath) { | ||
if (!path.isAbsolute(ignoreFilePath)) { | ||
throw new Error("The ignore file location must be an absolute path."); | ||
} | ||
|
||
const ignoreFile = fs.readFileSync(ignoreFilePath, "utf8"); | ||
const lines = ignoreFile.split(/\r?\n/u); | ||
|
||
return { | ||
name: "Imported .gitignore patterns", | ||
ignores: lines | ||
.map(line => line.trim()) | ||
.filter(line => line && !line.startsWith("#")) | ||
.map(convertIgnorePatternToMinimatch), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*/ | ||
|
||
export * from "./fixup-rules.js"; | ||
export * from "./ignore-file.js"; |
17 changes: 17 additions & 0 deletions
17
packages/compat/tests/fixtures/ignore-files/gitignore1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Node.js | ||
node_modules | ||
!/fixtures/node_modules | ||
/dist | ||
|
||
# Logs | ||
*.log | ||
|
||
# Gatsby files | ||
.cache/ | ||
|
||
# vuepress build output | ||
.vuepress/dist | ||
|
||
# other | ||
*/foo.js | ||
dir/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* @filedescription Fixup tests | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Imports | ||
//----------------------------------------------------------------------------- | ||
|
||
import assert from "node:assert"; | ||
import { | ||
includeIgnoreFile, | ||
convertIgnorePatternToMinimatch, | ||
} from "../src/ignore-file.js"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
//----------------------------------------------------------------------------- | ||
// Tests | ||
//----------------------------------------------------------------------------- | ||
|
||
describe("@eslint/compat", () => { | ||
describe("convertIgnorePatternToMinimatch", () => { | ||
const tests = [ | ||
["", ""], | ||
["**", "**"], | ||
["/**", "/**"], | ||
["**/", "**/"], | ||
["src/", "**/src/"], | ||
["src", "**/src"], | ||
["src/**", "src/**/*"], | ||
["!src/", "!**/src/"], | ||
["!src", "!**/src"], | ||
["!src/**", "!src/**/*"], | ||
["*/foo.js", "*/foo.js"], | ||
["*/foo.js/", "*/foo.js/"], | ||
]; | ||
|
||
tests.forEach(([pattern, expected]) => { | ||
it(`should convert "${pattern}" to "${expected}"`, () => { | ||
assert.strictEqual( | ||
convertIgnorePatternToMinimatch(pattern), | ||
expected, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("includeIgnoreFile", () => { | ||
it("should throw an error when a relative path is passed", () => { | ||
const ignoreFilePath = | ||
"../tests/fixtures/ignore-files/gitignore1.txt"; | ||
assert.throws(() => { | ||
includeIgnoreFile(ignoreFilePath); | ||
}, /The ignore file location must be an absolute path./u); | ||
}); | ||
|
||
it("should return an object with an `ignores` property", () => { | ||
const ignoreFilePath = fileURLToPath( | ||
new URL( | ||
"../tests/fixtures/ignore-files/gitignore1.txt", | ||
import.meta.url, | ||
), | ||
); | ||
const result = includeIgnoreFile(ignoreFilePath); | ||
assert.deepStrictEqual(result, { | ||
name: "Imported .gitignore patterns", | ||
ignores: [ | ||
"**/node_modules", | ||
"!fixtures/node_modules", | ||
"dist", | ||
"**/*.log", | ||
"**/.cache/", | ||
".vuepress/dist", | ||
"*/foo.js", | ||
"dir/**/*", | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters