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

Fix TypeError error "Cannot destructure property..." in custom-property-pattern #5982

Merged
merged 2 commits into from Mar 24, 2022

Conversation

mattxwang
Copy link
Member

@mattxwang mattxwang commented Mar 23, 2022

Which issue, if any, is this issue related to?

Closes #5972.

Is there anything in the PR that needs further explanation?

In the issue description, @jeddy3 mentions to check if nodes exists before restructuring. My understanding is that nodes will always exist, and the bug arises from if the array is empty (and thus, nodes[0] doesn't exist). So, this approach checks if nodes is an empty array, and returns early if so. So, this approach checks if nodes[0] exists instead.

I've also added a test case (that previously failed with the same error, and now is resolved).

…erty-pattern

Checks if nodes is an empty array, and returns early if so. Adds test case.
@ybiquitous
Copy link
Member

@mattxwang Thanks for creating the pull request. 👍🏼

I think we can use the noUncheckedIndexedAccess option of TypeScript to detect such a potential bug:

--- a/tsconfig.json
+++ b/tsconfig.json
@@ -9,6 +9,7 @@
 		"strict": true,
 		"noFallthroughCasesInSwitch": true,
 		"noUnusedParameters": true,
+		"noUncheckedIndexedAccess": true,
 		"esModuleInterop": true,
 		"resolveJsonModule": true,
 		"typeRoots": ["./types", "./node_modules/@types"]

Then, run npm run lint:types:

$ npm run lint:types | grep custom-property-pattern
lib/rules/custom-property-pattern/index.js(61,13): error TS2339: Property 'value' does not exist on type 'Node | undefined'.

If we would turn on the option, the following might be better:

const firstNode = nodes[0];

if (firstNode && check(firstNode.value)) return;

Any thoughts?

@mattxwang
Copy link
Member Author

Thanks for the suggestion!

If we would turn on the option, the following might be better:

const firstNode = nodes[0];

if (firstNode && check(firstNode.value)) return;

Any thoughts?

I like how concise this is. Just to confirm, should the early return look more like

const firstNode = nodes[0];

if (!firstNode || check(firstNode.value)) return;

In your suggestion, the original error fails, since firstNode is undefined (and so the checker doesn't early-exit). Or, did you mean something else?

I think we can use the noUncheckedIndexedAccess option of TypeScript to detect such a potential bug:

I think this is a good idea. I know @jeddy3 mentioned in the original issue

Follow-up PR to add "empty functions" to commonly overlooked edge cases.

Perhaps I can create a follow-up PR to enable the TS rule, resolve the other issues (umbrella-issue style), and add it to the edge case list?

@ybiquitous
Copy link
Member

In your suggestion, the original error fails, since firstNode is undefined (and so the checker doesn't early-exit). Or, did you mean something else?

Oh, my suggestion code is wrong. 😅 !firstNode || check(firstNode.value) is right. 👍🏼

Perhaps I can create a follow-up PR to enable the TS rule, resolve the other issues (umbrella-issue style), and add it to the edge case list?

Agree. The current number of errors:

$ npm run lint:types
...
Found 148 errors in 55 files.
...

@ybiquitous
Copy link
Member

I've created the draft pull request #5983 to turn on noUncheckedIndexedAccess.

@mattxwang
Copy link
Member Author

Great, sounds good - have pushed a copy of your suggested change, thanks for the help! If we think this approach works, I can work on eventually getting #5983 to have no errors 😊

Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍🏼

@jeddy3 jeddy3 changed the title fixes TypeError error "Cannot destructure property..." in custom-property-pattern Fix TypeError error "Cannot destructure property..." in custom-property-pattern Mar 24, 2022
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too, thanks!

@jeddy3 jeddy3 merged commit 2e3d1f6 into main Mar 24, 2022
@jeddy3 jeddy3 deleted the fix-custom-property-pattern-type-error-node0 branch March 24, 2022 17:25
@jeddy3
Copy link
Member

jeddy3 commented Mar 24, 2022

  • Fixed: custom-property-pattern TypeError for "Cannot destructure property..." (#5982).

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.

Fix TypeError error "Cannot destructure property..." in custom-property-pattern
3 participants