Skip to content

Commit

Permalink
refactor: use Object.fromEntries to build rule config maps (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods authored Jan 3, 2023
1 parent 3718e82 commit f3cb13b
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,27 @@ const importDefault = (moduleName: string) =>
const rulesDir = join(__dirname, 'rules');
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];

const rules = readdirSync(rulesDir)
.map(rule => parse(rule).name)
.filter(rule => !excludedFiles.includes(rule))
.reduce<Record<string, RuleModule>>(
(acc, curr) => ({
...acc,
[curr]: importDefault(join(rulesDir, curr)) as RuleModule,
}),
{},
);
const rules = Object.fromEntries(
readdirSync(rulesDir)
.map(rule => parse(rule).name)
.filter(rule => !excludedFiles.includes(rule))
.map(rule => [rule, importDefault(join(rulesDir, rule)) as RuleModule]),
);

const recommendedRules = Object.entries(rules)
.filter(([, rule]) => rule.meta.docs.recommended)
.reduce(
(acc, [name, rule]) => ({
...acc,
[`jest/${name}`]: rule.meta.docs.recommended,
}),
{},
);
const recommendedRules = Object.fromEntries(
Object.entries(rules)
.filter(([, rule]) => rule.meta.docs.recommended)
.map(([name, rule]) => [
`jest/${name}`,
rule.meta.docs.recommended as TSESLint.Linter.RuleLevel,
]),
);

const allRules = Object.entries(rules)
.filter(([, rule]) => !rule.meta.deprecated)
.reduce(
(acc, [name]) => ({
...acc,
[`jest/${name}`]: 'error',
}),
{},
);
const allRules = Object.fromEntries<TSESLint.Linter.RuleLevel>(
Object.entries(rules)
.filter(([, rule]) => !rule.meta.deprecated)
.map(([name]) => [`jest/${name}`, 'error']),
);

const createConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({
plugins: ['jest'],
Expand Down

0 comments on commit f3cb13b

Please sign in to comment.