Skip to content

chore(utils): use Extract generic for ast-utils' predicates' helper functions #4545

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

Merged
merged 10 commits into from
Aug 25, 2022

Conversation

MichaelDeBoey
Copy link
Contributor

@MichaelDeBoey MichaelDeBoey commented Feb 12, 2022

@nx-cloud
Copy link

nx-cloud bot commented Feb 12, 2022

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 8def652. 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 47 targets

Sent with 💌 from NxCloud.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @MichaelDeBoey!

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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@netlify
Copy link

netlify bot commented Feb 12, 2022

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 8def652
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/630381704f3d8e0009407aa6
😎 Deploy Preview https://deploy-preview-4545--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.

@bradzacher bradzacher added the refactor PRs that refactor code only label Feb 14, 2022
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.

Extract is indeed neat, but this is a few extra characters per line for... what is the benefit here?

Happy to approve if there's some reason for it that can be shown. 🙂

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Feb 23, 2022
@MichaelDeBoey
Copy link
Contributor Author

what is the benefit here?

@JoshuaKGoldberg As seen in the mentioned tweets, this wil result in a nicer type & cleaner error messages

type A = { name: 'A', lowercase: 'a' };
type B = { name: 'B', lowercase: 'b' };

type AOrB = A | B;

type AWithExact = Exact<AOrB, { name: 'A' }>;
// type A

type AWithoutExact = AOrB & { name: 'A' };
// { name: 'A', lowercase: 'a' } & { name: 'A' } | { name: 'B', lowercase: 'b' } & { name: 'A' }
// which results in { name: 'A', lowercase: 'a' } & { name: 'A' } | never
// or just { name: 'A', lowercase: 'a' } & { name: 'A' }
// which is the same as { name: 'A', lowercase: 'a' }
// which is the same as type A

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.

Cool, thanks - that makes sense!

Maybe we should include a lint rule asking for this preference?

@MichaelDeBoey
Copy link
Contributor Author

Maybe we should include a lint rule asking for this preference?

@JoshuaKGoldberg I think this can indeed be a great addition to this package 👍

@MichaelDeBoey
Copy link
Contributor Author

It seems like type checks are failing, but I can't figure out why exactly tbh.

@farskid @Beraliv Do one of you guys have any pointers for me please?

CC/ @bradzacher

@bradzacher
Copy link
Member

  Overload 1 of 2, '(predicate: (value: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>, index: number, array: ObjectEntry<...>[]) => value is ObjectEntry<...>, thisArg?: any): this is ObjectEntry<...>[]', gave the following error.
    Argument of type '([key, value]: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>) => boolean' is not assignable to parameter of type '(value: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>, index: number, array: ObjectEntry<...>[]) => value is ObjectEntry<...>'.
      Signature '([key, value]: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>): boolean' must be a type predicate.
  Overload 2 of 2, '(predicate: (value: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>, index: number, array: ObjectEntry<...>[]) => unknown, thisArg?: any): boolean', gave the following error.
    Argument of type '([key, value]: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>) => boolean' is not assignable to parameter of type '(value: ObjectEntry<Extract<BooleanToken, { type: TokenType; }> | Extract<BlockComment, { type: TokenType; }> | Extract<LineComment, { ...; }> | ... 9 more ... | Extract<...>>, index: number, array: ObjectEntry<...>[]) => unknown'.
      Types of parameters '__0' and 'value' are incompatible.
        Type 'ObjectEntry<Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BooleanToken, { type: TokenType; }> | Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BlockComment, { ...; }> | ... 10 more ... | Extract<...>>' is not assignable to type 'ObjectEntry<Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BooleanToken, { type: TokenType; }> | Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BlockComment, { ...; }> | ... 10 more ... | Extract<...>>'. Two different types with this name exist, but they are unrelated.
          Type at position 1 in source is not compatible with type at position 1 in target.
            Type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>] | ... 11 more ... | Extract<...>[keyof Extract<...> & ... 11 more ... & keyof Extract<...>]' is not assignable to type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>] & ... 11 more ... & Extract<...>[keyof Extract<...> & ... 11 more ... & keyof Extract<...>]'.
              Type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>]' is not assignable to type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>] & ... 11 more ... & Extract<...>[keyof Extract<...> & ... 11 more ... & keyof Extract<...>]'.
                Type 'Extract<BooleanToken, { type: TokenType; }>[string] | Extract<BooleanToken, { type: TokenType; }>[number] | Extract<...>[symbol]' is not assignable to type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>] & ... 11 more ... & Extract<...>[keyof Extract<...> & ... 11 more ... & keyof Extract<...>]'.
                  Type 'Extract<BooleanToken, { type: TokenType; }>[string]' is not assignable to type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>] & ... 11 more ... & Extract<...>[keyof Extract<...> & ... 11 more ... & keyof Extract<...>]'.
                    Type 'Extract<BooleanToken, { type: TokenType; }>[string]' is not assignable to type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>]'.
                      Type 'string' is not assignable to type 'keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<BlockComment, { type: TokenType; }> & ... 10 more ... & keyof Extract<...>'.
                        Type 'string' is not assignable to type 'keyof Extract<BooleanToken, { type: TokenType; }>'.
                          Type 'string' is not assignable to type 'keyof BooleanToken'.
                            Type 'Extract<BooleanToken, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>]' is not assignable to type 'Extract<BlockComment, { type: TokenType; }>[keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<...> & ... 10 more ... & keyof Extract<...>]'.
                              Type 'Extract<BooleanToken, { type: TokenType; }>' is not assignable to type 'Extract<BlockComment, { type: TokenType; }>'.
                                Type '{ type: TokenType; } & BooleanToken' is not assignable to type 'Extract<BlockComment, { type: TokenType; }>'.
