-
Notifications
You must be signed in to change notification settings - Fork 237
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
chore: build recommended ruleset based on rules meta doc property #615
chore: build recommended ruleset based on rules meta doc property #615
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay 🚀
Looking over our tests I think some might be a bit overkill - I don't mind per say, but am conscientious of how many places changes have to happen i.e snapshotting + updating the rule count constant.
Rule count is to make sure we don't miss any when exporting, which happened at least twice. Changing a number in a test is way better than shipping broken code
0b3535d
to
c33607f
Compare
import globals from './globals.json'; | ||
import * as snapshotProcessor from './processors/snapshot-processor'; | ||
|
||
type RuleModule = TSESLint.RuleModule<string, unknown[]> & { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use this type for all rules where we declare them as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you mean?
Ideally we should try and use the inferred type that's returned from createRule
as much as possible for each rule, as that has the proper types for their message ids & options.
We could switch to using a barrel export like @typescript-eslint
does, where we have index.ts
that explicitly exports each rule:
import noIfs from './no-ifs';
export default {
'no-ifs': noIfs,
}
That would let us import all the rules here as a single default import.
we can even script it
#!/usr/bin/env ts-node-transpile-only
import * as fs from 'fs';
import * as path from 'path';
import prettier from 'prettier';
import { prettier as prettierConfig } from '../package.json';
const toCamelCase = (str: string) => {
const words = str.split('-');
return [
words.shift(),
...words.map(w => w.substring(0, 1).toUpperCase() + w.substring(1)),
].join('');
};
const excludedFiles = ['__tests__', 'utils', 'index'];
const ruleFiles = fs
.readdirSync(path.resolve(__dirname, '../src/rules'))
.map(file => path.parse(file).name)
.filter(file => !excludedFiles.includes(file));
const contents = [
...ruleFiles.map(rule => `import ${toCamelCase(rule)} from './${rule}';`),
'',
'export default {',
...ruleFiles.map(rule => ` '${rule}': ${toCamelCase(rule)},`),
'};', //
].join('\n');
fs.writeFileSync(
path.resolve(__dirname, '../src/rules/index.ts'),
prettier.format(contents, { parser: 'typescript', ...prettierConfig }),
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'm going to merge this, but happy to continue this discussion 🙂)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant doing something here so we use the same type:
eslint-plugin-jest/src/rules/utils.ts
Lines 12 to 16 in 75f8897
export const createRule = ESLintUtils.RuleCreator(name => { | |
const ruleName = parsePath(name).name; | |
return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`; | |
}); |
Not sure how feasible it is, tho?
🎉 This PR is included in version 23.18.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
While we don't change the recommended ruleset often, this ensures we'll keep the right properties up to date.
Looking over our tests I think some might be a bit overkill - I don't mind per say, but am conscientious of how many places changes have to happen i.e snapshotting + updating the rule count constant.