Skip to content
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

Merged
merged 1 commit into from Jul 5, 2020

Conversation

G-Rath
Copy link
Collaborator

@G-Rath G-Rath commented Jun 25, 2020

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.

@G-Rath G-Rath requested a review from SimenB June 25, 2020 04:45
Copy link
Member

@SimenB SimenB left a 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

src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
@G-Rath G-Rath force-pushed the build-recommended-rules-config-based-on-property branch from 0b3535d to c33607f Compare July 4, 2020 01:33
import globals from './globals.json';
import * as snapshotProcessor from './processors/snapshot-processor';

type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
Copy link
Member

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?

Copy link
Collaborator Author

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 }),
);

Copy link
Collaborator Author

@G-Rath G-Rath Jul 5, 2020

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 🙂)

Copy link
Member

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:

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?

@G-Rath G-Rath merged commit 94fa724 into master Jul 5, 2020
@G-Rath G-Rath deleted the build-recommended-rules-config-based-on-property branch July 5, 2020 09:58
@github-actions
Copy link

github-actions bot commented Jul 5, 2020

🎉 This PR is included in version 23.18.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants