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

fix(eslint-plugin): [no-magic-numbers] handle UnaryExpression for enums #1415

Merged
merged 1 commit into from Jan 8, 2020

Conversation

bradzacher
Copy link
Member

Fixes #1414

Also fixes a consistency issue wherein our extension would include the + when reporting, but the base rule doesn't.
Also reformatted the code a bit:

  • moved utility functions outside of create
  • used optional chaining

Fixes #1414

Also fixes a consistency issue wherein our extension would include the `+` when reporting, but the base rule doesn't.
Also reformatted the code a bit:
- moved utility functions outside of create
- used optional chaining
@bradzacher bradzacher added the bug Something isn't working label Jan 8, 2020
@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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Jan 8, 2020

Codecov Report

Merging #1415 into master will decrease coverage by 0.1%.
The diff coverage is 71.42%.

@@            Coverage Diff             @@
##           master    #1415      +/-   ##
==========================================
- Coverage   94.58%   94.48%   -0.11%     
==========================================
  Files         140      140              
  Lines        6057     6055       -2     
  Branches     1723     1721       -2     
==========================================
- Hits         5729     5721       -8     
  Misses        181      181              
- Partials      147      153       +6
Impacted Files Coverage Δ
...ckages/eslint-plugin/src/rules/no-magic-numbers.ts 82.35% <71.42%> (-11.99%) ⬇️

*/
function getLiteralParent(node: TSESTree.Literal): TSESTree.Node | undefined {
if (
node.parent?.type === AST_NODE_TYPES.UnaryExpression &&
Copy link
Member

@armano2 armano2 Jan 8, 2020

Choose a reason for hiding this comment

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

hmm, i was thinking what to do with node.parent?

parent is always defined in eslint context for nodes (with exception for Program).

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah we should probably fix the types.
I was considering just adding a new type to ts-estree:

type KnownParent<TNode extends Node, TParent extends Node> = TNode & { parent: TParent };

But that type wouldn't doesn't fix cases like this. It would let us express direct parent selectors like VariableDeclaration > VariableDeclarator, so it's probably a nice thing to add as a non-breaking feature.


I was considering doing this via generics to make it even more explicit. But this only works if we make it non-optional.

interface BaseNode<TParent extends Node = Node> {
  // ...
  parent: TParent;
}

interface VariableDeclarator<TParent extends Node = Node> extends BaseNode<TParent> {}

interface Program extends BaseNode {
  parent?: never;
}

The problem with making it non-optional is that this complicates the converter code because the converter isn't the one that adds the parent, eslint is.

Copy link
Member

@armano2 armano2 Jan 8, 2020

Choose a reason for hiding this comment

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

q: what do you think maybe about adding d.ts with augmentation for type in eslint-utils or plugin itself?

import { Node } from "@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree";

declare module '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree' {
  export interface BaseNode {
    parent: Node;
  }

  export interface Program {
    parent: never;
  }
}

and dropping parent from ts-estree completely?


this is just an idea (should work, but i haven't tested it)

@bradzacher bradzacher merged commit 852fc31 into master Jan 8, 2020
@bradzacher bradzacher deleted the 1414-unary-in-enum branch January 8, 2020 22:30
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-magic-numbers] False positive on negative numbers in enums w/ ignoreEnums flag on
2 participants