Commit graph

122 commits

Author SHA1 Message Date
DonIsaac
6159560170 fix(parser): string ImportSpecifiers for type imports (#6352)
Fixes a parse failure on imports like this:
```ts
import { type "<A>" as typeA } from "./arbitraryModuleNamespaceIdentifiers_module";
```
2024-10-08 08:17:30 +00:00
DonIsaac
14275b138e feat(cfg): color-code edges in CFG dot diagrams (#6314)
- add `Attrs`, a nominal struct for cleaner management of node/edge styles and
  other attributes
- Style edges in DOT diagrams based on their kind
2024-10-07 02:43:30 +00:00
overlookmotel
bdd9e925f1 refactor(semantic): rename vars from ast_node_id to node_id (#6304)
Style nit. We renamed `AstNodeId` to `NodeId`, so rename vars from `ast_node_id` to `node_id` too.
2024-10-06 01:03:48 +00:00
overlookmotel
d11070051b refactor(semantic): dereference IDs as quickly as possible (#6303)
It's better to pass around `SymbolId`s (4 bytes) than `&SymbolId`s (8 bytes). In my view, the style of dereferencing immediately is preferable.
2024-10-06 01:03:47 +00:00
DonIsaac
933a7431b1 fix(semantic): add interfaces and functions to SymbolFlags::ClassExcludes (#6057)
This brings ClassExcludes into alignment with TypeScript.
2024-09-27 05:57:03 +00:00
DonIsaac
b1af73db81 fix(semantic): do not create a global symbol for declare global {} (#6040)
Re-creation of #6004 to unblock it from down-stack PRs. @Boshen has already reviewed the previous PR and determined this to be correct behavior.
2024-09-25 08:46:00 +00:00
DonIsaac
93575cd09e test(semantic): add comprehensive regression test suite (#5976)
# What This PR Does

Enhance's `oxc_semantic`'s integration tests with a regression test suite that ensures semantic's contract guarantees hold over all test cases in typescript-eslint's scope snapshot tests. Each test case checks a separate assumption and runs independently from other test cases.

This PR sets up the code infrastructure for this test suite and adds two test cases to start us off:
1. Reflexivity tests for `IdentifierReference` and `Reference`
2. Symbol declaration reflexivity tests between declarations in `SymbolTable` and their corresponding node in the AST.

Please refer to the doc comments for each of these tests for an in-depth explanation.

## Aren't our existing tests sufficient?
`oxc_semantic` is currently tested directly via
1. scope snapshot tests, ported from `typescript-eslint`
2. Hand-written tests using `SemanticTester` in `tests/integration`

And indirectly via

3. Conformance test suite over Test262/TypeScript/Babel
4. Linter snapshot tests

Shouldn't this be sufficient? I argue not, for two reasons:

## 1. Clarify Contract Ambiguity

When using `Semantic`, I often find myself asking these questions?
* Does `semantic.symbols().get_declaration(id)` point to a `BindingIdentifer`/`BindingPattern` or the declaration that holds an identifier/pattern?
* Will a `Reference`'s `node_id` point me to an `IdentifierReference` or the expression/statement that is holding an `IdentifierReference`?
* When will `BindingIdentifier`'s `symbol_id` get populated? can we guarantee that after semantic analysis it will never be `None`?
* What actually _is_ the node covered by `semantic.symbols().get_span(id)`? This one really messed me up, and resulted in me creating #4739.
* What scope does `Function::scope_id` point to? The one where the function is declared? The one created by its body? The one created by the type annotations but before the function body? Or something else entirely?

**These test cases are meant to answer such questions and guarnatee those answers as test cases**. No other existing testing solution currently upholds such promises: they only tell us if code expecting one answer or another produces an unexpected result. However, those parts of the codebase could always be adjusted to conform to new `Semantic` behavior, meaning no contract guarantees are actually upheld.

## 2. Existing Tests Do Not Test The Same Behavior

I'll cover each above listed test case one-by-one:

1. For starters, these tests only cover scopes. Additionally, they only tell us **how behavior has changed**, not that **behavior is now incorrect**.
2. These _do_ generally cover the same behaviors, but **are not comprehensive and are difficult to maintain**. These are unit tests that should be used hand-in-hand with this new test suite.
3. The most relevant tests here are for the parser. However, these tests **only tell us if a syntax/parse error was produced**, and tell us nothing about the validity of `Semantic`.
4. Relying on lint rule's output is a a mediiocre proxy of `Semantic`'s behavior at best. They can tell us if changes to `Semantic` break assumptions made by lint rules, but they do not tell us if **those assumptions are the ones we want to uphold to external crates consuming `Semantic`.
2024-09-25 02:24:57 +00:00
Dunqing
a23879cb96 fix(semantic): analyze ReferenceFlags incorrectly when there are nested AssignmentTarget (#5847)
close: #5801
2024-09-21 04:13:24 +00:00
overlookmotel
c96b712f6b refactor(syntax)!: remove SymbolFlags::ArrowFunction (#5857)
`SymbolFlags::ArrowFunction` is an oddity, as whether a symbol is an arrow function is not statically knowable. In the following cases, `f` symbol did not have `ArrowFunction` flag set:

```js
const {f} = {f: () => {}};
```

```js
let f = 123;
f = () => {};
```

`SymbolFlags::ArrowFunction` is therefore not particularly useful, and possibly misleading. Having it complicates the transformer, and it's not used anywhere in Oxc.

This PR removes it.
2024-09-18 14:03:03 +00:00
IWANABETHATGUY
805fbac44c feat(oxc_cfg): better control flow graph dot dot repr (#5731)
1. add basic block index for each basicblock, so there should no basic block display empty string
2. use `shape` box, since it is basic **block**
## Before
![image](https://github.com/user-attachments/assets/9b556cca-8401-40bd-b3b6-d31c5e7cf8ba)

## After
![image](https://github.com/user-attachments/assets/b8e4c52d-ecdf-4e92-85be-ea37f0cb2330)
2024-09-12 18:39:01 +00:00
Boshen
4bdc202325 refactor(rust): remove some #[allow(unused)] (#5716) 2024-09-12 02:19:01 +00:00
Boshen
4a8aec1605 feat(span)!: change SourceType::js to SourceType::cjs and SourceType::mjs (#5606) 2024-09-08 14:11:02 +00:00
Boshen
b06052501a refactor(semantic)!: remove source_type argument from SemanticBuilder::new (#5553)
Realized we can get the source type from the AST.

The next PR will introduce `unambiguous` to `SourceType` and directly set `Program::source_type` to either `script` or `module`.
2024-09-06 16:40:10 +00:00
overlookmotel
dc924892cc test: add trailing line breaks to conformance fixtures (#5541)
Continuation of #5537. Ensure all conformance fixture files have a trailing line break.
2024-09-06 12:55:17 +00:00
Dunqing
7a797ac635 fix(semantic): incorrect reference when MemberExpression used in TSPropertySignature (#5525)
close: https://github.com/oxc-project/oxc/issues/5435#issuecomment-2333032168
2024-09-06 06:24:45 +00:00
Boshen
1bed5ce2a5 chore: run cargo +nightly fmt to sort imports (#5503)
They are never going to be stable are they ... cedf7a4daa/.rustfmt.toml (L8-L16)
2024-09-06 04:04:26 +00:00
dalaoshu
2a43fa4efd
style(linter): introduce the writing style from PR #5491 and reduce the if nesting (#5512)
Related to #5491
2024-09-06 11:19:48 +08:00
Don Isaac
340b535715
test(linter/no-unused-vars): arrow functions in tagged templates (#5510)
Closes  #5391
2024-09-05 14:55:39 -04:00
Dunqing
e4ed41d4fa refactor(semantic): change the reference flag to ReferenceFlags::Type if it is used within a TSTypeQuery (#5444)
So far, the `ReferenceFlags::TSTypeQuery` only used indicates it is referenced by `TSTypeQuery` that we can confirm the reference should be regarded as a type reference, namely `ReferenceFlags::Type`.

This PR adds a `ReferenceFlags::ValueAsType` instead of `ReferenceFlags::TSTypeQuery`.  The new flag has the same behavior as the previous one. But it looks more general and is not only used in `TSTypeQuery`. But now it is a temporary flag. We use it to resolve the symbol correctly and replace `ReferenceFlags::ValueAsTyoe` with `ReferenceFlags::Type` after resolved.

Also, this change eliminates the inconsistency in behavior between the `Reference::is_type` and `ReferenceFlags::is_type` methods.
2024-09-05 01:50:20 +00:00
Dunqing
d8b9909bd8 fix(semantic): IdentifierReference within TSPropertySignature cannot reference type-only import binding (#5441)
close: #5435

The behavior of `IdentifierReference` in `TSPropertySignature` is the same as in `TSTypeQuery`, both allow only reference value bindings and type-only import bindings.

I still use `ReferenceFlags::TSTypeQuery` here because I want to avoid producing many changes unrelated to the bug in this PR. I will refactor it in the follow-up PR soon
2024-09-05 01:50:18 +00:00
Boshen
01cc2ceac0 feat(semantic)!: add ScopeTree:get_child_ids API behind a runtime flag (#5403)
The runtime API for enabling `get_child_ids` is `SemanticBuilder::with_scope_tree_child_ids`.

I've tested this PR in Rolldown to ensure correctness.
2024-09-02 16:22:26 +00:00
Dunqing
be7b8c6845 test(semantic): add JSXIdentifierReference-related tests (#5224)
These tests demonstrate what kind of JSX will have references.
2024-08-30 17:43:34 +00:00
Boshen
a17cf33dc3 refactor(semantic): remove ScopeTree::child_ids (#5232)
closes https://github.com/oxc-project/oxc/issues/5244
2024-08-27 01:49:47 +00:00
Dunqing
293413fe71 fix(semantic): implicit return UpdateExpression in ArrowFunctionExpression does not as read reference (#5161)
close: #5158
close: #5156
2024-08-26 03:30:16 +00:00
DonIsaac
c2fa72571f feat(ast,parser): parse TSTypeAnnotations on AccessorProperty (#5179)
Closes #5177

While making this, I noticed an uncaught parse error for accessors: accessors cannot be optional. I'll add a fix for this in an up-stack PR.
2024-08-25 01:02:48 +00:00
overlookmotel
f2b8d82499 refactor(semantic)!: ScopeTree::get_child_ids + get_child_ids_mut return value not Option (#5058)
Previously `ScopeTree::get_child_ids` and `ScopeTree::get_child_ids_mut` returned an `Option`. Instead return the unwrapped value, in line with other methods e.g. `get_flags`.
2024-08-22 05:57:55 +00:00
overlookmotel
4336a3275c refactor(semantic): rename fields in snapshots from flag to flags (#5032)
Part of #4991.
2024-08-21 00:19:59 +00:00
overlookmotel
5f4c9ab38e refactor(semantic)!: rename SymbolTable::get_flag to get_flags (#5030)
Part of #4991.
2024-08-21 00:19:58 +00:00
overlookmotel
ca70cc7c03 refactor(linter, mangler, parser, semantic, transformer, traverse, wasm): rename various flag vars to flags (#5028)
Part of #4991.
2024-08-21 00:19:58 +00:00
overlookmotel
58bf21531e refactor(semantic)!: rename Reference::flag and flag_mut methods to plural (#5025)
Part of #4991.
2024-08-21 00:19:57 +00:00
overlookmotel
d262a58eb5 refactor(syntax)!: rename ReferenceFlag to ReferenceFlags (#5023)
Part of #4991.
2024-08-21 00:19:56 +00:00
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