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

bug(semantic): incorrect parents for ArrayAssignmentTarget #4435

Closed
DonIsaac opened this issue Jul 23, 2024 · 5 comments · Fixed by #4456
Closed

bug(semantic): incorrect parents for ArrayAssignmentTarget #4435

DonIsaac opened this issue Jul 23, 2024 · 5 comments · Fixed by #4456
Labels
A-ast Area - AST A-semantic Area - Semantic C-bug Category - Bug

Comments

@DonIsaac
Copy link
Collaborator

Consider the following JS snippet:

let _a, b;
foo.forEach(item => {
	[_a, b] = item;
	doSomething(b);
});

When the AST is viewed on the playground, _a is in the (seemingly) expected node:

https://oxc-project.github.io/oxc/playground/?code=3YCAAIDGgICAgICAgIC2mcpqpbutXC+Uf72ne1SDCJK4Tn++NlLltohMrXmjZqTqJg1QwG6pQhU2rkclTIa8/2PwyR/FKLiP+NUxD23V6F5UHPN/ezQkgA%3D%3D

However, when I get a reference to _a and iterate through its parents like so:

// here, `reference` is the restructured write on _a (the only reference _a has)
for parent in self.nodes().iter_parents(reference.node_id()) {
    println!("{:#?}", parent.kind().debug_name());
}

I get this output:

"IdentifierReference(_a)"
"SimpleAssignmentTarget"
"AssignmentTarget"
"AssignmentTarget"
"AssignmentExpression"
"ExpressionStatement"
"FunctionBody"
"ArrowFunctionExpression"
"Argument"
"CallExpression"
"ExpressionStatement"
"Program"
@DonIsaac DonIsaac added C-bug Category - Bug A-semantic Area - Semantic A-ast Area - AST labels Jul 23, 2024
@Dunqing
Copy link
Member

Dunqing commented Jul 24, 2024

I cannot reproduce it in your provided playground.
image

@Dunqing
Copy link
Member

Dunqing commented Jul 25, 2024

I get this output:

"IdentifierReference(_a)"
"SimpleAssignmentTarget"
"AssignmentTarget"
"AssignmentTarget"
"AssignmentExpression"
"ExpressionStatement"
"FunctionBody"
"ArrowFunctionExpression"
"Argument"
"CallExpression"
"ExpressionStatement"
"Program"

Which AST is unexpected?

@DonIsaac
Copy link
Collaborator Author

_a's parent shouldn't be a simple assignment target, it should be an array pattern target.

@Dunqing
Copy link
Member

Dunqing commented Jul 25, 2024

_a's parent shouldn't be a simple assignment target, it should be an array pattern target.

I get it. That's because we don't have AstKind::ArrayAssigmentTarget

@Boshen
Copy link
Member

Boshen commented Jul 25, 2024

We can try adding these back, given the previous hand rolled versions aren't complete

"ArrayAssignmentTarget",

Boshen pushed a commit that referenced this issue Jul 25, 2024
…ignmentTarget` and `AstKind::ObjectAssignmentTarget` (#4456)

close: #4435
@Dunqing Dunqing closed this as completed Jul 25, 2024
Dunqing pushed a commit that referenced this issue Jul 31, 2024
> Re-creation of #4427 due to rebasing issues. Original attempt: #642
-----

Third time's the charm?

Each time I attempt this rule, I find a bunch of bugs in `Semantic`, and I expect this attempt to be no different. Expect sidecar issues+PRs stemming from this PR here.

## Not Supported
These are cases supported in the original eslint rule, but that I'm intentionally deciding not to support
- export comments in scripts
  ```js
  /* exported a */ var a;
  ```
- global comments
  ```js
  /* global a */ var a;
   ```

## Behavior Changes
These are intentional deviations from the original rule's behavior:
- logical re-assignments are not considered usages
  ```js
  // passes in eslint/no-unused-vars, fails in this implementation
  let a = 0; a ||= 1;
  let b = 0; b &&= 2;
  let c = undefined; c ??= []
  ```

## Known Limitations
- Lint rules do not have babel or tsconfig information, meaning we can't determine if `React` imports are being used or not. The relevant tsconfig settings here are `jsx`, `jsxPragma`, and `jsxFragmentName`. To accommodate this, all imports to symbols named `React` or `h` are ignored in JSX files.
- References to symbols used in JSDoc `{@link}` tags are not created, so symbols that are only used in doc comments will be reported as unused. See: #4443
- `.vue` files are skipped completely, since variables can be used in templates in ways we cannot detect
  > note: `.d.ts` files are skipped as well.

## Todo
- [x] Skip unused TS enum members on used enums
- [x] Skip unused parameters followed by used variables in object/array spreads
- [x] Re-assignments to array/object spreads do not respect `destructuredArrayIgnorePattern` (related to: #4435)
- [x] #4493
- [x] References inside a nested scope are not considered usages (#4447)
- [x] Port over typescript-eslint test cases _(wip, they've been copied and I'm slowly enabling them)_
- [x] Handle constructor properties
  ```ts
  class Foo {
    constructor(public a) {} // `a` should be allowed
  }
  ```
- [x] Read references in sequence expressions (that are not in the last position) should not count as a usage
  ```js
  let a = 0; let b = (a++, 0); console.log(b)
  ```
  > Honestly, is anyone even writing code like this?
- [x] function overload signatures should not be reported
- [x] Named functions returned from other functions get incorrectly reported as unused (found by @camc314)
  ```js
  function foo() {
    return function bar() { }
  }
  Foo()()
  ```
- [x] false positive for TS modules within ambient modules
  ```ts
  declare global {
    // incorrectly marked as unused
    namespace jest { }
  }
  ```

## Blockers
- #4436
- #4437
- #4446
- #4447
- #4494
- #4495

## Non-Blocking Issues
- #4443
- #4475 (prevents checks on exported symbols from namespaces)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ast Area - AST A-semantic Area - Semantic C-bug Category - Bug
Projects
None yet
3 participants