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
Add JSDoc type annotations #3731
base: master
Are you sure you want to change the base?
Conversation
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.
this isn't just adding type annotations and a lot of the changes are potentially breaking.
lib/rules/button-has-type.js
Outdated
const typeProp = props.find( | ||
(prop) => prop.type === "Property" && prop.key && prop.key.type === "PrivateIdentifier" && prop.key.name === 'type' | ||
); | ||
|
||
if (!typeProp) { | ||
if (typeProp.type !== "Property") { |
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.
these changes aren't simple annotation changes. can they be extracted at least to a different commit, if not to a different PR?
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.
I see. My apologies 🙇♂️
Would it be acceptable to create a PR that adds annotations only to rules where type changes are unnecessary?
Or would it be better to add annotations to all rules, and if changes are needed, ignore the entire file (@ ts-nocheck)?
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.
yes, that would be fine.
any non-type changes that are needed must be accompanied by tests - if the ostensible type error can't be reproduced, then the type system is wrong.
lib/rules/forbid-elements.js
Outdated
const argType = argument.type; | ||
|
||
if (argType === 'Identifier' && /^[A-Z_]/.test(argument.name)) { | ||
if (argument.type === 'Identifier' && /^[A-Z_]/.test(argument.name)) { | ||
reportIfForbidden(argument.name, argument); | ||
} else if (argType === 'Literal' && /^[a-z][^.]*$/.test(argument.value)) { | ||
} else if (argument.type === 'Literal' && /^[a-z][^.]*$/.test(argument.value.toString())) { | ||
reportIfForbidden(argument.value, argument); | ||
} else if (argType === 'MemberExpression') { | ||
} else if (argument.type === 'MemberExpression') { |
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.
the original only has one observable lookup; this has 3. why is this an improvement?
also, String(x)
, never x.toString()
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.
the original only has one observable lookup; this has 3. why is this an improvement?
The reason is because the following error message occurs during the execution of the type-check script.
Property 'name' does not exist on type 'ClassExpression | ArrayExpression | ArrowFunctionExpression | AssignmentExpression | ... 23 more ... | SpreadElement'. Property 'name' does not exist on type 'ClassExpression'.
if (argType === 'Identifier' && /^[A-Z_]/.test(argument.name)) {
also, String(x), never x.toString()
thanks! Fixed in 4ae24a2 .
I have addressed this in pull request #3732 and will proceed to close this one. I'll handle the areas requiring type changes separately. |
I would prefer to update this PR with those changes rather than abandoning it. |
…into feature/add-jsdoc-annotations
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.
i only went like 20% through, but this should help you refine the entire thing
function getBlockStatementAncestors() { | ||
return context.getAncestors().filter( |
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.
i'm pretty sure this change will cause eslint 9 to break. can you confirm?
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.
Sorry. I do not understand it well.
Should I add v9 to eslint in package.json as shown below and see if I get a compile error?
"devDependencies": {
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9",
...
},
"peerDependencies": {
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9"
},
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.
No, adding eslint 9 is a hard task. What i mean is, what’s wrong with passing the node here?
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.
When looking at the type information for context.getAncestors, it seems that the argument is unnecessary.
(method) Rule.RuleContext.getAncestors(): Node[]
lib/rules/jsx-no-bind.js
Outdated
const variableViolationType = getNodeViolationType(node.init); | ||
|
||
if ( | ||
blockAncestors.length > 0 | ||
&& variableViolationType | ||
&& node.parent.type === 'VariableDeclaration' |
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.
we're already inside VariableDeclarator, this shouldn't be needed
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.
Property 'kind' does not exist on type 'Node'.
This error occurs.
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.
if this is just for narrowing, we can use type casts to tell TS what kind of thing it 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.
As far as I know, since it's a JavaScript file, I don't think type assertions can be used.
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.
they can, by wrapping the value in parens and preceding it with a @type
comment.
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.
I didn't know that method! Thank you! Fixed in e2c3497 .
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3731 +/- ##
==========================================
- Coverage 97.71% 97.69% -0.03%
==========================================
Files 133 133
Lines 9470 9507 +37
Branches 3469 3494 +25
==========================================
+ Hits 9254 9288 +34
- Misses 216 219 +3 ☔ View full report in Codecov by Sentry. |
@@ -149,9 +150,11 @@ module.exports = { | |||
} | |||
|
|||
const props = node.arguments[1].properties; |
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.
is the type of node
properly narrowed to a CallExpression node here? if not, let's do that as an annotation on the visitor function rather than needing to add all these runtime changes.
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.
I attempted to make corrections in 040d77c, but is my understanding correct?
Sorry. I didn't fully understand it.
is the type of node properly narrowed to a CallExpression node here?
The type of node is not properly narrowed down.
It's (parameter) node: CallExpression & Rule.NodeParentExtension
.
I have added type annotations and fixed associated type errors.