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

feat: improve rule schemas, add test to validate schemas, add tooling to generate schema types #6899

Merged
merged 18 commits into from Apr 20, 2023

Conversation

bradzacher
Copy link
Member

@bradzacher bradzacher commented Apr 13, 2023

Overview

@JoshuaKGoldberg I went through with my threat for no reason other than it seemed fun.

Previously we used json-schema-to-typescript to generate our types, however we have found that the package has a number of edge-cases that don't do what we want. Attempting to change the package is... difficult because it is built to be super flexible so it can attempt to handle every single permutation and valid state of a JSON schema.

However in reality we don't actually need all that power - just a fraction of it because rule schemas are some degree of sane (usually... at least our are). This means we can easily constrain the transformation code and thus simplify it significantly.

This PR:

  1. introduces a new (currently internal) package that contains tooling to convert a rule's schema to TypeScript types.
  2. introduces snapshot tests for the plugin that snapshots the schema and the generated TS types
    • I realised that running this against our rules is a great way for us to validate our schemas are doing what we think they are - I personally find that seeing the fully resolved schema along side the computed TS types are incredibly helpful in understanding the schemas.
      This has the added benefit that we can validate the types that will get embedded in the website without needing to do a website build - which is a nice, quick sanity check.
  3. introduces a test for the plugin that validates that the schemas don't include invalid properties
    • Whilst building the tool I noticed a lot of invalid states in the schema, so I threw together a test to shout about unknown properties. This in turn helped me find a bunch of incorrect schemas!
  4. updates the website build to use the new package

Note: I purposely haven't written tests for the tool as it's pretty extensively tested by running on our rules already. I didn't see any need in additional tests.

@bradzacher bradzacher added enhancement New feature or request tests anything to do with testing repo maintenance things to do with maintenance of the repo, and not with code/docs labels Apr 13, 2023
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @bradzacher!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Apr 13, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit f113341
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/644094a9ecfed60008f056b0
😎 Deploy Preview https://deploy-preview-6899--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@nx-cloud
Copy link

nx-cloud bot commented Apr 13, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit f113341. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 32 targets

Sent with 💌 from NxCloud.

@codecov
Copy link

codecov bot commented Apr 14, 2023

Codecov Report

Merging #6899 (f113341) into v6 (4bc7571) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##               v6    #6899      +/-   ##
==========================================
- Coverage   87.51%   87.50%   -0.01%     
==========================================
  Files         376      376              
  Lines       12940    12936       -4     
  Branches     3820     3820              
==========================================
- Hits        11324    11320       -4     
  Misses       1231     1231              
  Partials      385      385              
Flag Coverage Δ
unittest 87.50% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/eslint-plugin/src/rules/array-type.ts 97.14% <ø> (ø)
packages/eslint-plugin/src/rules/ban-ts-comment.ts 96.96% <ø> (ø)
...-plugin/src/rules/explicit-member-accessibility.ts 96.10% <ø> (-0.05%) ⬇️
...es/eslint-plugin/src/rules/lines-around-comment.ts 100.00% <ø> (ø)
packages/eslint-plugin/src/rules/no-type-alias.ts 100.00% <ø> (ø)
...lugin/src/rules/padding-line-between-statements.ts 93.83% <ø> (ø)
...es/eslint-plugin/src/rules/parameter-properties.ts 94.11% <ø> (ø)
...ackages/eslint-plugin/src/rules/prefer-readonly.ts 99.09% <ø> (ø)
...eslint-plugin/src/rules/type-annotation-spacing.ts 81.69% <ø> (-0.26%) ⬇️
packages/type-utils/src/isTypeReadonly.ts 88.88% <ø> (ø)
... and 7 more

@bradzacher bradzacher marked this pull request as ready for review April 14, 2023 03:52
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also what armano2 said 😄 - but this looks great so far! I'll take a deeper look upon re-request

@@ -71,6 +71,42 @@ See our docs on [type aware linting](./Typed_Linting.mdx) for more information.
You're using an outdated version of `@typescript-eslint/parser`.
Update to the latest version to see a more informative version of this error message, explained [above](#i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file 'backlink to I get errors telling me ESLint was configured to run ...').

## How do I turn on a `@typescript-eslint` rule?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant for another PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally added as part of cleaning up the holistic docs update in this PR.
We've seen this asked a few times and the ESLint team doesn't think their docs are unclear, so as part of improving the rule config docs - this FAQ article to quick-link people to.

packages/eslint-plugin/src/rules/array-type.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/ban-types.ts Show resolved Hide resolved
@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Apr 16, 2023
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 18, 2023
package.json Outdated Show resolved Hide resolved
Copy link
Member

@armano2 armano2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks awesome, thank you for you hard work

yarn.lock Outdated Show resolved Hide resolved
@bradzacher bradzacher merged commit acc1a43 into v6 Apr 20, 2023
45 of 46 checks passed
@bradzacher bradzacher deleted the v6-improved-schema-generation-and-validation branch April 20, 2023 01:57
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request repo maintenance things to do with maintenance of the repo, and not with code/docs tests anything to do with testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants