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): references on MemberExpression not recorded #4446

Closed
DonIsaac opened this issue Jul 24, 2024 · 4 comments
Closed

bug(semantic): references on MemberExpression not recorded #4446

DonIsaac opened this issue Jul 24, 2024 · 4 comments
Labels
A-semantic Area - Semantic C-bug Category - Bug

Comments

@DonIsaac
Copy link
Contributor

DonIsaac commented Jul 24, 2024

enum Foo { // <- foo creates a scope, just like a namespace
  A        // <- has a symbol (SymbolFlags::EnumMember)
}

// Foo has a reference, but A does not
console.log(Foo.A) 

(edit: it appears playground links are broken. When I go to copy a playground URL for this snippet, it takes me to the playground with the default react component snippet)

@armano2
Copy link

armano2 commented Jul 24, 2024

enums are special

const foo = 2;
const b = 2;
enum Foo {
  outer = foo, // foo == 2
  a = 1, // 1
  b = a, // b == Foo.a == 1  |  shadows const b
  c = b, // c == Foo.b == Foo.c == 1
}

you can read a little about some edge cases at typescript-eslint/typescript-eslint#325


typescript enum behaves like a object and block at the same time,

code above compiles to:

const foo = 2;
const b = 2;
var Foo;
(function (Foo) {
    Foo[Foo["outer"] = 2] = "outer";
    Foo[Foo["a"] = 1] = "a";
    Foo[Foo["b"] = 1] = "b";
    Foo[Foo["c"] = 1] = "c";
})(Foo || (Foo = {}));

from no unused vars persepctive we should not care if enum values are read or not.


MemberExpression can't be easily referenced, as you would need way more data about structures.

eg.

// file1.ts
export enum Foo {
  test = 1,
  unused = 2 // ??
}

// file2.ts
import { Foo } from 'file1.ts'

Foo.test

@overlookmotel
Copy link
Contributor

I agree with @armano2 that the property of a MemberExpression should not be a reference (i.e. the A in Foo.A).

But the issue of what is a symbol is trickier.

In one sense A inside the enum is a symbol. But it doesn't quite map cleanly onto the usual scopes concept. e.g.:

const A = 123;
enum X {
    A = 1,
    B = A, // refers to `A = 1`
}
enum X {
    C = A, // refers to `A = 1`
}

TS playground

If we're treating A in enum X as a symbol, then that symbol should also be bound inside the body of the second enum X.

@armano2 How does typescript-eslint handle this case?

I think we probably do need it to be a symbol to make this kind of thing tractable:

const A = 123;
enum X {
   A = 1,
   B = A, // refers to `A = 1`
}
enum X {
   C = A, // refers to `A = 1`
   D = (() => {
     enum Y {
       Q = A, // refers to `A = 1`
     }
     return Y.Q;
   })(),
}

We don't seem to be getting this quite right at present: Oxc playground

TS playground

@armano2
Copy link

armano2 commented Jul 24, 2024

we are ignoring this case, and we are "marking" all identifiers in enum's as already in use

https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/no-unused-vars.ts#L516

@DonIsaac
Copy link
Contributor Author

I agree that no-unused-vars should ignore this case. I'm hoping to add class/object/interface/etc members to the symbol table later, but that can be for a later time. I'll close this issue.

@DonIsaac DonIsaac closed this as not planned Won't fix, can't repro, duplicate, stale 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-semantic Area - Semantic C-bug Category - Bug
Projects
None yet
Development

No branches or pull requests

3 participants