Commit graph

305 commits

Author SHA1 Message Date
overlookmotel
91343913ca fix(semantic): transform checker check unresolved references (#5096)
Transform checker check root unresolved references.

The transform checker is now checking pretty much everything it can.

Only fields of `ScopeTree` and `SymbolTable` that it's *not* checking are those which contain `AstNodeId`s, because transformer does not create node IDs at present:

* `ScopeTree::node_ids`
* `SymbolTable::declarations`
* `Reference::node_id`

Checking also only proceeds in "from AST" direction.

i.e. for each `SymbolId` which appears in the AST, we check everything about that symbol. But we *don't* go through all the "rows" in `SymbolTable` and check if there are any extra symbols in the table which aren't in the AST.

Presumably transformer will leave a lot of old symbols lying around in `SymbolTable` (ditto scopes and references). We'd need to add `ScopeFlags::Deleted`, `SymbolFlags::Deleted` and `ReferenceFlags::Deleted` for the transformer to be able to "delete" existing symbols.
2024-08-23 07:52:30 +00:00
overlookmotel
c57e078c71 fix(semantic): transform checker check unbound references (#5093) 2024-08-23 08:37:48 +01:00
Boshen
aa7718ab7b feat(transform_conformance): show printed output alongside with errors (#5105)
closes #5098

```
cargo run -p oxc_transform_conformance -- --filter logical-assignment/arrow-functions-transform/input.js

Input:

var a;
a ||= () => {};
a &&= () => {};
a ??= () => {};

Expected:

var a;
a || (a = () => {});
a && (a = () => {});
a ?? (a = () => {});

Transformed:

var a;
a || (a = () => {});
a && (a = () => {});
a ?? (a = () => {});

Errors:

  x Symbol reference IDs mismatch:
  | after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1),
  | ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(5),
  | ReferenceId(6), ReferenceId(7), ReferenceId(8)]
  | rebuilt        : SymbolId(0): [ReferenceId(0), ReferenceId(1),
  | ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(5)]

  x Reference flags mismatch:
  | after transform: ReferenceId(4): ReferenceFlags(Write)
  | rebuilt        : ReferenceId(1): ReferenceFlags(Read | Write)

  x Reference flags mismatch:
  | after transform: ReferenceId(6): ReferenceFlags(Write)
  | rebuilt        : ReferenceId(3): ReferenceFlags(Read | Write)

  x Reference flags mismatch:
  | after transform: ReferenceId(8): ReferenceFlags(Write)
  | rebuilt        : ReferenceId(5): ReferenceFlags(Read | Write)

Passed: true
```
2024-08-23 04:00:45 +00:00
overlookmotel
d5de97d6fe fix(semantic): transform checker check reference flags (#5092) 2024-08-23 00:21:21 +00:00
overlookmotel
9da6a21e40 refactor(semantic): rename transform checker output for reference symbol mismatches (#5091) 2024-08-22 23:40:27 +00:00
overlookmotel
a8005b9914 fix(semantic): transform checker check symbol redeclarations (#5089) 2024-08-22 23:40:26 +00:00
overlookmotel
205bff7ea9 fix(semantic): transform checker check symbol references (#5088) 2024-08-22 23:40:25 +00:00
overlookmotel
4a57086d79 fix(semantic): transform checker check symbol IDs (#5078) 2024-08-22 15:08:48 +00:00
overlookmotel
ea7d2163e3 fix(semantic): transform checker check symbol spans (#5076) 2024-08-22 15:08:47 +00:00
overlookmotel
1b6b27a6de fix(semantic): transform checker check symbol flags (#5074) 2024-08-22 15:08:45 +00:00
Dunqing
3b353321ad fix(transformer/logical-assignment-operators): fix semantic errors (#5047)
Fix semantic error caused by `clone_in` causing `reference_id` to not exist.

In this PR, I try to use `move_expression` as much as possible instead of `expr.clone_in`. But in some cases, we need to reuse the same expression in multiple places. I have added a `clone_expression` to workaround it

I felt a bit painful we missing a [clone_in_scope](https://github.com/oxc-project/oxc/issues/4804) API
2024-08-22 08:41:31 +00:00
overlookmotel
f187b71877 fix(semantic): transform checker compare scope children (#5056) 2024-08-22 02:34:44 +00:00
overlookmotel
b52c6a4269 fix(semantic): transform checker compare scope parents (#5055) 2024-08-22 02:34:43 +00:00
overlookmotel
da64014a6c fix(semantic): transform checker catch more scope flags mismatches (#5054)
There was a bug previously where scope flags were only checked if there was also a bindings mismatch.
2024-08-22 02:34:43 +00:00
overlookmotel
67d1a96391 fix(semantic): transform checker compare scope flags (#5052) 2024-08-22 02:34:41 +00:00
overlookmotel
ee7ac8b0b7 refactor(semantic): store all data in PostTransformChecker in transform checker (#5050)
Pure refactor of transform checker. Store all scope data in `PostTransformChecker` so it doesn't need to be passed around. `SemanticData` contains a full set of all semantic data for semantic pass, so we have 2 instances of it for 1. after transform and 2. rebuilt semantic.
2024-08-21 17:04:48 +00:00
overlookmotel
8cded08eb8 refactor(semantic): rename error labels in transformer checker snapshots (#5044)
Rename labels in transformer checker snapshots "after transform" vs "rebuilt".
2024-08-21 14:33:19 +00:00
overlookmotel
863b9cb921 fix(semantic): transform checker handle conditional scopes (#5040)
Some scopes are conditional e.g. `ForStatement` only gets a scope when initializer has a binding (`for (let i = 0; ...)` vs `for (i = 0; ...)`).

Make transform compare this between post-transform and fresh semantics.
2024-08-21 12:07:14 +00:00
overlookmotel
586e15c814 refactor(semantic): reformat transform checker errors (#5039)
Reformat transform checker error output - shorter and consistent capitalization.
2024-08-21 12:07:11 +00:00
Boshen
1bd9365bd0 fix(coverage): correctly check semantic data after transform (#5035)
closes #4999
2024-08-21 09:35:04 +00:00
Dunqing
f51d3f9169 feat(transformer/nullish-coalescing-operator): handles nullish coalescing expression in the FormalParamter (#4975)
### What I did in this PR
1. Replace `self.clone_identifier_reference` with `ctx.clone_identifier.reference`
2. Remove the usage of `ast.copy`
3. Handle below example correctly

### Example

```js
// Input
var foo = object.foo ?? "default";

// Output
var _object$foo;
var foo =
(_object$foo = object.foo) !== null && _object$foo !== void 0
  ? _object$foo
  : "default";
```
2024-08-20 01:06:37 +00:00
Dunqing
f794870dd4 feat(transformer/nullish-coalescing-operator): generate the correct binding name (#4974)
match Babel's [implementation](440fe41333/packages/babel-plugin-transform-nullish-coalescing-operator/src/index.ts (L40))
2024-08-20 01:06:36 +00:00
Boshen
64ace42566 feat(transform_conformance): show transform conformance errors (#4976) 2024-08-19 09:01:34 +00:00
Boshen
4fdf26dac8 refactor(transform_conformance): add driver (#4969) 2024-08-19 07:27:39 +00:00
Dunqing
f1fcdde593 feat(transformer): support react fast refresh (#4587)
close: #3943

## Further improvements

There is a double visit here. We need to collect all react hooks calling in `Function` and `ArrowFunctionExpression`. If we want to remove this implementation, we may wait for #4188.

d797e595d2/crates/oxc_transformer/src/react/refresh.rs (L744-L947)

## Tests

All tests copy from https://github.com/facebook/react/blob/main/packages/react-refresh/src/__tests__/ReactFresh-test.js

There are still 4 tests that have not been passed

**1. refresh/can-handle-implicit-arrow-returns/input.jsx**

Related to #4767. transform correct, just output doesn't match the expected output

**2. refresh/registers-identifiers-used-in-jsx-at-definition-site/input.jsx**
**3. refresh/registers-identifiers-used-in-react-create-element-at-definition-site/input.jsx**

Blocked by #4746

**4. refresh/supports-typescript-namespace-syntax/input.tsx**

oxc transforms ts to js first, so probably we can ignore this case. If we really want to pass this test, we also need to turn off `TypeScript` plugin.

## What's next?

### Options:

1. Support transform `refresh_reg` and `refresh_sig` options to `MemberExpression`. Currently `import.meta.xxxx` still is an `Identifier`
2. Support `emit_full_signatures` option

### Other
NAPI, testing in `monitor-oxc`, etc..
2024-08-15 16:41:30 +00:00
Dunqing
0d7912217a feat(transformer): support logical-assignment-operators plugin (#4890)
part of #4754

The implementation copy from the original implementation which removed in https://github.com/oxc-project/oxc/pull/2865.
2024-08-15 10:10:36 +00:00
Dunqing
ab1d08ccfb feat(transformer): support optional-catch-binding plugin (#4885)
part of #4754

The implementation copy from the original implementation which removed in https://github.com/oxc-project/oxc/pull/2865.
2024-08-15 10:10:34 +00:00
Dunqing
69da9fda3a feat(transformer): support nullish-coalescing-operator plugin (#4884)
The implementation copy from the original implementation which removed in https://github.com/oxc-project/oxc/pull/2865.
2024-08-15 10:10:33 +00:00
Dunqing
3a66e5843d feat(transformer): support exponentiation operator plugin (#4876)
The implementation copy from the original implementation which removed in https://github.com/oxc-project/oxc/pull/2865.
2024-08-15 10:10:32 +00:00
Dunqing
62f759c1f2 fix(transformer/typescript): generated assignment for constructor arguments with access modifiers should be injected to the top of the constructor (#4808)
fix: #4789
2024-08-10 11:21:45 +00:00
Dunqing
3987665490 fix(transformer/typescript): incorrect enum-related symbol_id/reference_id (#4660)
part of #4581
2024-08-06 02:57:19 +00:00
Dunqing
03c643a8af fix(semantic): incorrect scope_id for catch parameter symbols (#4659)
When we move all the bindings in the CatchClause scope to its child scope (BlockStatement), we also need to replace the scope_id in the symbol with the scope_id of the new scope.
2024-08-06 02:57:18 +00:00
Dunqing
5327acdc1f fix(transformer/react): the require IdentifierReference does not have a reference_id (#4658)
part of #4581. Looks we had to pass `TraverseCtx` to the `ModuleImports`
2024-08-05 22:55:07 +00:00
Dunqing
48031ada93 fix(transformer/typescript) shadowed imports have not been removed (#4550)
close: #4423
2024-08-05 03:15:14 +00:00
Dunqing
4efd54b84d fix(transformer/typescript): incorrect SymbolFlags for jsx imports (#4549)
All `SymbolFlags` for imports should be `SymbolFlags::Import`
2024-08-05 03:15:13 +00:00
Dunqing
3a6e4d91cf feat(transformer_conformance): test ScopeTree and SymbolTable for correctness after transformation (#4581)
part of #4549

This test verifies that the correct symbols and scopes were added/changed during the transformation. From the snapshots, most of them are incorrect currently
2024-08-04 11:45:10 +00:00
Dunqing
ecdee88cfb
fix(transformer/typescript): incorrect eliminate exports when the referenced symbol is both value and type (#4507) 2024-07-27 12:11:29 -04: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
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
Boshen
83bd40db4e
chore: turn off doctest for all [[bin]] 2024-07-14 16:55:19 +08:00
Boshen
e167ef79c6 fix(codegen): print parenthesis properly (#4245)
`TSParenthesizedType` handles parenthesis in ts types.

It should be considered a bug if parenthesis is not printed correctly after this PR.
2024-07-14 04:13:10 +00:00
Don Isaac
4413e2d298
fix(transformer): missing initializer for readonly consructor properties (#4103) 2024-07-08 12:45:50 +08:00
Boshen
aaac2d8775
fix(codegen): preserve parentheses from AST instead calculating from operator precedence (#4055)
…operator precedence

Calculating from operator precedence is currently unsafe and will result
incorrect semantics.
2024-07-05 14:01:17 +08:00
Dunqing
bdee156c5d fix(transformer/typescript): declare class incorrectly preserved as runtime class (#3997)
fix: #3993
2024-07-01 16:04:40 +00:00
Dunqing
a50ce3d299 fix(transformer/typescript): missing initializer for class constructor arguments with private and protected modifier (#3996)
close: #3992
2024-07-01 15:10:32 +00:00
Boshen
5845057bff refactor(transformer): pass in symbols and scopes (#3978)
This PR adds a new method `build_with_symbols_and_scopes` to make semantic building optional, there may be prior steps that has the semantic data already built.
2024-06-30 06:33:48 +00:00
DonIsaac
63f36daae0 feat(parser): parse modifiers with parse_modifiers (take 2) (#3977)
Same as #3948, with fixes for bugs found by @Boshen.
2024-06-30 03:46:34 +00:00
Boshen
14bc31ee74
Revert "feat(parser): parse modifiers with parse_modifiers (#3948)"
This reverts commit 7b38bde073.
2024-06-29 14:16:26 +08:00