oxc/crates/oxc_syntax/src
DonIsaac b952942993 feat(linter): add eslint/no-unused-vars ( attempt 3.2) (#4445)
> 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: https://github.com/oxc-project/oxc/issues/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
- https://github.com/oxc-project/oxc/issues/4436
- https://github.com/oxc-project/oxc/issues/4437
- #4446
- #4447
- #4494
- #4495

## Non-Blocking Issues
- #4443
- #4475 (prevents checks on exported symbols from namespaces)
2024-07-31 03:22:16 +00:00
..
class.rs feat(semantic): add static property, ElementKind::Getter, ElementKind::Setter in ClassTable (#2445) 2024-02-20 13:07:48 +08:00
identifier.rs refactor(parser): make is_identifier methods consistent 2024-01-23 11:05:17 +08:00
keyword.rs fix(syntax): correct is_reserved_keyword_or_global_object's incorrect function calling. (#4484) 2024-07-26 17:20:10 -04:00
lib.rs refactor(syntax): move number related functions to number module (#3130) 2024-04-29 18:54:35 +08:00
module_graph_visitor.rs chore: improve some format by running cargo +nightly fmt 2024-06-19 00:48:30 +08:00
module_record.rs fix(semantic): export default foo should have ExportLocalName::Default(NameSpan) entry (#3823) 2024-06-22 11:09:23 +00:00
node.rs perf(semantic): give AstNodeId a niche (#4469) 2024-07-26 00:14:53 +00:00
number.rs feat(syntax): add ToJsInt32 trait for f64 (#3132) 2024-04-29 21:13:04 +08:00
operator.rs feat(linter): add eslint/no-unused-vars ( attempt 3.2) (#4445) 2024-07-31 03:22:16 +00:00
precedence.rs feat(codegen): align operator precedence with esbuild (#4509) 2024-07-28 11:48:51 +00:00
reference.rs refactor(syntax): use NonMaxU32 for IDs (#4467) 2024-07-26 00:14:47 +00:00
scope.rs refactor(syntax): give ScopeId a niche (#4468) 2024-07-26 00:14:50 +00:00
symbol.rs feat(linter): add eslint/no-unused-vars ( attempt 3.2) (#4445) 2024-07-31 03:22:16 +00:00
xml_entities.rs feat(transformer/jsx): escape xhtml in jsx attributes (#1088) 2023-10-29 15:16:50 +08:00