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
Conversation
…erty-pattern Checks if nodes is an empty array, and returns early if so. Adds test case.
@mattxwang Thanks for creating the pull request. 👍🏼 I think we can use the --- 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 | 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? |
Thanks for the suggestion!
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
I think this is a good idea. I know @jeddy3 mentioned in the original issue
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? |
Oh, my suggestion code is wrong. 😅
Agree. The current number of errors: $ npm run lint:types
...
Found 148 errors in 55 files.
... |
I've created the draft pull request #5983 to turn on |
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 😊 |
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.
LGTM! 👍🏼
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.
LGTM too, thanks!
|
Closes #5972.
In the issue description, @jeddy3 mentions to check if
nodes
exists before restructuring. My understanding is thatnodes
will always exist, and the bug arises from if the array is empty (and thus,nodes[0]
doesn't exist).So, this approach checks ifSo, this approach checks ifnodes
is an empty array, and returns early if so.nodes[0]
exists instead.I've also added a test case (that previously failed with the same error, and now is resolved).