Commit graph

4544 commits

Author SHA1 Message Date
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
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
overlookmotel
56d50cf92c fix(transformer): exponentiation transform: do not assume Math is not a local var (#6302)
`Math` is hopefully a global var (and transform will be incorrect if it isn't), but make sure scope tree is correct either way.
2024-10-06 00:51:29 +00:00
overlookmotel
15cc8afd2e refactor(transformer): exponentiation transform: break up into functions (#6301)
Pure refactor.
2024-10-06 00:51:28 +00:00
overlookmotel
bd81c5134c fix(transformer): exponentiation transform: fix duplicate symbols (#6300)
Identifier was being cloned unnecessarily, creating an extra symbol.
2024-10-06 00:51:28 +00:00
overlookmotel
7f5a94b902 refactor(transformer): use Option::get_or_insert_with (#6299)
`Option::get_or_insert_with` is designed for use case where you want to fill an `Option` if it's `None`, and get the contents either way. Use it in transformer where borrow checker allows.
2024-10-06 00:51:27 +00:00
Boshen
8729755baa feat(oxc,napi/transform): napi/transform use oxc compiler pipeline (#6298)
part of #6156
2024-10-05 16:35:09 +00:00
overlookmotel
0cea6e9fa1 refactor(transformer): exponentiation transform: reduce identifier cloning (#6297)
We already have to look up the reference, so use the `SymbolId` from that reference, rather than looking it up again in `clone_identifier_reference`.
2024-10-05 16:16:29 +00:00
overlookmotel
ac7a3edf31 refactor(transformer): logical assignment transform: reduce identifier cloning (#6296)
We already have to look up the reference, so use the `SymbolId` from that reference, rather than looking it up again in `clone_identifier_reference`.
2024-10-05 16:00:51 +00:00
overlookmotel
527f7c82ef refactor(transformer): nullish coalescing transform: no cloning identifier references (#6295)
`clone_identifier_reference` involves a lookup of `SymbolId`. Use `BoundIdentifier` instead which caches the ID.
2024-10-05 16:00:51 +00:00
overlookmotel
409dffc8ab refactor(traverse)!: generate_uid return a BoundIdentifier (#6294)
Closes #5020.

`TraverseCtx::generate_uid` and associated methods return a `BoundIdentifier`, containing both symbol ID and name. This reduces boilerplate code for the caller and avoids and unnecessary lookup to get the name.

Also add a couple of helper methods to `BoundIdentifier` to reduce boilerplate further.
2024-10-05 16:00:50 +00:00
overlookmotel
7b62966813 refactor(transformer): move BoundIdentifier into oxc_traverse crate (#6293)
Pure refactor.
2024-10-05 16:00:49 +00:00
overlookmotel
c7fbf686b9 refactor(transformer): logical assignment operator transform: no cloning identifier references (#6290)
Use `BoundIdentifier` to generate `IdentifierReference`s from, instead of cloning `IdentifierReference`s. This simplifies the code and is a less error-prone pattern.
2024-10-05 14:48:38 +00:00
overlookmotel
06797b6900 fix(transformer): logical assignment operator transform: fix reference IDs (#6289) 2024-10-05 14:48:37 +00:00
overlookmotel
e19deaa102 ci(transformer): move post-transform checker to tasks crate (#6288)
Move post-transform checker into a `tasks` crate. It doesn't feel like it belongs in `oxc_semantic`. It also feels like too heavy a lump of code to put in `tasks/common`.
2024-10-05 14:48:37 +00:00
overlookmotel
d4f2ee99c1 test(transformer): tidy up transform checker (#6287)
Pure refactor. Use `oxc_semantic`'s public APIs and shorten a few lines.
2024-10-05 14:48:36 +00:00
overlookmotel
0f5afd7ede test(transformer): transform checker output symbol name for mismatches (#6286)
Transform checker include symbol names in output for symbol mismatches. This is rather more helpful for locating bugs than just `SymbolId(3)`.
2024-10-05 14:42:06 +00:00
dalaoshu
d953a6be02
fix(minifier): correct the reference link (#6283)
These are minor changes, and recently I’ve been learning and trying to
implement some features of `oxc_minifier`, which feels a bit complex.

By the way, I’m wondering whether we should gradually add test cases
during the feature implementation process or just copy all the
corresponding tests directly? Perhaps we could implement something like
`rulegen` similar to `linter`?
2024-10-05 08:54:59 +08:00
overlookmotel
f0a74ca7c6 refactor(transformer): prefer create_bound_reference_id to create_reference_id (#6282)
`TraverseCtx::create_reference_id` is intended for when you don't know if a reference is bound or not. Replace uses of it with the more specific `create_bound_reference_id` and `create_unbound_reference_id`.
2024-10-04 15:25:31 +00:00
dalaoshu
841317538f
refactor(linter): move shared function from utils to rule (#6127)
I previously extracted the common parts of `prefer_to_be_truthy` and
`prefer_to_be_falsy` into `utils`. However, we should place
rule-specific logic that isn't general-purpose directly within the
respective rules.

Co-authored-by: Don Isaac <donald.isaac@gmail.com>
2024-10-04 23:24:08 +08:00
IWANABETHATGUY
9736aa0112 fix(oxc_transformer): define import.meta and import.meta.* (#6277)
1. related esbuild test
d34e79e2a9/internal/bundler_tests/bundler_default_test.go (L5077-L5119)
2024-10-04 14:22:56 +00:00
overlookmotel
ba3e85b157 refactor(transformer): fix spelling (#6279)
`property` not `proeperty`.
2024-10-04 13:44:59 +00:00
camchenry
50a0029a97 perf(linter): do not concat vec in no-useless-length-check (#6276)
Probably a very small win, but rather than making lots of small vecs and concatenating, we should just use one vec and then push to it as we visit nodes.
2024-10-04 04:41:59 +00:00
camchenry
ba9c372a15 refactor(linter): make jest/vitest rule mapping more clear (#6273)
- Renamed the `map_jest` method to make it more clear what it does, now called `map_jest_rule_to_vitest`
- Tried to use a `phf_set!` over a list of strings for checking if we should map rules. Probably not faster, but should be simpler a constant amount of work if we expand the list of rules in the future.
- Made it easier to not mess up the arguments to the `map_jest` function by making it accept a `RuleWithSeverity` instead of just strings.
2024-10-03 21:46:57 -04:00
camchenry
7ca70ddab6 docs(linter): add docs for ContextHost and LintContext (#6272)
- towards https://github.com/oxc-project/oxc/issues/5870

Adds some very basic docs for methods and properties related to `LintContext` and `ContextHost`.
2024-10-03 21:46:57 -04:00
Boshen
2f888ed871 feat(oxc): add napi transform options (#6268) 2024-10-03 13:36:38 +00:00
7086cmd
37cbabbac4 fix(minifier): should not handle the strict operation for bool comparison. (#6261) 2024-10-03 12:16:10 +00:00
7086cmd
fcb4651819 test(minifier): enable null comparison with bigint. (#6252) 2024-10-03 12:16:09 +00:00
H11
e0a3378864
fix(linter): correct false positive in unicorn/prefer-string-replace-all (#6263)
closes #6259
2024-10-03 12:58:09 +08:00
Boshen
aa0dbb6375 refactor(oxc): add napi feature, change napi parser to use oxc crate (#6265) 2024-10-03 04:28:38 +00:00
7086cmd
e29c0676d6 fix(minifier): handle exceeded shifts. (#6237)
Related: #6161, the last situation.

```js
// https://github.com/tc39/test262/blob/main/test/language/expressions/unsigned-right-shift/S9.6_A2.2.js
test((-2147483649 >>> 0) !== 2147483647)
```
2024-10-03 02:52:05 +00:00
leaysgur
5a73a663dc refactor(regular_expression)!: Simplify public APIs (#6262)
This PR makes 2 changes to improve the existing API that are not very useful.

- Remove `(Literal)Parser` and `FlagsParser` and their ASTs
- Add `with_flags(flags_text)` helper to `ParserOptions`

Here are the details.

> Remove `(Literal)Parser` and `FlagsParser` and their ASTs

Previously, the `oxc_regular_expression` crate exposed 3 parsers.

- `(Literal)Parser`: assumes `/pattern/flags` format
- `PatternParser`: assumes `pattern` part only
- `FlagsParser`: assumes `flags` part only

However, it turns out that in actual usecases, only the `PatternParser` is actually sufficient, as the pattern and flags are validated and sliced in advance on the `oxc_parser` side.

The current usecase for `(Literal)Parser` is mostly for internal testing.

There were also some misuses of `(Literal)Parser` that restore `format!("/{pattern}/{flags}")` back and use `(Literal)Parser`.

Therefore, only `PatternParser` is now published, and unnecessary ASTs have been removed.
(This also obsoletes #5592 .)

> Added `with_flags(flags_text)` helper to `ParserOptions`

Strictly speaking, there was a subtle difference between the "flag" strings that users were aware of and the "mode" recognised by the parser.

Therefore, it was a common mistake to forget to enable `unicode_mode` when using the `v` flag.

With this helper, crate users no longer need to distinguish between flags and modes.
2024-10-03 02:47:08 +00:00
camchenry
5957214f4c feat(linter): allow fixing in files with source offsets (#6197)
- fixes https://github.com/oxc-project/oxc/issues/5913

This PR fixes the calculation of spans when dealing with files that have multiple sources and non-zero source start offsets. This is almost always the case for `.vue`, `.astro`, and `.svelte` files. This enables us to correctly apply fixes for these file types both in the CLI with `oxlint` and also in editors like VS Code.

https://github.com/user-attachments/assets/2836c8bd-09be-4e59-801d-7c95f8c2491f

I'm open to ideas on how to improve testing in this area, as I don't think that we currently have any tests for fixing files end-to-end (beyond what the linter rules check). I did run this locally on the gitlab repository (which is written in Vue) and all of the fixes appeared to be applied correctly.
2024-10-03 00:53:30 +00:00
overlookmotel
32d972e2a4 refactor(parser)!: treat unambiguous files containing TS export assignments as modules (#6253)
An "unambiguous" TS source which contains an `export = <value>;` statement should be interpreted as an ES module.

This is in line with [this Babel test case](94e166782a/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous) which has [input option of `sourceType: "unambiguous"`](94e166782a/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/options.json (L2)), and [outputs an AST with `sourceType: "module"`](94e166782a/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/output.json (L7)).
2024-10-03 00:28:00 +00:00
overlookmotel
4f6bc79734 refactor(transformer)!: remove source_type param from Transformer::new (#6251)
Closes #6248.
2024-10-03 00:21:01 +00:00
7086cmd
115ccc941e feat(minifier): bitwise not in exceeded value. (#6235)
```rs
        test("x = ~2147483658.0", "x = 2147483637");
        test("x = ~-2147483658", "x = -2147483639");
```

Used `wrapping_neg`, and maybe it is a great solution for #6161.
2024-10-03 00:16:05 +00:00
overlookmotel
bc757c89b4 refactor(transformer): move functionality of common transforms into stores (#6243)
In common transforms, move all functionality into the `*Store` structs. The `Traverse` impls become just thin wrappers which call into methods on the `*Store` structs.

I think this is clearer because now reading these files from top-to-bottom, you have the code that inserts into the stores before the code that reads from the stores and acts on it.
2024-10-02 14:13:48 +00:00
7086cmd
ee6c85003d feat(minifier): scaffold peephole replace known methods. (#6245) 2024-10-02 14:06:05 +00:00
7086cmd
c32af57e13 feat(minifier): fold demical bitwise not for bigint. (#6233)
Only demical first, because the rest bases are not natively supported by transforming from raw str.
2024-10-02 13:44:20 +00:00
7086cmd
23b646484c feat(minifier): fold true / false comparison. (#6225)
Input:
```js
a == false
```
Previous:
```js
a == !1
```
Current:
```js
a == 0
```

Only handle it when it is non-plus, non-relation binary expressions. Align with [Closure Compiler](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520SIMPLE_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250Ax%2520%253C%253C%2520true%253B%250A%250Ax%2520%252B%2520true%253B%250A%250Ax%2520-%2520true%253B%250A%250Ax%2520%257C%2520true%253B%250A%250Ax%2520%2525%2520true%253B%250A%250Ay%2520!%253D%2520false%253B%250A%250Af()%2520%253D%253D%2520false%253B%250A%250Ax%2520instanceof%2520true%250A%250Ax%2520**%2520true%250A%250Ax%2520%2526%2520true%250A%250Ax%2520%255E%2520false%250A%250Ax%2520%253D%253D%2520(x%2520instanceof%2520false)%250A%250Ax%2520instanceof%2520(x%2520%253C%253C%2520true)%250A%250Ax%2520%253D%253D%2520fake(false)).
2024-10-02 13:37:17 +00:00
leaysgur
acab777c0a refactor(regular_expression): Misc fixes (#6234)
Preparation for #6141

- Keep `enum` size + add size asserts tests
- Arrange AST related directories
- Renaming
2024-10-02 13:32:29 +00:00
overlookmotel
1c31932f03 refactor(transformer): rename var in VarDeclarations common transform (#6242)
Pure refactor. Just rename a variable for clarity.
2024-10-02 11:38:27 +00:00
7086cmd
585ccdad8c feat(minifier): support subtraction assignment. (#6214)
Due to the potential for string concatenation when using the `+=` operator, we should only handle the scenario when using the `-=` operator.
2024-10-02 01:42:56 +00:00
dalaoshu
ea28ee9eda
fix(linter): improve the fixer of prefer-namespace-keyword (#6230)
closes #6204
2024-10-02 09:41:22 +08:00
yoho
a089e19cf9
feat(linter): eslint/no-else-return (#4305) 2024-10-01 19:09:58 -04:00