Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(code-eslint): enable cache strategy #460

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/eslint/.eslintcache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/commitlint/src/commitlint.enums.ts":"1","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/commitlint/src/commitlint.rules.ts":"2","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/commitlint/src/index.ts":"3","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/index.ts":"4","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/base.rules.ts":"5","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/index.ts":"6","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/nextjs.rules.ts":"7","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/node.rules.ts":"8","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/react.rules.ts":"9","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/security.rules.ts":"10","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/typescript.rules.ts":"11","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/prettier/src/index.ts":"12","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/typescript/dist/index.d.ts":"13","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/typescript/dist/index.js":"14","/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/typescript/src/index.ts":"15"},{"results":"16","hashOfConfig":"17","size":1279,"mtime":1732437182522},{"results":"18","hashOfConfig":"17","size":756,"mtime":1732437182522},{"results":"19","hashOfConfig":"17","size":76,"mtime":1732437182522},{"results":"20","hashOfConfig":"17","size":2605,"mtime":1734675915872},{"results":"21","hashOfConfig":"17","size":18045,"mtime":1732437182523},{"results":"22","hashOfConfig":"17","size":314,"mtime":1732437182523},{"results":"23","hashOfConfig":"17","size":1031,"mtime":1732437182523},{"results":"24","hashOfConfig":"17","size":786,"mtime":1732437182523},{"results":"25","hashOfConfig":"17","size":11646,"mtime":1732437182524},{"results":"26","hashOfConfig":"17","size":755,"mtime":1732437182524},{"results":"27","hashOfConfig":"17","size":7974,"mtime":1734676508431},{"results":"28","hashOfConfig":"17","size":208,"mtime":1732437182525},{"results":"29","hashOfConfig":"17","size":999,"mtime":1734432311074},{"results":"30","hashOfConfig":"31","size":1252,"mtime":1734432311071},{"results":"32","hashOfConfig":"17","size":1045,"mtime":1734432428222},{"filePath":"33","messages":"34","suppressedMessages":"35","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"a168i0",{"filePath":"36","messages":"37","suppressedMessages":"38","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"39","messages":"40","suppressedMessages":"41","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","suppressedMessages":"44","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"45","messages":"46","suppressedMessages":"47","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"48","messages":"49","suppressedMessages":"50","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"51","messages":"52","suppressedMessages":"53","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"54","messages":"55","suppressedMessages":"56","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"57","messages":"58","suppressedMessages":"59","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"60","messages":"61","suppressedMessages":"62","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"63","messages":"64","suppressedMessages":"65","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"66","messages":"67","suppressedMessages":"68","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"69","messages":"70","suppressedMessages":"71","errorCount":4,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":2,"fixableWarningCount":0,"source":null},{"filePath":"72","messages":"73","suppressedMessages":"74","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1nohspf",{"filePath":"75","messages":"76","suppressedMessages":"77","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/commitlint/src/commitlint.enums.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/commitlint/src/commitlint.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/commitlint/src/index.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/index.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/base.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/index.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/nextjs.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/node.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/react.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/security.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/eslint/src/rules/typescript.rules.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/prettier/src/index.ts",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/typescript/dist/index.d.ts",["78","79","80","81"],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/typescript/dist/index.js",[],[],"/Users/nikoroach/Documents/Dev/JS/atls-raijin/config/typescript/src/index.ts",[],[],{"ruleId":"82","severity":2,"message":"83","line":1,"column":15,"nodeType":"84","messageId":"85","endLine":32,"endColumn":2},{"ruleId":"86","severity":2,"message":"87","line":1,"column":15,"nodeType":"88","messageId":"89","endLine":32,"endColumn":2},{"ruleId":"90","severity":2,"message":"91","line":3,"column":14,"nodeType":"92","messageId":"93","endLine":3,"endColumn":22,"fix":"94"},{"ruleId":"90","severity":2,"message":"91","line":31,"column":14,"nodeType":"92","messageId":"93","endLine":31,"endColumn":22,"fix":"95"},"@typescript-eslint/naming-convention","Variable name `_default` must match one of the following formats: camelCase, PascalCase, UPPER_CASE","Identifier","doesNotMatchFormat","no-underscore-dangle","Unexpected dangling '_' in '_default'.","VariableDeclarator","unexpectedUnderscore","@typescript-eslint/array-type","Array type using 'string[]' is forbidden. Use 'Array<string>' instead.","TSArrayType","errorStringGeneric",{"range":"96","text":"97"},{"range":"98","text":"97"},[62,70],"Array<string>",[961,969]]
41 changes: 35 additions & 6 deletions code/code-lint/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

export interface LintOptions {
fix?: boolean
cache?: boolean
}

export class Linter extends EventEmitter {
private ignore: ignorer.Ignore

constructor(
private readonly linter: ESLinter,
private readonly cacheLinter: ESLint,
private readonly config: Array<ESLinter.Config>,
private readonly cwd: string
) {
Expand All @@ -33,12 +35,12 @@
}

static async initialize(rootCwd: string, cwd: string): Promise<Linter> {
const { Linter: LinterConstructor } = await import('@atls/code-runtime/eslint')
const { Linter: LinterConstructor, ESLint } = await import('@atls/code-runtime/eslint')
const { eslintconfig } = await import('@atls/code-runtime/eslint')

const linter = new LinterConstructor({ configType: 'flat' })

const config = eslintconfig.map((item) => ({
const config: Array<ESLinter.Config> = eslintconfig.map((item) => ({
...item,
languageOptions: {
...(item.languageOptions || {}),
Expand All @@ -49,7 +51,15 @@
},
}))

return new Linter(linter, config, cwd)
const eslint = new ESLint({
cache: true,
baseConfig: config,
overrideConfigFile: true,
cwd,
cacheLocation: join(rootCwd, '.config/eslint/.eslintcache'),
})

return new Linter(linter, eslint, config, cwd)
}

async lintFile(filename: string, options?: LintOptions): Promise<ESLint.LintResult> {
Expand Down Expand Up @@ -103,17 +113,36 @@
const filesForLint =
files && files.length > 0 ? files : await globby(createPatterns(this.cwd), { dot: true })

return this.lintFiles(
filesForLint.filter((file) => this.ignore.filter([relative(this.cwd, file)]).length !== 0),
options
const finalFiles = filesForLint.filter(
(file) => this.ignore.filter([relative(this.cwd, file)]).length !== 0
)

if (options?.cache) {
return this.lintWithCache(finalFiles)
}

return this.lintFiles(finalFiles, options)
}

private async lintWithCache(files: Array<string> = []): Promise<ESLint.LintResult[]> {
this.emit('start', { files })

Check failure on line 128 in code/code-lint/src/linter.ts

View workflow job for this annotation

GitHub Actions / Lint

(@typescript-eslint/array-type): Array type using 'ESLint.LintResult[]' is forbidden. Use 'Array<ESLint.LintResult>' instead.

Array type using 'ESLint.LintResult[]' is forbidden. Use 'Array<ESLint.LintResult>' instead.
Raw output
  125 |   }
  126 |
> 127 |   private async lintWithCache(files: Array<string> = []): Promise<ESLint.LintResult[]> {
      |                                                                   ^
  128 |     this.emit('start', { files })
  129 |
  130 |     const results = await this.cacheLinter?.lintFiles(files)

const results = await this.cacheLinter?.lintFiles(files)

for (const result of results) {
this.emit('lint:end', { result })
}

this.emit('end', { results })

return results
}

private getProjectIgnorePatterns(): Array<string> {
const content = readFileSync(join(this.cwd, 'package.json'), 'utf-8')

Check failure on line 143 in code/code-lint/src/linter.ts

View workflow job for this annotation

GitHub Actions / Lint

(n/no-sync): Unexpected sync method: 'readFileSync'.

Unexpected sync method: 'readFileSync'.
Raw output
  140 |
  141 |   private getProjectIgnorePatterns(): Array<string> {
> 142 |     const content = readFileSync(join(this.cwd, 'package.json'), 'utf-8')
      |                     ^
  143 |
  144 |     const { linterIgnorePatterns = [] } = JSON.parse(content)
  145 |
const { linterIgnorePatterns = [] } = JSON.parse(content)

return linterIgnorePatterns
}

Check failure on line 147 in code/code-lint/src/linter.ts

View workflow job for this annotation

GitHub Actions / Lint

(@typescript-eslint/no-unsafe-return): Unsafe return of a value of type `any`.

Unsafe return of a value of type `any`.
Raw output
  144 |     const { linterIgnorePatterns = [] } = JSON.parse(content)
  145 |
> 146 |     return linterIgnorePatterns
      |     ^
  147 |   }
  148 | }
  149 |
}
Loading
Loading