-
Notifications
You must be signed in to change notification settings - Fork 15
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): create groups from rules' meta.type (problem/sug…
…gestion/layout)
- Loading branch information
1 parent
346596d
commit 0350e49
Showing
9 changed files
with
541 additions
and
14 deletions.
There are no files selected for viewing
425 changes: 425 additions & 0 deletions
425
packages/plugin-eslint/src/lib/__snapshots__/eslint-plugin.spec.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
import type { Rule } from 'eslint'; | ||
import type { AuditGroup, AuditGroupRef } from '@code-pushup/models'; | ||
import { objectToKeys } from '@code-pushup/utils'; | ||
import { ruleIdToSlug } from './hash'; | ||
import type { RuleData } from './rules'; | ||
|
||
type RuleType = NonNullable<Rule.RuleMetaData['type']>; | ||
|
||
// docs on meta.type: https://eslint.org/docs/latest/extend/custom-rules#rule-structure | ||
const typeGroups: Record<RuleType, Omit<AuditGroup, 'refs'>> = { | ||
problem: { | ||
slug: 'problems', | ||
title: 'Problems', | ||
description: | ||
'Code that either will cause an error or may cause confusing behavior. Developers should consider this a high priority to resolve.', | ||
}, | ||
suggestion: { | ||
slug: 'suggestions', | ||
title: 'Suggestions', | ||
description: | ||
"Something that could be done in a better way but no errors will occur if the code isn't changed.", | ||
}, | ||
layout: { | ||
slug: 'formatting', | ||
title: 'Formatting', | ||
description: | ||
'Primarily about whitespace, semicolons, commas, and parentheses, all the parts of the program that determine how the code looks rather than how it executes.', | ||
}, | ||
}; | ||
|
||
export function groupsFromRuleTypes(rules: RuleData[]): AuditGroup[] { | ||
const allTypes = objectToKeys(typeGroups); | ||
|
||
const auditSlugsMap = rules.reduce<Partial<Record<RuleType, string[]>>>( | ||
(acc, { meta: { type }, ruleId, options }) => | ||
type == null | ||
? acc | ||
: { | ||
...acc, | ||
[type]: [...(acc[type] ?? []), ruleIdToSlug(ruleId, options)], | ||
}, | ||
{}, | ||
); | ||
|
||
return allTypes.map(type => ({ | ||
...typeGroups[type], | ||
refs: | ||
auditSlugsMap[type]?.map( | ||
(slug): AuditGroupRef => ({ slug, weight: 1 }), | ||
) ?? [], | ||
})); | ||
} |
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,18 @@ | ||
import type { ESLint } from 'eslint'; | ||
import type { Audit } from '@code-pushup/models'; | ||
import type { Audit, AuditGroup } from '@code-pushup/models'; | ||
import { groupsFromRuleTypes } from './groups'; | ||
import { listRules } from './rules'; | ||
import { ruleToAudit } from './transform'; | ||
|
||
export async function listAudits( | ||
export async function listAuditsAndGroups( | ||
eslint: ESLint, | ||
patterns: string | string[], | ||
): Promise<Audit[]> { | ||
): Promise<{ audits: Audit[]; groups: AuditGroup[] }> { | ||
const rules = await listRules(eslint, patterns); | ||
return rules.map(ruleToAudit); | ||
|
||
const audits = rules.map(ruleToAudit); | ||
|
||
const groups = groupsFromRuleTypes(rules); | ||
|
||
return { audits, groups }; | ||
} |
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