-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [no-magic-numbers] handle UnaryExpression for enums #1415
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
Conversation
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
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 Report
@@ 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
|
*/ | ||
function getLiteralParent(node: TSESTree.Literal): TSESTree.Node | undefined { | ||
if ( | ||
node.parent?.type === AST_NODE_TYPES.UnaryExpression && |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
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: