Commit graph

6015 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
Dunqing
585cdd5bcd chore(codegen): add more tests for comments from esbuild (#6357)
Copy from https://github.com/rolldown-rs/rolldown/blob/main/crates/rolldown/tests/esbuild/dce/remove_unused_pure_comment_calls/entry.js
2024-10-08 05:42:43 +00:00
Dunqing
84b2d072e8 fix(codegen): converts line comment to block comment if it is a PURE comment (#6356)
The last part of fixing https://github.com/rolldown/rolldown/pull/2375/files#r1789011257

In the following case, the pure comment was written by `//`
```ts
const Component = // #__PURE__
React.forwardRef((props, ref) => {});
```

The printed code looks like this
```ts
const Component =
// #__PURE__ React.forwardRef((props, ref) => {});
```

As you can see, it is broken because the code also commented, so we need to replace `//` with `/* */`
2024-10-08 05:42:42 +00:00
Dunqing
1380d8b5dc fix(parser): should regard comments where after = as leading comments of next token (#6355)
A part of fixing https://github.com/rolldown/rolldown/pull/2375/files#r1789011257

### Compare to `Babel`
This is also among its behavior, see

<img width="1470" alt="image" src="https://github.com/user-attachments/assets/1b1f9c78-3e44-495d-b2f7-38e6c3bc8bf1">

### Compare to `esbuild`

I haven't looked through related code from esbuild, from its playground, the behavior looks the same

https://esbuild.github.io/try/#dAAwLjI0LjAAAGNvbnN0IEEgPSAvLyBzZGRkZGQKKCkgPT4ge30
2024-10-08 05:42:41 +00:00
oxc-bot
62d45bf14e
release(oxlint): v0.9.10 (#6340)
## [0.9.10] - 2024-10-07

### Features

- f272137 editors/vscode: Clear diagnostics on file deletion (#6326)
(dalaoshu)
- 1a5f293 editors/vscode: Update VSCode extention to use project's
language server (#6132) (dalaoshu)
- 376cc09 linter: Implement `no-throw-literal` (#6144) (dalaoshu)
- 5957214 linter: Allow fixing in files with source offsets (#6197)
(camchenry)
- a089e19 linter: Eslint/no-else-return (#4305) (yoho)
- 183739f linter: Implement prefer-await-to-callbacks (#6153) (dalaoshu)
- ae539af linter: Implement no-return-assign (#6108) (Radu Baston)

### Bug Fixes

- 9e9808b linter: Fix regression when parsing ts in vue files (#6336)
(Boshen)
- 93c6db6 linter: Improve docs and diagnostics message for
no-else-return (#6327) (DonIsaac)
- e0a3378 linter: Correct false positive in
`unicorn/prefer-string-replace-all` (#6263) (H11)
- ea28ee9 linter: Improve the fixer of `prefer-namespace-keyword`
(#6230) (dalaoshu)
- f6a3450 linter: Get correct source offsets for astro files (#6196)
(camchenry)
- be0030c linter: Allow whitespace control characters in
`no-control-regex` (#6140) (camchenry)
- e7e8ead linter: False positive in `no-return-assign` (#6128)
(DonIsaac)

### Performance

- ac0a82a linter: Reuse allocator when there are multiple source texts
(#6337) (Boshen)
- 50a0029 linter: Do not concat vec in `no-useless-length-check` (#6276)
(camchenry)

### Documentation

- 7ca70dd linter: Add docs for `ContextHost` and `LintContext` (#6272)
(camchenry)
- a949ecb linter: Improve docs for `eslint/getter-return` (#6229)
(DonIsaac)
- 14ba263 linter: Improve docs for `eslint-plugin-import` rules (#6131)
(dalaoshu)

### Refactor

- 642725c linter: Rename vars from `ast_node_id` to `node_id` (#6305)
(overlookmotel)
- 8413175 linter: Move shared function from utils to rule (#6127)
(dalaoshu)
- ba9c372 linter: Make jest/vitest rule mapping more clear (#6273)
(camchenry)
- 82b8f21 linter: Add schemars and serde traits to AllowWarnDeny and
RuleCategories (#6119) (DonIsaac)
- ea908f7 linter: Consolidate file loading logic (#6130) (DonIsaac)
- db751f0 linter: Use regexp AST visitor in `no-control-regex` (#6129)
(camchenry)
- 3aa7e42 linter: Use RegExp AST visitor for `no-hex-escape` (#6117)
(camchenry)
- 9d5b44a linter: Use regex visitor in `no-regex-spaces` (#6063)
(camchenry)
- 0d44cf7 linter: Use regex visitor in `no-useless-escape` (#6062)
(camchenry)
- eeb8873 linter: Use regex visitor in `no-empty-character-class`
(#6058) (camchenry)

### Testing

- d883562 linter: Invalid `eslint/no-unused-vars` options (#6228)
(DonIsaac)

---------

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-10-08 12:14:54 +08:00
overlookmotel
4fd89e8eed test(transformer): fix indentation in transformer test fixtures (#6346)
Unimportant nit. Fix whitespace in the transformer test fixtures.
2024-10-08 01:32:07 +00:00
overlookmotel
cf20f3a89d feat(transformer): exponentiation transform: support private fields (#6345)
Babel doesn't support private fields in this transform, but there's no reason not to. So we can.
2024-10-08 01:32:06 +00:00
overlookmotel
bd5fb5ab5c refactor(transformer): exponentiation transform: rename methods (#6344)
Pure refactor. Rename methods only. I named them badly in #6338.
2024-10-08 01:32:06 +00:00
overlookmotel
4aa4e6bec0 refactor(transformer): exponentiation transform: do not wrap in SequenceExpression if not needed (#6343)
`left **= right` is transformed to `left = Math.pow(left, right)`. There is no need to wrap it in a `SequenceExpression`.
2024-10-08 01:32:05 +00:00
overlookmotel
0dd9a2ecf3 refactor(traverse): add helper methods to BoundIdentifier (#6341)
Add helper methods to `BoundIdentifier` for common operations - creating an `IdentifierReference` as an `Expression` or an `AssignmentTarget`.
2024-10-08 01:32:05 +00:00
DonIsaac
00df6e5ccb fix(linter): friendly diagnostic messages for no-else-return (#6349)
### Before

```
  ⚠ eslint(no-else-return): Unnecessary 'else' after 'return'.
   ╭─[no_else_return.tsx:1:48]
 1 │ function foo1() { if (true) { return x; } else { return y; } }
   ·                                                ─────────────
   ╰────
  help: Replace ` else { return y; }` with `  return y; `.
```

### After

```
  ⚠ eslint(no-else-return): Unnecessary 'else' after 'return'.
   ╭─[no_else_return.tsx:1:31]
 1 │ function foo1() { if (true) { return x; } else { return y; } }
   ·                               ────┬────  ───┬──
   ·                                   │         ╰── Making this `else` block unnecessary.
   ·                                   ╰── This consequent block always returns,
   ╰────
  help: Remove the `else` block, moving its contents outside of the `if` statement.
```
2024-10-08 01:27:00 +00:00
DonIsaac
40932f79b1 refactor(cfg): use IndexVec for storing basic blocks (#6323)
Use an `IndexVec` when storing basic blocks. This makes the link between nodes in `.graph` and elements of `.basic_blocks` more clear. I had to rename `BasicBlockId` to `BlockNodeId` to avoid a name collision. I wasn't sure what else to name the `Idx` type for the basic blocks vec.
2024-10-07 19:28:40 -04:00
DonIsaac
71ad5d3e3f fix(linter): no-else-return fixer fails when else has no trailing whitespace (#6348)
Fixes a bug in `eslint/no_else_return`'s fixer where fixes were not being
property applied when `else` had no whitespace immediately after it. For
example:
```js
if(x){ return x }else{ return y }
```

I also refactored the rule's fixer to avoid string allocations as much as
possible.
2024-10-07 23:19:54 +00:00
DonIsaac
a1e0d30f20 refactor(cfg): add type alias for Graph (#6322)
Pure refactor. No logic changes
2024-10-07 15:28:30 +00:00
DonIsaac
95ca01ccc1 refactor(cfg)!: make BasicBlock::unreachable private (#6321)
Protect `unreachable` property of basic blocks in preparation of upcoming
refactors. This is technically a breaking change.
2024-10-07 15:13:56 +00:00
overlookmotel
a15235ac14 refactor(transformer): exponentiation transform: no cloning (#6338)
Exponentiation Operator transform was cloning a lot of values unnecessarily. This is not great for performance and would also have produced incorrect symbol IDs, as `clone_in` loses scope/symbol information.

Instead just move parts of the original AST around, without cloning.

Also split up the transform and document it more clearly.
2024-10-07 15:06:56 +00:00
Boshen
ac0a82af71 perf(linter): reuse allocator when there are multiple source texts (#6337) 2024-10-07 14:43:23 +00:00
Boshen
9e9808b429 fix(linter): fix regression when parsing ts in vue files (#6336) 2024-10-07 14:28:44 +00:00
Boshen
f243cf23c6
chore(linter): change NoElseReturn and NoThrowLiteral to pedantic 2024-10-07 22:02:33 +08:00
dalaoshu
376cc092ee
feat(linter): implement no-throw-literal (#6144) 2024-10-07 09:17:35 -04:00
7086cmd
e304e8cd2b feat(minifier): minify exponential arithmetic operation. (#6281) 2024-10-07 12:59:27 +00:00
7086cmd
ac5a23fd34 refactor(minifier): use ctx.ast.vec instead of Vec::new. (#6331) 2024-10-07 12:25:35 +00:00
7086cmd
f9ae70c74a feat(minifier): minify basic arithmetic calculations. (#6280)
It uses to_string to check which is shorter, which is extremely tough. Waiting for further refactor.
2024-10-07 10:41:06 +00:00
overlookmotel
2bcd12a868 fix(transformer): exponentiation transform: fix reference flags (#6330) 2024-10-07 10:35:34 +00:00
7086cmd
964d71e1d2 test(minifier): add arithmetic tests for fold constants. (#6269) 2024-10-07 09:37:30 +00:00
Boshen
febedffc4b
chore: Update README.md 2024-10-07 17:11:10 +08:00
overlookmotel
28cbfa7c64 fix(transformer): exponentiation transform: fix temp var names (#6329)
Fix one case that I missed in #6318.
2024-10-07 09:00:35 +00:00
overlookmotel
c7636d7213 docs(traverse): remove erroneous doc comment (#6328)
`TraverseCtx::generate_uid_in_current_scope_based_on_node` no longer delegates to a method on `TraverseScoping`, so this comment was outdated.
2024-10-07 07:35:00 +00:00
camchenry
4008afe512 feat(minifier): fold array and object constructors (#6257)
This will fold expressions like `new Object()` to `{}`, and `new Array()` to `[]`. Based on the closure compiler tests: b7e380b632/test/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntaxTest.java (L78).

This is outside my usual area, so feedback is welcome.

NOTE: this was previously a full stack of PRs, but Graphite decided to stop working completely for some reason and only gave me this error when I submitted a PR:
```
ERROR: Failed to submit PR for 10-02-feat_minifier_fold_single_arg_new_array_expressions:
{}
```
so I decided to just completely remake this stack and submit as 1 PR.
2024-10-07 06:02:07 +00:00
DonIsaac
93c6db6550 fix(linter): improve docs and diagnostics message for no-else-return (#6327) 2024-10-07 05:54:59 +00:00
Tim Fish
f6e42b6d85
feat(sourcemap): Add support for sourcemap debug IDs (#6221)
The sourcemap [`debugId`
proposal](https://github.com/tc39/source-map/blob/main/proposals/debug-id.md)
adds globally unique build or debug IDs to source maps and generated
code, making build artifacts self-identifying.

Support for debug IDs was added to
[`rust-sourcemap`](https://github.com/getsentry/rust-sourcemap/pull/66)
in 2023 and Sentry have made use of this to aid in matching up source
and sourcemap files without having to worry about path mismatches or
release versions.

I want to add debug ID support to Rolldown but it uses `oxc::sourcemap`
so it looks like I need to start here first!
2024-10-07 13:54:08 +08:00
Boshen
9e62396803 feat(syntax_operations): add crate oxc_syntax_operations (#6202)
I intend to add constant folding and eval logic to this crate. There are downstream tools that require these functionalities alone.

It's also reasonable to move these traits out of the `ast` crate.
2024-10-07 05:39:59 +00:00
DonIsaac
fa4d505b92 feat(cfg): derive more base traits for CFG blocks (#6320) 2024-10-07 05:01:07 +00:00
DonIsaac
7672793542 refactor(cfg): move block data types to separate file (#6319)
Pure refactor. No logic has been changed.
2024-10-07 05:01:06 +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
dalaoshu
f2721375ea
feat(editors/vscode): clear diagnostics on file deletion (#6326)
closes #6325
2024-10-07 10:42:01 +08:00
overlookmotel
03bc041ddf refactor(syntax): remove some unsafe code creating IDs (#6324) 2024-10-06 23:26:26 +00:00
overlookmotel
3a4bcc77fc fix(transformer): exponentiation transform: fix temp var names (#6318)
Make naming of temp vars follow Babel. It wasn't apparent that our version of this transform was behaving differently from Babel because the Babel plugin has very few tests. The added tests replicate Babel's output.
2024-10-06 23:08:10 +00:00
overlookmotel
c0e2fef0af refactor(traverse): function to get var name from node (#6317)
Pure refactor. Separate out the logic for creating a var name from an AST node into its own function, so it can be used standalone, outside of `generate_uid_based_on_node`.

Apart from the new `get_var_name_from_node` function, and changing return type of `to_identifier` to `String`, the only other changes are moving files around.
2024-10-06 23:08:09 +00:00
overlookmotel
7d93b25221 refactor(transformer): exponentiation transform: split into 2 paths (#6316)
Exponentiation transform has 2 paths for when left-hand side of `**=` is an identifier, and when it's a member expression. But these two paths are mixed up together in the `convert_assignment_expression`, `explode`, and `get_obj_ref` functions, with each branching on the same condition (is it an identifier or a member expression?).

Refactor to separate out these 2 code paths, and make the logic easier to follow.
2024-10-06 23:08:09 +00:00
overlookmotel
eb1d0b8838 docs(transformer): exponentiation transform: update doc comments (#6315)
Just update docs for this transform.
2024-10-06 23:08:08 +00:00
overlookmotel
ccb7bdcc56 fix(transformer): exponentiation transform: do not replace object when private property (#6313)
Fix exponentiation operator transform to bail out early if a private class property.

We can't transform this:

```js
class C {
    #p;
    method() {
        this.#p **= 2;
    }
}
```

But we should at least leave it alone. Previously `get_obj_ref` called `ast.move_expression(expr)` on the member expression's object before bailing out, so example above was transformed to:

```js
class C {
    #p;
    method() {
        null.#p **= 2; // <-- `null`
    }
}
```

This PR makes it spot this case early and bail out *before* calling `ast.move_expression(expr)`.
2024-10-06 23:08:08 +00:00
Alexander S.
fe25b651bd
feat(prettier): indent for class definition (#6059)
trying to match the output for:
5b868377c0/tests/format/typescript/classes/__snapshots__/format.test.js.snap (L3-L92)

<details><summary>main branch output:</summary>

```typescript
class MyContractSelectionWidget extends React.Component<void, MyContractSelectionWidgetPropsType, void> implements SomethingLarge {
  method() {}
}

class DisplayObject1 extends utils.EventEmitter implements interaction_InteractiveTarget {}

class DisplayObject2 extends utils.EventEmitter implements interaction_InteractiveTarget {}

class DisplayObject3 extends utils.EventEmitter implements interaction_InteractiveTarget, somethingElse_SomeOtherThing, somethingElseAgain_RunningOutOfNames {}

class DisplayObject4 extends utils.EventEmitter implements interaction_InteractiveTarget {}
class Readable extends events.EventEmitter implements NodeJS_ReadableStream {}
class InMemoryAppender extends log4javascript.Appender implements ICachedLogMessageProvider {}

class Foo extends Immutable.Record({
  ipaddress: "",
}) {
  ipaddress: string;
}

export class VisTimelineComponent implements AfterViewInit, OnChanges, OnDestroy {}
export class VisTimelineComponent2 implements AfterViewInit, OnChanges, OnDestroy, AndSomethingReallyReallyLong {}
```

</details> 

<details><summary>this branch output:</summary>

```typescript
class MyContractSelectionWidget
  extends React.Component<void, MyContractSelectionWidgetPropsType, void>
  implements SomethingLarge
{
  method() {}
}

class DisplayObject1
  extends utils.EventEmitter
  implements interaction_InteractiveTarget {}

class DisplayObject2
  extends utils.EventEmitter
  implements interaction_InteractiveTarget {}

class DisplayObject3
  extends utils.EventEmitter
  implements
    interaction_InteractiveTarget,
    somethingElse_SomeOtherThing,
    somethingElseAgain_RunningOutOfNames {}

class DisplayObject4
  extends utils.EventEmitter
  implements interaction_InteractiveTarget {}
class Readable
  extends events.EventEmitter
  implements NodeJS_ReadableStream {}
class InMemoryAppender
  extends log4javascript.Appender
  implements ICachedLogMessageProvider {}

class Foo extends Immutable.Record({
  ipaddress: "",
}) {
  ipaddress: string;
}

export class VisTimelineComponent
  implements AfterViewInit, OnChanges, OnDestroy {}
export class VisTimelineComponent2
  implements
    AfterViewInit,
    OnChanges,
    OnDestroy,
    AndSomethingReallyReallyLong {}
```

</details> 

Sadly I can't fix the `class Readable` line :/
2024-10-06 22:23:24 +08:00
Boshen
abd3a9fe11 feat(napi/transform): perform dce after define plugin (#6312) 2024-10-06 09:49:52 +00:00
overlookmotel
642725cd7d refactor(linter): rename vars from ast_node_id to node_id (#6305)
Style nit. We renamed `AstNodeId` to `NodeId`, so rename vars from `ast_node_id` to `node_id` too.
2024-10-06 08:35:51 +00:00
Boshen
020bb80b65 refactor(codegen)!: change to CodegenReturn::code and CodegenReturn::map (#6310) 2024-10-06 05:05:47 +00:00
Boshen
a0ccc26c12 feat(napi/transform): add lang option to change source type (#6309)
part of #6274 and #6156

```
    /// Treat the source text as `js`, `jsx`, `ts`, or `tsx`.
    #[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")]
    pub lang: Option<String>,
```
2024-10-06 04:53:47 +00:00
Boshen
5b5daec392 refactor(napi): use vitest (#6307) 2024-10-06 02:57:48 +00:00
Boshen
58a8615747 refactor(napi/transform): remove context (#6306) 2024-10-06 01:57:49 +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