Commit graph

91 commits

Author SHA1 Message Date
Burlin
f88970bc79
refactor(ast)!: Change order of fields in CallExpression (#4859)
fix: #4821

---------

Co-authored-by: Dunqing <dengqing0821@gmail.com>
2024-08-20 09:47:12 +08:00
DonIsaac
48821c0110 feat(semantic,syntax): add SymbolFlags::ArrowFunction (#4946)
There are many cases in lint rules where we want to see if a symbol is a
function by checking its SymbolFlags. This is currently not fully possible,
since variables assigned to arrow functions are not distinguished from any other
kind of variable. This PR adds `SymbolFlags::ArrowFunction` for variables that
are initialized to arrow functions. Symbols that are re-assigned to arrow
functions will not have this flag, but this is acceptable for lint rules.
2024-08-18 05:54:23 +00:00
DonIsaac
80d0d1f0c4 feat(semantic): check for invalid interface heritage clauses (#4928) 2024-08-16 02:30:09 +00:00
DonIsaac
0a01a4729a docs(semantic): improve documentation (#4850) 2024-08-13 02:14:07 +00:00
DonIsaac
fd2d9dafcd feat(ast): improve AstKind::debug_name (#4553)
> Part of #4445

Improve `debug_name` to show identifier names for more AST kinds.
2024-08-03 21:22:05 +00:00
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
Dunqing
cf1854be7c feat(semantic): remove ReferenceFlags::Value from non-type-only exports that referenced type binding (#4511)
```ts
type T = 0;
export { T }
         ^ ReferenceFlags::Type | ReferenceFlags::Read
```
The export `T` only referenced type binding. We should remove `ReferenceFlags::Read` for it.
2024-07-29 03:39:18 +00:00
Dunqing
36bb6805c6 fix(semantic): TSExportAssignment cannot reference type binding (#4502)
close: #4488
2024-07-27 04:55:03 +00:00
Dunqing
2477330440 feat(ast): add AstKind::TSExportAssignment (#4501)
part of #4488
2024-07-27 04:55:02 +00:00
Dunqing
cb2fa4924e
fix(semantic): typeof operator cannot reference type-only import (#4500) 2024-07-27 00:21:06 -04:00
Dunqing
ef0e953702
fix(semantic): generic passed to typeof not counted as a reference (#4499) 2024-07-27 00:16:25 -04:00
Dunqing
40cafb8c61 fix(semantic): params in export default (function() {}) flagged as SymbolFlags::Export (#4480)
The code changes in `builder.rs` optimize the export flag check in the `SemanticBuilder` implementation. Instead of setting the export flag to `true` for all cases except `ExportDefaultDeclarationKind::ClassDeclaration(ref class)`, the flag is now set to `true` only for `ExportDefaultDeclarationKind::ClassDeclaration` and `false` for all other cases. This change improves the efficiency of the export flag check.
2024-07-27 02:42:24 +00:00
Don Isaac
2e01a45b2b
fix(semantic): non-exported namespace member symbols flagged as exported (#4493)
> Part of #4445

Fixes a bug where non-exported functions and variables inside of
exported TS namespaces were being flagged with `SymbolFlags::Export`

```ts
export namespace Foo {
  // incorrectly flagged as exported
  function foo() { }
}
```
2024-07-27 08:46:21 +08:00
Dunqing
e4ca06ae8c
fix(semantic): incorrect symbol’s scope_id after var hoisting (#4458)
Bug found in https://github.com/rolldown/rolldown/pull/1726
2024-07-25 21:12:26 +08:00
Dunqing
aaee07e170 feat(ast): add AstKind::AssignmentTargetPattern, AstKind::ArrayAssignmentTarget and AstKind::ObjectAssignmentTarget (#4456)
close: #4435
2024-07-25 09:32:33 +00:00
Dunqing
7cd53f3897 refactor(semantic): var hoisting (#4379)
close: #4323

This PR refactors the var hoisting logic to avoid inserting the same symbol into every scope that can be hosted.
2024-07-25 00:55:02 +00:00
DonIsaac
871b3d6135 docs(semantic): add doc comments for SymbolTester and SemanticTester (#4433) 2024-07-24 16:58:13 +00:00
DonIsaac
4f5a7cbf3c refactor(semantic): mark SemanticTester and SymbolTester as must_use (#4430) 2024-07-24 16:58:11 +00:00
DonIsaac
4b274a8e6f test(semantic): add more test cases for symbol references (#4429) 2024-07-24 16:58:09 +00:00
Dunqing
46cf7179a4 chore(semantic): fix incorrect reference id in snapshot (#4441) 2024-07-24 12:51:57 +00:00
Dunqing
85a7cea9d1 refactor(semantic)!: remove name from reference (#4329)
related to: https://github.com/oxc-project/backlog/issues/32
2024-07-22 15:59:23 +00:00
Dunqing
6ffce865d1 fix(semantic): align visit_arrow_function_expression field visit order with ast (#4366)
related: #4364
2024-07-19 14:51:59 +00:00
Dunqing
f8565ae3cd fix(transformer/typescript): unexpectedly removed class binding from ExportNamedDeclaration (#4351)
The original `SymbolFlags` methods were a bit confusing I renamed and re-implemented them.
2024-07-18 16:44:38 +00:00
Dunqing
95e15b6dc5 fix(semantic): incorrect resolve references for ExportSpecifier (#4320)
```ts
type A = any;
const B = 0;
export { A, B }
       ^^^^^^^^ ExportSpecifiers

export { A }
       ^^^^^ type-only ExportSpecifiers

```

non-type-only `ExportSpecifier` can reference value and type symbols. but currently, `IdentifierReference` in ExportSpecifier only has a `ReferenceFlags::Read`
2024-07-17 09:52:58 +00:00
Dunqing
a88d588a07 feat(semantic): add ReferenceFlags::TSTypeQuery to indicate referenced by TSTypeQuery (#4317)
`ReferenceFlags::TSTypeQuery` can be used to help us insist on whether the reference is referenced by the type or not.
2024-07-17 09:52:57 +00:00
Dunqing
c362bf7edf fix(semantic): incorrect resolve references for TSInterfaceHeritage (#4311)
related issue: https://github.com/oxc-project/oxc/issues/3963
fixes: https://github.com/oxc-project/monitor-oxc/actions/runs/9960183591/job/27518854841
2024-07-17 03:33:02 +00:00
Dunqing
351ecf2707 fix(semantic): incorrect resolve references for TSTypeQuery (#4310)
```ts
type A = typeof Foo
                ^^^ Only allow reference to value symbol
```

I have verified the changed snapshot. That's correct
2024-07-17 03:33:00 +00:00
Dunqing
48724a0d44 chore(semantic): copy tests from typescript-eslint’s scope-manager (#3990)
close:  #2947

These fixtures were copied from a5b652da1e/packages/scope-manager/tests/fixtures. We used them to test out semantic `ScopeTree` and `SymbolTable`
2024-07-17 02:50:50 +00:00
Dunqing
1108f2a700 fix(semantic): resolve references to the incorrect symbol (#4280)
close: #3799
related: #3863
2024-07-17 02:50:48 +00:00
Dunqing
af4dc0112e feat(ast): align ts ast scope with typescript (#4253)
close: #3969
close: #2023

We need to add scope according to [this](d8086f14b6/src/compiler/binder.ts (L3883-L3962)). There are still some ASTs that need to be added to the scope.

---

Context from @Boshen:

Before this whole journey of fixing symbols and scopes I asked @Dunqing to debug through binder.ts via a debugger to fully understand how it does resolution.

We then agreed to align the implementation so that when a problem occurs, we can debug through both implementations and find where our problem is.

tsc doesn't have a specification, so we need to align with the reference implementation instead.
2024-07-17 02:50:47 +00:00
Dunqing
7f1adddaf0 refactor(semantic): correct scope in CatchClause (#4192)
close: #4186

CatchClause has two scopes. The first one is `CatchClause`, which will add a `CatchParameter` to it. The second one is `Block`, which will add binding that declares in the current block scope.

The spec has a syntax error about `CatchParameter`
- It is a Syntax Error if any element of the BoundNames of CatchParameter also occurs in the LexicallyDeclaredNames of Block.
2024-07-11 08:45:30 +00:00
Don Isaac
0f026089d1
fix(semantic): bind TSImportEqualsDeclarations (#4100)
Closes #4091
2024-07-08 14:03:47 +08:00
Dunqing
d995f94280 fix(semantic): resolve reference incorrectly when a parameter references a parameter that hasn't been defined yet (#4004)
close: #3682

The TypeScript code that handles this is [here](d8086f14b6/src/compiler/utilities.ts (L11515-L11577)). It looks complicated.
2024-07-02 02:12:15 +00:00
Boshen
54c653ebd4 Revert "perf(semantic): use Atom<'a> for References" (#3974)
Reverts oxc-project/oxc#3972

@DonIsaac As it turns out we can't have lifetimes on these structures because semantic data is exposed to downstream users to use freely, detached from the ast and allocator.
2024-06-29 15:55:04 +00:00
Don Isaac
1eac3d244d
perf(semantic): use Atom<'a> for References (#3972)
Relates to [this
issue](https://github.com/oxc-project/backlog/issues/31) on the backlog.
2024-06-29 23:23:36 +08:00
Boshen
9b38119ec9 refactor(ast)!: replace Modifiers with declare on VariableDeclaration (#3839)
part of #2958
2024-06-23 10:34:52 +00:00
Don Isaac
d5f6aeb1ca
feat(semantic): check for illegal symbol modifiers (#3838) 2024-06-23 12:36:09 +08:00
Boshen
445603444f feat(ast)!: add IdentifierReference to ExportSpecifier (#3820)
closes #3795
closes #3796
2024-06-22 11:43:41 +00:00
overlookmotel
4f7ff7e3ad perf: do not pass &Atom to functions (#3818)
`Atom` is just a wrapper around `&str`, so better not to pass `&Atom` to functions, as that's a double-reference. Prefer `Atom` or `&str` instead to avoid indirection.
2024-06-22 04:48:00 +00:00
Boshen
45b0d1147e chore(semantic): regenerate snapshot (#3757) 2024-06-19 09:28:31 +00:00
Boshen
051ceb6539
chore: improve some format by running cargo +nightly fmt 2024-06-19 00:48:30 +08:00
rzvxa
256acc949b
chore(semantic)!: no longer generate CFG by default. (#3738)
related but won't close #3641
2024-06-19 00:13:29 +08:00
rzvxa
d8ad321687 refactor(semantic): make control flow generation optional. (#3737)
For maximum backward compatibility, we generate CFG by default.

Note: It can't be done with a simple method since lifetimes make it impossible(at least without unsafe trickery) I've tried to do it without a macro but it was just unintuitive.
2024-06-18 15:59:38 +00:00
rzvxa
0537d298db refactor(cfg)!: move control flow to its own crate. (#3728) 2024-06-18 15:59:29 +00:00
Ali Rezvani
e148a32ce3
fix(semantic/cfg): correct unreachability propagation in try-finally. (#3667)
closes #3663


[oxlint-ecosystem-ci](https://github.com/rzvxa/oxlint-ecosystem-ci/actions/runs/9511509383/job/26217870705)

From this:

![Screenshot 2024-06-14
092916](https://github.com/oxc-project/oxc/assets/3788964/9e6a9a01-984a-4a19-8a73-abc3b71fb9c2)

To this:

![Screenshot 2024-06-14
092852](https://github.com/oxc-project/oxc/assets/3788964/46483bc2-a227-4416-b8da-07b11ab96990)

Since try-finally (without a catch) wouldn't join after finalization.
2024-06-14 14:39:20 +08:00
rzvxa
9c31ed9178 feat(semantic/cfg): propagate unreachable edges through subgraphs. (#3648)
From this:

![Screenshot 2024-06-12 230152](https://github.com/oxc-project/oxc/assets/3788964/b489cee8-62ec-4483-9c9b-6e3d9858fec2)

To this:

![Screenshot 2024-06-12 230031](https://github.com/oxc-project/oxc/assets/3788964/674a6e42-5311-4ca6-940b-65d8e3ccc0f4)
2024-06-13 08:34:12 +00:00
Ali Rezvani
d9c5b33394
feat(semantic/cfg): add Condition instruction. (#3567) 2024-06-13 16:17:24 +08:00
rzvxa
f2dfd667df feat(semantic/cfg): add iteration instructions. (#3566) 2024-06-13 07:36:20 +00:00
rzvxa
defef057e1 improvement(semantic/cfg): better control flow for switch statements. (#3547)
There was an issue similar to the one in loops see #3451 to #3453
2024-06-13 07:36:18 +00:00
rzvxa
9b3097147e improvement(semantic/cfg): rework error path. (#3519)
This PR aims to provide a more accurate error/finalization flow, I've nuked the old error path approach and rewrote it with more versatility in mind.

We used to visit the finalizer block twice and create 2 sets of AstNodes/Basic Blocks for them, This was there to differentiate between the error path finalizer and success path one. There is no longer a need for having 2 separate sets of nodes to do this differentiation.

As for the error path now we have 2 kinds of them, Everything is attached to an error block - even if it is not in a try-catch statement - this results in a lot of extra edges to keep track of these "Implicit" error blocks but I believe in future it can help us to track cross block error paths, For example, we can dynamically attach and cache the implicit error block of a function to its call site error path (either implicit or explicit).
2024-06-13 07:36:16 +00:00