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

Add JSDoc type annotations #3731

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

y-hsgw
Copy link
Contributor

@y-hsgw y-hsgw commented Apr 9, 2024

I have added type annotations and fixed associated type errors.

Copy link
Member

@ljharb ljharb left a 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.

Comment on lines 153 to 157
const typeProp = props.find(
(prop) => prop.type === "Property" && prop.key && prop.key.type === "PrivateIdentifier" && prop.key.name === 'type'
);

if (!typeProp) {
if (typeProp.type !== "Property") {
Copy link
Member

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?

Copy link
Contributor Author

@y-hsgw y-hsgw Apr 9, 2024

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)?

Copy link
Member

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/checked-requires-onchange-or-readonly.js Outdated Show resolved Hide resolved
lib/rules/checked-requires-onchange-or-readonly.js Outdated Show resolved Hide resolved
Comment on lines 106 to 111
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') {
Copy link
Member

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()

Copy link
Contributor Author

@y-hsgw y-hsgw Apr 10, 2024

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 .

lib/rules/forbid-foreign-prop-types.js Outdated Show resolved Hide resolved
@ljharb ljharb marked this pull request as draft April 9, 2024 02:52
@y-hsgw
Copy link
Contributor Author

y-hsgw commented Apr 10, 2024

I have addressed this in pull request #3732 and will proceed to close this one. I'll handle the areas requiring type changes separately.

@y-hsgw y-hsgw closed this Apr 10, 2024
@y-hsgw y-hsgw deleted the feature/add-jsdoc-annotations branch April 10, 2024 05:45
@ljharb
Copy link
Member

ljharb commented Apr 10, 2024

I would prefer to update this PR with those changes rather than abandoning it.

@y-hsgw y-hsgw restored the feature/add-jsdoc-annotations branch April 10, 2024 06:10
@y-hsgw y-hsgw reopened this Apr 10, 2024
Copy link
Member

@ljharb ljharb left a 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

lib/rules/checked-requires-onchange-or-readonly.js Outdated Show resolved Hide resolved
lib/rules/forbid-foreign-prop-types.js Outdated Show resolved Hide resolved
lib/rules/forbid-prop-types.js Outdated Show resolved Hide resolved
lib/rules/jsx-closing-bracket-location.js Outdated Show resolved Hide resolved
lib/rules/jsx-curly-spacing.js Outdated Show resolved Hide resolved
lib/rules/jsx-indent.js Outdated Show resolved Hide resolved
Comment on lines +126 to +127
function getBlockStatementAncestors() {
return context.getAncestors().filter(
Copy link
Member

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?

Copy link
Contributor Author

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"
  },

Copy link
Member

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?

Copy link
Contributor Author

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[]

const variableViolationType = getNodeViolationType(node.init);

if (
blockAncestors.length > 0
&& variableViolationType
&& node.parent.type === 'VariableDeclaration'
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

@y-hsgw y-hsgw Apr 11, 2024

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.

Copy link
Member

@ljharb ljharb Apr 13, 2024

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.

Copy link
Contributor Author

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 .

lib/rules/jsx-sort-default-props.js Outdated Show resolved Hide resolved
lib/rules/jsx-space-before-closing.js Outdated Show resolved Hide resolved
Copy link

codecov bot commented Apr 11, 2024

Codecov Report

Attention: Patch coverage is 94.89796% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 97.69%. Comparing base (4467db5) to head (e2c3497).

❗ Current head e2c3497 differs from pull request most recent head b392497. Consider uploading reports for the commit b392497 to get more accurate results

Files Patch % Lines
lib/rules/jsx-curly-spacing.js 83.33% 1 Missing ⚠️
lib/rules/jsx-equals-spacing.js 50.00% 1 Missing ⚠️
lib/rules/jsx-space-before-closing.js 50.00% 1 Missing ⚠️
lib/rules/no-deprecated.js 92.85% 1 Missing ⚠️
lib/rules/no-unused-class-component-methods.js 50.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@y-hsgw y-hsgw changed the title Add JSDoc annotations Add JSDoc type annotations Apr 11, 2024
@@ -149,9 +150,11 @@ module.exports = {
}

const props = node.arguments[1].properties;
Copy link
Member

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.

Copy link
Contributor Author

@y-hsgw y-hsgw Apr 11, 2024

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.

@y-hsgw y-hsgw requested a review from ljharb April 17, 2024 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants