-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feature(eslint)!: require eslint v9.5+
Signed-off-by: Pauline <[email protected]>
- Loading branch information
1 parent
02089c2
commit 948e472
Showing
14 changed files
with
311 additions
and
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,73 @@ | ||
import fs from 'node:fs'; | ||
import { findUpSync } from 'find-up'; | ||
// @ts-expect-error no types | ||
import parse from 'parse-gitignore'; | ||
import process from 'node:process'; | ||
import path from 'pathe'; | ||
import { findUpSync } from 'find-up-simple'; | ||
import type { OptionsGitignore, TypedFlatConfigItem } from '../types'; | ||
import { toArray } from '../utils'; | ||
import { convertIgnorePatternToMinimatch } from '@eslint/compat'; | ||
|
||
export async function gitignore(options: OptionsGitignore = {}): Promise<TypedFlatConfigItem[]> { | ||
// eslint-disable-next-line perfectionist/sort-objects -- root needs to be defined first | ||
const { root = false, strict = true, files = root ? ['.gitignore'] : [findUpSync('.gitignore')!] || [] } = options; | ||
const ignores: string[] = []; | ||
const { cwd = process.cwd(), root = false, strict = true } = options; | ||
const files = toArray(options.files ?? root ? ['.gitignore'] : findUpSync('.gitignore') || []); | ||
|
||
files.forEach((f) => { | ||
if (!fs.existsSync(f)) | ||
if (strict) | ||
throw new Error(`.gitignore file was not found: ${f}`); | ||
else return; | ||
|
||
const readFile = fs.readFileSync(f, 'utf-8'); | ||
parse(`${readFile}\n`).globs().forEach((g: any) => { | ||
if (g.type === 'ignore') | ||
ignores.push(...g.patterns); | ||
else if (g.type === 'unignore') | ||
ignores.push(...g.patterns.map((p: string) => `!${p}`)); | ||
}); | ||
const content = fs.readFileSync(f, 'utf-8'); | ||
const relativePath = path.relative(cwd, path.dirname(f)); | ||
const globs = content.split(/\r?\n/u) | ||
.filter(l => l && !l.startsWith('#')) | ||
.map(l => convertIgnorePatternToMinimatch(l)) | ||
.map(g => relativeMinimatch(g, relativePath, cwd)) | ||
.filter(g => g !== null); | ||
|
||
ignores.push(...globs); | ||
}); | ||
|
||
if (strict && files.length === 0) | ||
throw new Error('no .gitignore file found'); | ||
|
||
return [ | ||
{ | ||
ignores, | ||
// todo | ||
// name: 'petal/ignores/git', | ||
name: 'petal/ignores/git', | ||
}, | ||
]; | ||
} | ||
|
||
function relativeMinimatch(pattern: string, relativePath: string, cwd = process.cwd()) { | ||
if (['', '.', '/'].includes(relativePath)) | ||
return pattern; | ||
|
||
const negated = pattern.startsWith('!') ? '!' : ':'; | ||
const minimatch = { clearPattern: negated ? pattern.slice(1) : pattern }; | ||
|
||
if (!relativePath.endsWith('/')) | ||
relativePath = `${relativePath}/`; | ||
|
||
const parent = relativePath.startsWith('..'); | ||
|
||
if (!parent) | ||
return `${negated}${relativePath}${minimatch.clearPattern}`; | ||
|
||
if (!relativePath.match(/^(\.\.\/)+$/)) | ||
throw new Error('the ignored file location should be a parent or child directory.'); | ||
|
||
if (minimatch.clearPattern.startsWith('**')) | ||
return pattern; | ||
|
||
const parents = path.relative(path.resolve(cwd, relativePath), cwd).split(/[/\\]/); | ||
while (parents.length && minimatch.clearPattern.startsWith(`${parents[0]}/`)) { | ||
minimatch.clearPattern = minimatch.clearPattern.slice(parents[0].length + 1); | ||
parents.shift(); | ||
} | ||
|
||
if (minimatch.clearPattern.startsWith('**') || parents.length === 0) | ||
return `${negated}${minimatch.clearPattern}`; | ||
|
||
return null; | ||
} |
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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import type { TypedFlatConfigItem } from '../types'; | ||
import { GLOB_EXCLUDE } from '../globs'; | ||
|
||
export async function ignores(): Promise<TypedFlatConfigItem[]> { | ||
export async function ignores(userIgnores: string[] = []): Promise<TypedFlatConfigItem[]> { | ||
return [ | ||
{ | ||
ignores: GLOB_EXCLUDE, | ||
// todo | ||
// name: 'petal/ignores', | ||
ignores: [ | ||
...GLOB_EXCLUDE, | ||
...userIgnores, | ||
], | ||
name: 'petal/ignores', | ||
}, | ||
]; | ||
} |
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
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,9 @@ | ||
export interface OptionsUnicorn { | ||
/** | ||
* Include all rules recommended by `eslint-plugin-unicorn`. | ||
* | ||
* @see https://github.com/sindresorhus/eslint-plugin-unicorn#rules | ||
* @default false | ||
*/ | ||
useRecommended?: boolean; | ||
} |
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
Oops, something went wrong.