Skip to content

Commit

Permalink
feat(code-eslint): enable cache strategy (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelfimov authored Dec 20, 2024
1 parent 867ec77 commit b6e9360
Show file tree
Hide file tree
Showing 4 changed files with 565 additions and 528 deletions.
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 @@ import { createLintResult } from './linter.utils.js'

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 @@ export class Linter extends EventEmitter {
}

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 @@ export class Linter extends EventEmitter {
},
}))

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,10 +113,29 @@ export class Linter extends EventEmitter {
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 })

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> {
Expand Down
Loading

0 comments on commit b6e9360

Please sign in to comment.