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: enable `prefer-ast-types-enum` internal rule #1514

Merged

Conversation

@G-Rath
Copy link
Contributor

G-Rath commented Jan 24, 2020

This is based off #1508, so shouldn't be merged until after that one has.

@typescript-eslint

This comment has been minimized.

Copy link

typescript-eslint bot commented Jan 24, 2020

Thanks for the PR, @G-Rath!

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.

@G-Rath

This comment has been minimized.

Copy link
Contributor Author

G-Rath commented Jan 24, 2020

Looking over the diff, there are some pretty decent ones in there that have been caught & fixed.

constructor is the only "ehhh" one that looks like it could be considered overzealous.

Co-Authored-By: Brad Zacher <brad.zacher@gmail.com>
@G-Rath G-Rath force-pushed the G-Rath:enable-prefer-ast-types-internal-rule branch from 69cd1fe to 12ec7cc Jan 24, 2020
@G-Rath G-Rath force-pushed the G-Rath:enable-prefer-ast-types-internal-rule branch from 12ec7cc to c6e5e06 Jan 24, 2020
@G-Rath

This comment has been minimized.

Copy link
Contributor Author

G-Rath commented Jan 24, 2020

@armano2 now this should be good & ready for review :)

@G-Rath G-Rath requested a review from armano2 Jan 24, 2020
@G-Rath G-Rath force-pushed the G-Rath:enable-prefer-ast-types-internal-rule branch from c6e5e06 to fb6b0fa Jan 24, 2020
Copy link
Member

bradzacher left a comment

Almost all changes are correct.
If you undo the changes to the files I've marked, and disable with a comment, I'm happy to merge this.

We could probs improve it with type info, but it's probs not worth it. The cases are pretty edge-casey, and fine for the internal usage.
The rule in #315 will cover this so I'm happy with a few disable comments until we get around to finishing that rule.

I should probably prioritise finishing some of those "abandoned" PRs.

@G-Rath

This comment has been minimized.

Copy link
Contributor Author

G-Rath commented Jan 24, 2020

👍

I should probably prioritise finishing some of those "abandoned" PRs.

I'm happy to help out with this - I've already got a couple on my plate that I'd like to action but they're all somewhat complicated or have high depth, so I'm keen for some lighter-weight contributions to sink my teeth into for upskilling 🙂

(In particular, learning how to use the types provided by TypeScript properly)

@G-Rath G-Rath force-pushed the G-Rath:enable-prefer-ast-types-internal-rule branch from 1fbfd9e to 68bf5d9 Jan 24, 2020
`,
errors: [
{
data: { enumName: 'AST_NODE_TYPES', literal: AST_NODE_TYPES.Literal },

This comment has been minimized.

Copy link
@G-Rath

G-Rath Jan 24, 2020

Author Contributor

ah haha hoisted by my own petard!

@G-Rath G-Rath force-pushed the G-Rath:enable-prefer-ast-types-internal-rule branch from 68bf5d9 to 3d94b2c Jan 24, 2020
@armano2

This comment was marked as resolved.

Copy link
Member

armano2 commented Jan 25, 2020

bdw. what do you think about changing this validation to something like this

function checkNodes(
  node: TSESTree.Node,
  literal: TSESTree.Node,
): boolean {
  switch (node.type) {
    case AST_NODE_TYPES.MemberExpression:
    case AST_NODE_TYPES.OptionalMemberExpression:
      return checkNodes(node.property, literal);
    case AST_NODE_TYPES.BinaryExpression:
      return node.right === literal ? checkNodes(node.left, literal) : false;
    case AST_NODE_TYPES.Property:
      return node.value === literal ? checkNodes(node.key, literal) : false;
    case AST_NODE_TYPES.SwitchCase:
      return node.test === literal;
    case AST_NODE_TYPES.TSAsExpression:
      return checkNodes(node.parent!, node);
    case AST_NODE_TYPES.Identifier:
      return node.name === 'type' || node.name === 'node';
    case AST_NODE_TYPES.ArrayExpression:
    case AST_NODE_TYPES.TSTypeReference:
      return true;
  }
  return false;
}

// ...

      Literal(node: TSESTree.Literal): void {
        if (
          !isStringLiteral(node) ||
          !node.parent ||
          !checkNodes(node.parent, node)
        ) {
          return;
        }

this should eliminate most of false positives

@G-Rath G-Rath force-pushed the G-Rath:enable-prefer-ast-types-internal-rule branch from 3d94b2c to 4ce9b3a Jan 25, 2020
@G-Rath

This comment has been minimized.

Copy link
Contributor Author

G-Rath commented Jan 25, 2020

what do you think about changing this validation to something like this

I think that while cool it's probably worth focusing our efforts on getting #315 across the line before iterating any further on this rule, as I think we've got the majority of the false false positives, and it'll also make for a good double-blind test for that rule :)

@G-Rath G-Rath requested a review from armano2 Jan 25, 2020
Copy link
Member

armano2 left a comment

LGTM

Copy link
Member

bradzacher left a comment

legendary, thanks!

@bradzacher bradzacher changed the title Enable prefer ast types internal rule chore: enable `prefer-ast-types-enum` internal rule Jan 25, 2020
@bradzacher bradzacher added the tests label Jan 25, 2020
@bradzacher bradzacher merged commit afa7900 into typescript-eslint:master Jan 25, 2020
7 checks passed
7 checks passed
Semantic Pull Request ready to be squashed
Details
codecov/patch Coverage not affected when comparing de9ca75...95fc985
Details
codecov/project 95.57% remains the same compared to de9ca75
Details
typescript-eslint.typescript-eslint Build #20200125.7 succeeded
Details
typescript-eslint.typescript-eslint (Primary code validation and tests) Primary code validation and tests succeeded
Details
typescript-eslint.typescript-eslint (Run unit tests on other Node.js versions node_10_x) Run unit tests on other Node.js versions node_10_x succeeded
Details
typescript-eslint.typescript-eslint (Run unit tests on other Node.js versions node_8_x) Run unit tests on other Node.js versions node_8_x succeeded
Details
@G-Rath G-Rath deleted the G-Rath:enable-prefer-ast-types-internal-rule branch Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.