Error: src/ast-utils/helpers.ts(57,37): error TS2536: Type 'keyof Extract<BooleanToken, { type: TokenType; }> & keyof Extract<BlockComment, { type: TokenT

that is a truly gnarly error message lol

@bradzacher
Copy link
Member

From the error...

Type 'ObjectEntry<Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BooleanToken, { type: TokenType; }> | Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BlockComment, { ...; }> | ... 10 more ... | Extract<...>>' is not assignable to type 'ObjectEntry<Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BooleanToken, { type: TokenType; }> | Extract<import("/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec").BlockComment, { ...; }> | ... 10 more ... | Extract<...>>'. Two different types with this name exist, but they are unrelated.

is TS doing something weird here? why are there duplicate named types it's referencing?

@MichaelDeBoey
Copy link
Contributor Author

@bradzacher I've reverted all the changes except for isNodeOfType (for now), so we'll have less possible errors

@bradzacher
Copy link
Member

Just one error now which is nowhere near as gnarly!

src/ast-utils/helpers.ts(6,14): error TS4023: Exported variable 'isNodeOfType' has or is using name 'TSIntrinsicKeyword' from external module "/home/runner/work/typescript-eslint/typescript-eslint/packages/types/dist/ast-spec" but cannot be named.

@MichaelDeBoey
Copy link
Contributor Author

@bradzacher Do you happen to know how I could fix it?
I really have no clue tbh 😕

@bradzacher
Copy link
Member

bradzacher commented Mar 7, 2022

From what I can tell - the issue is that TSIntrinsicKeyword is not exported from the ast-spec package, so it's not a publically available type - so TS doesn't like it.

Make the following two changes:

  1. Rename this file packages/ast-spec/src/type/TSIntrinsicType/spec.ts to packages/ast-spec/src/type/TSIntrinsicKeyword/spec.ts
  2. make sure it's exported from packages/ast-spec/src/type/spec.ts (i.e. export * from './TSIntrinsicKeyword/spec')

A TODO here would be for us to add a test to ensure that all types named within our Node type are exported from ast-spec. We would probably have to use the TS compiler API to properly verify this.

@MichaelDeBoey
Copy link
Contributor Author

@bradzacher Those changes seem to be fixing the issues indeed
https://github.com/typescript-eslint/typescript-eslint/runs/5442617641

I'll update all other helper functions again, so we can see if that was the only problem or not.

A TODO here would be for us to add a test to ensure that all types named within our Node type are exported from ast-spec. We would probably have to use the TS compiler API to properly verify this.

I have the feeling that with the changes in this PR in place, CI will automatically fail again whenever we don't export everything correctly.
But a specific test for it won't hurt & will probably point into the right direction sooner.

@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Jul 2, 2022
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

lgtm - just one comment

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Aug 21, 2022
@bradzacher bradzacher merged commit 4483a4b into typescript-eslint:main Aug 25, 2022
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Aug 25, 2022
@MichaelDeBoey MichaelDeBoey deleted the patch-52 branch August 25, 2022 20:00
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
refactor PRs that refactor code only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants