-
Notifications
You must be signed in to change notification settings - Fork 243
ts-migration/prefer-called-with #346
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,14 @@ export const createRule = ESLintUtils.RuleCreator(name => { | |
| return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`; | ||
| }); | ||
|
|
||
| interface JestExpectIdentifier extends TSESTree.Identifier { | ||
| name: 'expect'; | ||
| interface JestSpecificIdentifier<Name extends string> | ||
| extends TSESTree.Identifier { | ||
| name: Name; | ||
| } | ||
|
|
||
| type JestExpectIdentifier = JestSpecificIdentifier<'expect'>; | ||
| type JestNotIdentifier = JestSpecificIdentifier<'not'>; | ||
|
|
||
| /** | ||
| * Checks if the given `node` is considered a {@link JestExpectIdentifier}. | ||
| * | ||
|
|
@@ -67,6 +71,16 @@ interface JestExpectCallWithParent extends JestExpectCallExpression { | |
| parent: JestExpectCallMemberExpression; | ||
| } | ||
|
|
||
| interface JestNotNamespaceMemberExpression | ||
| extends JestExpectCallMemberExpression { | ||
| object: JestExpectCallExpression; | ||
| property: JestNotIdentifier; | ||
| } | ||
|
|
||
| interface JestExpectCallWithNotParent extends JestExpectCallWithParent { | ||
| parent: JestNotNamespaceMemberExpression; | ||
| } | ||
|
|
||
| export const isExpectCallWithParent = ( | ||
| node: TSESTree.Node, | ||
| ): node is JestExpectCallWithParent => | ||
|
|
@@ -75,6 +89,27 @@ export const isExpectCallWithParent = ( | |
| node.parent.type === AST_NODE_TYPES.MemberExpression && | ||
| node.parent.property.type === AST_NODE_TYPES.Identifier; | ||
|
|
||
| /** | ||
| * Checks if the given `node` is a {@link JestExpectCallWithNotParent}. | ||
| * | ||
| * This is any call to `expect` that is followed by the `not` namespace: | ||
| * | ||
| * @example``` | ||
| * expect('a').not.toBe('b'); | ||
| * expect().not | ||
| * ``` | ||
| * | ||
| * @param {Node} node | ||
| * | ||
| * @return {node is JestExpectCallWithNotParent} | ||
| */ | ||
| export const isExpectCallWithNot = ( | ||
| node: TSESTree.Node, | ||
| ): node is JestExpectCallWithNotParent => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe? iirc I don't think b/c the parent is the I've actually spent the whole day pretty much writing a completely typesafe implementation of the guards that are also completely compatible w/ identifiers, literals and template literals, and am now converting the remaining rules right now. I hope to push them up pretty soon, but they're still somewhat messy. There was always going to be naming conflicts unfortunately. Just to give you a little taste of the new guards in action:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh looks nice.. Ok, no worries. Keep doing what you think is the best. As long as @SimenB is ok with these changes, later I can refactor my PR accordingly. 🖖
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem :) Your PR was a huge help, as it helped confirm a lot of things I wanted to confirm before trying to rewrite all the guards. Would it be ok if I pulled your PR into mine? Just in case I need to to hit 100% coverage & ensure the guards are all correct. I'm definitely going to want as many eyes as I can on it, as there's definitely redundancy, so I'll make sure to ping you when I push up a draft PR (which I might be doing very soon).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really have an opinion either way. In general I'd like to avoid the Anyways, rambling way of saying "these names are fine for now, I'd like to revisit after migration is complete to see if we can simplify" 🙂
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I completely agree, and just you wait until you see some of the new names :D Tbh, while the names seem to go against what we've taught as programmers, in AST land they're actually really nice - things like I do totally agree w/ the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure, I think we should keep the verbose names as they more directly map to the underlying AST nodes we operate on. But down the line we can create more human readable abstractions on top of it to easily make assertions about different matchers (while still keeping the verbose versions as the implementation)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's exactly the right way to go about it, and what I've been working towards. It's already how we're doing accessors now: |
||
| isExpectCallWithParent(node) && | ||
| node.parent.object.type === AST_NODE_TYPES.CallExpression && | ||
| node.parent.property.name === 'not'; | ||
|
|
||
| export enum DescribeAlias { | ||
| 'describe' = 'describe', | ||
| 'fdescribe' = 'fdescribe', | ||
|
|
||
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.
could possibly have a version with this that receives a
JestExpectCallWithParentwhich just checks the parent? in this rule, we callisExpectCallWithParenttwice. Probably not a big issue as the checks are pretty cheap