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?
Changes from 12 commits
f2c6548
3465df0
32d4165
9018532
fc04de6
88d2280
4a99c2a
4ae24a2
844d483
0ea8000
eb44ba7
ecee17c
25e6afd
a5ac70f
040d77c
b6856ed
d59ac20
5e77d12
4161d14
aa96591
e2c3497
b392497
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ const messages = { | |
forbiddenValue: '"{{value}}" is an invalid value for button type attribute', | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
|
@@ -149,9 +150,11 @@ module.exports = { | |
} | ||
|
||
const props = node.arguments[1].properties; | ||
const typeProp = props.find((prop) => prop.key && prop.key.name === 'type'); | ||
const typeProp = props.find( | ||
(prop) => prop.type === 'Property' && prop.key && prop.key.type === 'Identifier' && prop.key.name === 'type' | ||
); | ||
|
||
if (!typeProp) { | ||
if (typeProp.type !== 'Property') { | ||
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. test? 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've added tests in b6856ed. What do you think? |
||
reportMissing(node); | ||
return; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ const messages = { | |
preferFragment: 'Prefer fragment shorthand over {{react}}.{{fragment}}', | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
|
@@ -171,7 +172,7 @@ module.exports = { | |
ImportDeclaration(node) { | ||
if (node.source && node.source.value === 'react') { | ||
node.specifiers.forEach((spec) => { | ||
if (spec.imported && spec.imported.name === fragmentPragma) { | ||
if (spec.type === 'ImportSpecifier' && spec.imported && spec.imported.name === fragmentPragma) { | ||
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. why is this needed? what are the possible 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.
It's I added it because only ImportSpecifier has the imported property. 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. would 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. Yes, it will be narrowed down. Alternatively, it might be better to do it as follows. if (spec.type === 'ImportSpecifier' && spec.imported.name === fragmentPragma) { ... |
||
if (spec.local) { | ||
fragmentNames.add(spec.local.name); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ const messages = { | |
func: 'JSX props should not use functions', | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
|
@@ -122,8 +123,8 @@ module.exports = { | |
blockVariableNameSets[blockStart][violationType].add(variableName); | ||
} | ||
|
||
function getBlockStatementAncestors(node) { | ||
return context.getAncestors(node).filter( | ||
function getBlockStatementAncestors() { | ||
return context.getAncestors().filter( | ||
Comment on lines
+126
to
+127
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'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 commentThe reason will be displayed to describe this comment to others. Learn more. Sorry. I do not understand it well.
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, 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 commentThe 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[] |
||
(ancestor) => ancestor.type === 'BlockStatement' | ||
).reverse(); | ||
} | ||
|
@@ -145,7 +146,7 @@ module.exports = { | |
} | ||
|
||
function findVariableViolation(node, name) { | ||
getBlockStatementAncestors(node).find( | ||
getBlockStatementAncestors().find( | ||
(block) => reportVariableViolation(node, name, block.range[0]) | ||
); | ||
} | ||
|
@@ -156,7 +157,7 @@ module.exports = { | |
}, | ||
|
||
FunctionDeclaration(node) { | ||
const blockAncestors = getBlockStatementAncestors(node); | ||
const blockAncestors = getBlockStatementAncestors(); | ||
const variableViolationType = getNodeViolationType(node); | ||
|
||
if (blockAncestors.length > 0 && variableViolationType) { | ||
|
@@ -168,13 +169,15 @@ module.exports = { | |
if (!node.init) { | ||
return; | ||
} | ||
const blockAncestors = getBlockStatementAncestors(node); | ||
const blockAncestors = getBlockStatementAncestors(); | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
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. 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 commentThe 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 commentThe 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 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 didn't know that method! Thank you! Fixed in e2c3497 . |
||
&& node.parent.kind === 'const' // only support const right now | ||
&& node.id.type === 'Identifier' | ||
) { | ||
addVariableNameToSet( | ||
variableViolationType, node.id.name, blockAncestors[0].range[0] | ||
|
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.
The type of node is not properly narrowed down.
It's
(parameter) node: CallExpression & Rule.NodeParentExtension
.