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: fork json schema types for better compat with ESLint rule validation #6963

Merged
merged 5 commits into from Jun 17, 2023

Conversation

bradzacher
Copy link
Member

BREAKING CHANGE:
@typescript-eslint/utils now only exports the v4 JSON schema types - the only version ESLint validates rules with.
Removed the unsafe [string]: any indexer from the JSONSchema4 type to help ensure invalid properties aren't used.


This was something I noticed whilst working on our schema enhancements - we re-exported all these types that were mostly useless for lint rules because ESLint is hard-locked to v4 of the schema. So I decided to remove those newer schema types from the export.

I was going to leave it there, but then I remember that I had added a local patch that strictified the types by removing the unsafe indexer (which helped us catch bugs in our schemas). So I decided we can just fork the types entirely and apply my patch so all consumers get that same benefit.

@bradzacher bradzacher added enhancement New feature or request breaking change This change will require a new major version to be released labels Apr 27, 2023
@bradzacher bradzacher added this to the 6.0.0 milestone Apr 27, 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 27, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 96e019c
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/648d1262111e1c000823d6ba
😎 Deploy Preview https://deploy-preview-6963--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 27, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 96e019c. 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 31 targets

Sent with 💌 from NxCloud.

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.

🔥 looks great! I wonder if we could export this as our own types so other ESLint projects (maybe ESLint itself, if they want a devDependency to help with types) could use it? Given that it's already exported by @typescript-eslint/utils I think it's fine to stay as-is.

packages/utils/src/json-schema.ts Outdated Show resolved Hide resolved
@bradzacher
Copy link
Member Author

@JoshuaKGoldberg / @armano2 I just pushed a new commit (178cf35) that further strictifies the types to a discriminated union.
This allows us to use types to validate that you don't provide mismatched properties.
This caught a bug in two of our schema defs:

The downside to doing this is that you can be significantly less flexible in how you write schemas. For reference, you now must always define a type for each schema so that it can correctly pick the type, and it restricts some funky combos like adding anyOf + $ref + any concrete type.
Given that we always want to represent schemas as types - I don't think this is necessarily a bad thing though? Across our codebase we didn't end up changing anything aside from adding type in a few missing places.

If you guys think it's better to just stick to the spec and be loose - more than happy to revert the schema changes.

It's really hard to judge the impact of such a change, given that we don't know how all our consumers define schemas - but I'd think they're not too crazy?

@bradzacher bradzacher force-pushed the v6-remove-newer-json-schema-types branch from 178cf35 to 08bceb7 Compare April 28, 2023 01:13
@bradzacher bradzacher force-pushed the v6-remove-newer-json-schema-types branch from 08bceb7 to 24a27af Compare April 28, 2023 01:13
@JoshuaKGoldberg
Copy link
Member

Oof this hurts me though 😞. It seems like it'd be very annoying to have to write out the type when it can be inferred:

arrayOption: {
    type: 'string',
    enum: ['array', 'generic', 'array-simple'],
},

Maybe there's a happy medium we can go for? Only require type when it's not directly inferable?

@bradzacher
Copy link
Member Author

It seems like it'd be very annoying to have to write out the type when it can be inferred

The flip side is that the explicit type will prevent cases like enum: [1, 2, "3"] that would otherwise make the 3rd option inaccessible.

@codecov
Copy link

codecov bot commented Jun 17, 2023

Codecov Report

Merging #6963 (24a27af) into v6 (1366ae1) will increase coverage by 0.10%.
The diff coverage is 100.00%.

❗ Current head 24a27af differs from pull request most recent head 96e019c. Consider uploading reports for the commit 96e019c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##               v6    #6963      +/-   ##
==========================================
+ Coverage   87.50%   87.61%   +0.10%     
==========================================
  Files         376      375       -1     
  Lines       12937    12923      -14     
  Branches     3821     3821              
==========================================
+ Hits        11321    11322       +1     
+ Misses       1231     1216      -15     
  Partials      385      385              
Flag Coverage Δ
unittest 87.61% <100.00%> (+0.10%) ⬆️

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% <ø> (ø)
...t-plugin/src/rules/class-literal-property-style.ts 100.00% <ø> (ø)
packages/eslint-plugin/src/rules/comma-dangle.ts 93.33% <ø> (ø)
...lugin/src/rules/consistent-generic-constructors.ts 90.69% <ø> (ø)
...lugin/src/rules/consistent-indexed-object-style.ts 92.06% <ø> (ø)
...int-plugin/src/rules/consistent-type-assertions.ts 92.59% <ø> (ø)
...nt-plugin/src/rules/consistent-type-definitions.ts 97.29% <ø> (ø)
...eslint-plugin/src/rules/consistent-type-imports.ts 94.50% <ø> (ø)
...-plugin/src/rules/explicit-member-accessibility.ts 96.10% <ø> (-0.05%) ⬇️
... and 33 more

... and 7 files with indirect coverage changes

@JoshuaKGoldberg JoshuaKGoldberg merged commit a4967f2 into v6 Jun 17, 2023
44 checks passed
@JoshuaKGoldberg JoshuaKGoldberg deleted the v6-remove-newer-json-schema-types branch June 17, 2023 02:14
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking change This change will require a new major version to be released enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants