|
| 1 | +/** |
| 2 | + * @fileoverview Ignore file utilities for the config-helpers package. |
| 3 | + * This file was forked from the source code for the compat package. |
| 4 | + * |
| 5 | + * @author Nicholas C. Zakas |
| 6 | + * @author Kirk Waiblinger |
| 7 | + */ |
| 8 | + |
| 9 | +//----------------------------------------------------------------------------- |
| 10 | +// Imports |
| 11 | +//----------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import fs from "node:fs"; |
| 14 | +import path from "node:path"; |
| 15 | + |
| 16 | +//----------------------------------------------------------------------------- |
| 17 | +// Types |
| 18 | +//----------------------------------------------------------------------------- |
| 19 | + |
| 20 | +/** @typedef {import("@eslint/core").ConfigObject} Config */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @typedef {object} IncludeIgnoreFileOptionsObject |
| 24 | + * @property {boolean} [gitignoreResolution] Whether to interpret the contents of an ignore file relative to the config file or the ignore file. |
| 25 | + * - gitignoreResolution: false (default): Interprets ignore patterns relative to the config file |
| 26 | + * - gitignoreResolution: true: Interprets the ignore patterns in a file relative to the ignore file |
| 27 | + * @property {string} [name] The name to give the output config object(s). |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * Options for `includeIgnoreFile()`. May be provided as an object or, for |
| 32 | + * legacy compatibility with `@eslint/compat`, as a string which is treated as |
| 33 | + * the `name` option. |
| 34 | + * @typedef {IncludeIgnoreFileOptionsObject | string} IncludeIgnoreFileOptions |
| 35 | + */ |
| 36 | + |
| 37 | +//----------------------------------------------------------------------------- |
| 38 | +// Exports |
| 39 | +//----------------------------------------------------------------------------- |
| 40 | + |
| 41 | +/** |
| 42 | + * Converts an ESLint ignore pattern to a minimatch pattern. |
| 43 | + * @param {string} pattern The .eslintignore or .gitignore pattern to convert. |
| 44 | + * @returns {string} The converted pattern. |
| 45 | + */ |
| 46 | +export function convertIgnorePatternToMinimatch(pattern) { |
| 47 | + const isNegated = pattern.startsWith("!"); |
| 48 | + const negatedPrefix = isNegated ? "!" : ""; |
| 49 | + const patternToTest = (isNegated ? pattern.slice(1) : pattern).trimEnd(); |
| 50 | + |
| 51 | + // special cases |
| 52 | + if (["", "**", "/**", "**/"].includes(patternToTest)) { |
| 53 | + return `${negatedPrefix}${patternToTest}`; |
| 54 | + } |
| 55 | + |
| 56 | + const firstIndexOfSlash = patternToTest.indexOf("/"); |
| 57 | + |
| 58 | + const matchEverywherePrefix = |
| 59 | + firstIndexOfSlash < 0 || firstIndexOfSlash === patternToTest.length - 1 |
| 60 | + ? "**/" |
| 61 | + : ""; |
| 62 | + |
| 63 | + const patternWithoutLeadingSlash = |
| 64 | + firstIndexOfSlash === 0 ? patternToTest.slice(1) : patternToTest; |
| 65 | + |
| 66 | + /* |
| 67 | + * Escape `{` and `(` because in gitignore patterns they are just |
| 68 | + * literal characters without any specific syntactic meaning, |
| 69 | + * while in minimatch patterns they can form brace expansion or extglob syntax. |
| 70 | + * |
| 71 | + * For example, gitignore pattern `src/{a,b}.js` ignores file `src/{a,b}.js`. |
| 72 | + * But, the same minimatch pattern `src/{a,b}.js` ignores files `src/a.js` and `src/b.js`. |
| 73 | + * Minimatch pattern `src/\{a,b}.js` is equivalent to gitignore pattern `src/{a,b}.js`. |
| 74 | + */ |
| 75 | + const escapedPatternWithoutLeadingSlash = |
| 76 | + patternWithoutLeadingSlash.replaceAll( |
| 77 | + // eslint-disable-next-line regexp/no-empty-lookarounds-assertion -- False positive |
| 78 | + /(?=((?:\\.|[^{(])*))\1([{(])/guy, |
| 79 | + "$1\\$2", |
| 80 | + ); |
| 81 | + |
| 82 | + const matchInsideSuffix = patternToTest.endsWith("/**") ? "/*" : ""; |
| 83 | + |
| 84 | + return `${negatedPrefix}${matchEverywherePrefix}${escapedPatternWithoutLeadingSlash}${matchInsideSuffix}`; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @param {string} ignoreFilePath |
| 89 | + * @returns {string[]} |
| 90 | + */ |
| 91 | +function ignoreFilePathToPatterns(ignoreFilePath) { |
| 92 | + const ignoreFile = fs.readFileSync(ignoreFilePath, "utf8"); |
| 93 | + const lines = ignoreFile.split(/\r?\n/u); |
| 94 | + |
| 95 | + return lines |
| 96 | + .map(line => line.trim()) |
| 97 | + .filter(line => line && !line.startsWith("#")) |
| 98 | + .map(convertIgnorePatternToMinimatch); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Helper to parse and validate the options to `includeIgnoreFile()` |
| 103 | + * |
| 104 | + * @param {string | { gitignoreResolution?: unknown, name?: unknown } | undefined} options |
| 105 | + * @returns {{ gitignoreResolution: boolean, name: string }} |
| 106 | + */ |
| 107 | +function parseOptions(options) { |
| 108 | + // legacy compatibility with @eslint/compat's `includeIgnoreFile` |
| 109 | + if (typeof options === "string") { |
| 110 | + return { gitignoreResolution: false, name: options }; |
| 111 | + } |
| 112 | + |
| 113 | + const optionsObject = options ?? {}; |
| 114 | + if (typeof optionsObject !== "object" || Array.isArray(optionsObject)) { |
| 115 | + throw new TypeError( |
| 116 | + "The options argument to `includeIgnoreFile()` should be an object or a string.", |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + const gitignoreResolution = optionsObject.gitignoreResolution ?? false; |
| 121 | + if (typeof gitignoreResolution !== "boolean") { |
| 122 | + throw new TypeError( |
| 123 | + "The `gitignoreResolution` option must be specified a boolean or omitted", |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + const name = optionsObject.name ?? `Imported .gitignore patterns`; |
| 128 | + if (typeof name !== "string") { |
| 129 | + throw new TypeError( |
| 130 | + "The `name` option must be specified as a string or omitted.", |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + return { gitignoreResolution, name }; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * @overload |
| 139 | + * |
| 140 | + * Reads ignore files and returns objects with the ignore patterns. |
| 141 | + * |
| 142 | + * @param {string[]} ignoreFilePathArg The paths of ignore files to include. |
| 143 | + * @param {IncludeIgnoreFileOptions} [options] |
| 144 | + * @returns {Config[]} |
| 145 | + */ |
| 146 | + |
| 147 | +/** |
| 148 | + * @overload |
| 149 | + * |
| 150 | + * Reads an ignore file and returns an object with the ignore patterns. |
| 151 | + * |
| 152 | + * @param {string} ignoreFilePathArg The path of the ignore file to include. |
| 153 | + * @param {IncludeIgnoreFileOptions} [options] |
| 154 | + * @returns {Config} |
| 155 | + */ |
| 156 | + |
| 157 | +/** |
| 158 | + * @overload |
| 159 | + * |
| 160 | + * Reads an ignore file(s) and returns an object(s) with the ignore patterns. |
| 161 | + * |
| 162 | + * @param {string[] | string} ignoreFilePathArg The path(s) of the ignore file(s) to include. |
| 163 | + * @param {IncludeIgnoreFileOptions} [options] |
| 164 | + * @returns {Config[] | Config} |
| 165 | + */ |
| 166 | + |
| 167 | +/** |
| 168 | + * Reads an ignore file(s) and returns an object(s) with the ignore patterns. |
| 169 | + * |
| 170 | + * @param {string[] | string} ignoreFilePathArg The path(s) of the ignore file(s) to include. |
| 171 | + * @param {IncludeIgnoreFileOptions} [options] |
| 172 | + * @returns {Config[] | Config} |
| 173 | + */ |
| 174 | +export function includeIgnoreFile(ignoreFilePathArg, options) { |
| 175 | + const returnSingleObject = !Array.isArray(ignoreFilePathArg); |
| 176 | + const ignoreFilePaths = Array.isArray(ignoreFilePathArg) |
| 177 | + ? ignoreFilePathArg |
| 178 | + : [ignoreFilePathArg]; |
| 179 | + for (const ignorePath of ignoreFilePaths) { |
| 180 | + if (typeof ignorePath !== "string") { |
| 181 | + throw new TypeError( |
| 182 | + "The first argument to `includeIgnoreFile()` should be a string or array of strings", |
| 183 | + ); |
| 184 | + } |
| 185 | + if (!path.isAbsolute(ignorePath)) { |
| 186 | + throw new Error( |
| 187 | + `The ignore file location must be an absolute path. Received ${ignorePath}`, |
| 188 | + ); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + const { gitignoreResolution, name } = parseOptions(options); |
| 193 | + |
| 194 | + if (returnSingleObject) { |
| 195 | + return { |
| 196 | + name, |
| 197 | + ignores: ignoreFilePathToPatterns(ignoreFilePathArg), |
| 198 | + ...(gitignoreResolution |
| 199 | + ? { basePath: path.dirname(ignoreFilePathArg) } |
| 200 | + : {}), |
| 201 | + }; |
| 202 | + } |
| 203 | + |
| 204 | + return ignoreFilePaths.map((ignoreFilePath, i) => ({ |
| 205 | + name: `${name} (${i})`, |
| 206 | + ignores: ignoreFilePathToPatterns(ignoreFilePath), |
| 207 | + ...(gitignoreResolution |
| 208 | + ? { basePath: path.dirname(ignoreFilePath) } |
| 209 | + : {}), |
| 210 | + })); |
| 211 | +} |
0 commit comments