-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-eslint): rule options used to identify audit, options in …
…slug (hash) and description
- Loading branch information
1 parent
a546d68
commit b9f51c9
Showing
4 changed files
with
187 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,33 @@ | ||
import type { Audit } from '@quality-metrics/models'; | ||
import { distinct, slugify, toArray } from '@quality-metrics/utils'; | ||
import type { ESLint, Linter, Rule } from 'eslint'; | ||
import type { ESLint } from 'eslint'; | ||
import { ruleIdToSlug } from './hash'; | ||
import { RuleData, listRules } from './rules'; | ||
|
||
export async function listAudits( | ||
eslint: ESLint, | ||
patterns: string | string[], | ||
): Promise<Audit[]> { | ||
const configs = await toArray(patterns).reduce( | ||
async (acc, pattern) => [ | ||
...(await acc), | ||
await eslint.calculateConfigForFile(pattern), | ||
], | ||
Promise.resolve<Linter.Config[]>([]), | ||
); | ||
|
||
const rulesIds = distinct( | ||
configs.flatMap(config => Object.keys(config.rules ?? {})), | ||
); | ||
const rulesMeta = eslint.getRulesMetaForResults([ | ||
{ | ||
messages: rulesIds.map(ruleId => ({ ruleId })), | ||
suppressedMessages: [] as Linter.SuppressedLintMessage[], | ||
} as ESLint.LintResult, | ||
]); | ||
|
||
return Object.entries(rulesMeta).map(args => ruleToAudit(...args)); | ||
const rules = await listRules(eslint, patterns); | ||
return rules.map(ruleToAudit); | ||
} | ||
|
||
function ruleToAudit(ruleId: string, meta: Rule.RuleMetaData): Audit { | ||
export function ruleToAudit({ ruleId, meta, options }: RuleData): Audit { | ||
const name = ruleId.split('/').at(-1) ?? ruleId; | ||
const plugin = | ||
name === ruleId ? null : ruleId.slice(0, ruleId.lastIndexOf('/')); | ||
// TODO: add custom options hash to slug, copy to description | ||
|
||
const lines: string[] = [ | ||
`ESLint rule **${name}**${plugin ? `, from _${plugin}_ plugin` : ''}.`, | ||
...(options?.length ? ['Custom options:'] : []), | ||
...(options?.map(option => | ||
['```json', JSON.stringify(option, null, 2), '```'].join('\n'), | ||
) ?? []), | ||
]; | ||
|
||
return { | ||
slug: slugify(ruleId), | ||
slug: ruleIdToSlug(ruleId, options), | ||
title: meta.docs?.description ?? name, | ||
description: `ESLint rule **${name}**${ | ||
plugin ? `, from _${plugin}_ plugin` : '' | ||
}.`, | ||
description: lines.join('\n\n'), | ||
docsUrl: meta.docs?.url, | ||
}; | ||
} |
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,19 @@ | ||
import { slugify } from '@quality-metrics/utils'; | ||
import { createHash } from 'crypto'; | ||
|
||
export function ruleIdToSlug( | ||
ruleId: string, | ||
options: unknown[] | undefined, | ||
): string { | ||
const slug = slugify(ruleId); | ||
if (!options?.length) { | ||
return slug; | ||
} | ||
return `${slug}-${jsonHash(options)}`; | ||
} | ||
|
||
export function jsonHash(data: unknown, bytes = 8): string { | ||
return createHash('shake256', { outputLength: bytes }) | ||
.update(JSON.stringify(data)) | ||
.digest('hex'); | ||
} |
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,61 @@ | ||
import { distinct, toArray } from '@quality-metrics/utils'; | ||
import type { ESLint, Linter, Rule } from 'eslint'; | ||
import { jsonHash } from './hash'; | ||
|
||
export type RuleData = { | ||
ruleId: string; | ||
meta: Rule.RuleMetaData; | ||
options: unknown[] | undefined; | ||
}; | ||
|
||
export async function listRules( | ||
eslint: ESLint, | ||
patterns: string | string[], | ||
): Promise<RuleData[]> { | ||
const configs = await toArray(patterns).reduce( | ||
async (acc, pattern) => [ | ||
...(await acc), | ||
await eslint.calculateConfigForFile(pattern), | ||
], | ||
Promise.resolve<Linter.Config[]>([]), | ||
); | ||
|
||
const rulesIds = distinct( | ||
configs.flatMap(config => Object.keys(config.rules ?? {})), | ||
); | ||
const rulesMeta = eslint.getRulesMetaForResults([ | ||
{ | ||
messages: rulesIds.map(ruleId => ({ ruleId })), | ||
suppressedMessages: [] as Linter.SuppressedLintMessage[], | ||
} as ESLint.LintResult, | ||
]); | ||
|
||
const rulesMap = configs | ||
.flatMap(config => Object.entries(config.rules ?? {})) | ||
.reduce<Record<string, Record<string, RuleData>>>( | ||
(acc, [ruleId, ruleEntry]) => { | ||
const meta = rulesMeta[ruleId]; | ||
if (!meta) { | ||
console.warn(`Metadata not found for ESLint rule ${ruleId}`); | ||
return acc; | ||
} | ||
const options = toArray(ruleEntry).slice(1); | ||
const optionsHash = jsonHash(options); | ||
const ruleData: RuleData = { | ||
ruleId, | ||
meta, | ||
options, | ||
}; | ||
return { | ||
...acc, | ||
[ruleId]: { | ||
...acc[ruleId], | ||
[optionsHash]: ruleData, | ||
}, | ||
}; | ||
}, | ||
{}, | ||
); | ||
|
||
return Object.values(rulesMap).flatMap(Object.values); | ||
} |