Commit graph

5607 commits

Author SHA1 Message Date
overlookmotel
d74c7fa0c9 fix(transformer): remove AstBuilder::copy from arrow functions transform (#5825)
Remove `AstBuilder::copy` calls by taking ownership of the `ArrowFunctionExpression` at start of transform.
2024-09-18 02:23:33 +00:00
Alexander S.
c6d97e9366
feat(prettier): improve ts class output (#5835)
please look first at  #5834 :)
2024-09-17 17:33:35 -04:00
Alexander S.
6d9ccdda92
feat(prettier): support TSMappedType (#5834) 2024-09-17 16:25:51 -04:00
dalaoshu
026ee6a7e8
refactor(linter): decouple module resolution from import plugin (#5829)
closes #5815
2024-09-17 15:48:03 -04:00
overlookmotel
85ac3f749a refactor(transformer): arrow functions transform do not wrap function expressions in parentheses (#5824)
Wrapping a function expression in parentheses is unnecessary. Codegen already adds parentheses when printing a function expression as a statement expression. i.e. `(function() {});`.
2024-09-17 14:11:05 +00:00
Boshen
5901d2a0f1 fix(codegen): various spacing issues (#5820) 2024-09-17 09:03:28 +00:00
Alexander S.
18e4ac22ad
feat(prettier): improve TSInterfaceDeclaration (#5817)
last one for today :)

---------

Co-authored-by: Don Isaac <donald.isaac@gmail.com>
2024-09-17 14:41:33 +08:00
DonIsaac
f9424857df fix(linter): remove all* remaining "Disallow <foo>" messages (#5812)
Cleans diagnostic messages and help texts for (almost) all remaining rules.
There were two rules I couldn' find good replacements for.

I also made some logical improvements to a few rules, and added `pending` fixers
to others.
2024-09-17 05:46:16 +00:00
DonIsaac
3bf7b24621 feat(linter): make typescript/no-duplicate-enum-values a correctness rule (#5810)
This rule is enabled by default in typescript-eslint's recommended config, so we
should enable it by default too. I checked this change with `oxlint-ecosystem-ci` locally and saw no problems.

This PR also improves diagnostic messages for this rule.
2024-09-17 05:46:16 +00:00
DonIsaac
a5f2e9ac04 feat(span): impl From<Atom<'a>> for Atom (#5809) 2024-09-17 05:46:16 +00:00
DonIsaac
b5ad518621 fix(linter): improve diagnostic messages for various lint rules (#5808)
This is the first of a few PRs to remove "Disallow <x>" from all diagnostic
messages. More to come.
2024-09-17 05:46:15 +00:00
DonIsaac
858f7af2b5 fix(linter): plugin prefix name for eslint-plugin-node (#5807) 2024-09-17 05:46:15 +00:00
DonIsaac
3725d5d44e perf(linter): make all rules share a diagnostics vec (#5806)
## What This PR Does

Each time a lint rule is run on a file, it will now store diagnostics in a
shared vec instead of having its own list. This is done by moving `diagnostics`
from `LintContext` to `ContextHost`.

The motivating line of code can be found in `Linter::run` in
`oxc_linter/src/lib.rs#145`

```rust
rules.into_iter().flat_map(|(_, ctx)| ctx.into_message()).collect::<Vec<_>>()
```

Each `LintContext` allocates a new vec, and pushes diagnostics into it. After
all run-related methods have been run, a new vec is created and those
diagnostics are copied into it. This is `O(n+1)` allocations and `O(n)` copies,
not to mention that allocations for the original vecs cannot be re-used since
those vecs aren't dropped until after the new one is created.
2024-09-17 05:46:15 +00:00
DonIsaac
e04841c6a1 docs(syntax): add ModuleRecord documentation (#5818) 2024-09-17 05:22:01 +00:00
DonIsaac
50834bc6d2 refactor(linter): move override_rule to OxlintRules (#5708) 2024-09-17 05:15:38 +00:00
DonIsaac
a438743191 refactor(linter): move OxlintConfig to Oxlintrc (#5707) 2024-09-17 05:15:37 +00:00
Alexander S.
b5ac5a6bf0
feat(prettier): support TSModuleDeclaration (#5813)
Goal of this PR is to resolve this test:
aa3853b776/tests/format/typescript/interface/separator.ts
2024-09-16 16:39:42 -04:00
Cam McHenry
7799c06c1b
feat(linter/react): Implement no-danger-with-children rule (#5420)
- Part of https://github.com/oxc-project/oxc/issues/1022

This implements the [recommended rule
`no-danger-with-children`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-danger-with-children.md)
from the `eslint-plugin-react` package.

This rule proved to be a little bit trickier to implement than I
anticipated, as it required searching up through all of the scopes
recursively in order to resolve object properties. This would be easier
with something like a type-check system, but this will work for now.

(Sidenote: this is my first real attempt at Rust programming in ~5
years, so any feedback on making this more idiomatic is welcome.)
2024-09-16 13:33:00 -04:00
Alexander S.
8b1485bb00
feat(prettier): improve ClassMemberish with print_class_property (#5811) 2024-09-16 13:32:20 -04:00
Alexander S.
01f8c1bca8
feat(prettier): support TSIntersectionType and TSUnionType (#5794) 2024-09-16 13:09:30 -04:00
Boshen
8e7556f842 feat(parser): calculate leading and trailing position for comments (#5785) 2024-09-16 16:37:35 +00:00
overlookmotel
31e9db442d refactor(parser): shorten UniquePromise code (#5805)
Pure refactor. Shorten code a little.
2024-09-16 15:37:01 +00:00
overlookmotel
2322b8b713 refactor(parser): remove dead code warning when running tests (#5804)
`UniquePromise::new_for_tests` is not used in tests, so produces a dead code warning when running tests. Prevent that.

Also, rename it to `new_for_benchmarks`, since that's where it's used.
2024-09-16 15:37:00 +00:00
overlookmotel
47d9ad8cbf refactor(semantic): remove unused vars warning in release mode (#5803)
Avoid an `unused_variables` warning in release mode for a variable which is only used in debug mode.
2024-09-16 15:32:36 +00:00
Boshen
bb95306efd refactor(codegen): change annotation comment tests to snapshot (#5800) 2024-09-16 05:03:43 +00:00
DonIsaac
e978567659 perf(linter): shrink size of DisableDirectives (#5798)
Use boxed slices instead of vecs, shrinking `DisableDirectives` from 136 to 120
bytes.
2024-09-16 04:22:08 +00:00
DonIsaac
1bfa515088 perf(linter): remove redundant clone of diagnostics in context (#5797) 2024-09-16 04:22:07 +00:00
DonIsaac
e413cad0d7 perf(linter): move shared context info to ContextHost (#5795)
> Related to #5770

## What This PR Does

Moves state that is constant over a linted file out of `LintContext` and into a shared `ContextHost` struct, turning `LintContext` into a [flyweight](https://en.wikipedia.org/wiki/Flyweight_pattern).

This brings `LintContext` from 144 bytes down to 96. `Linter::run` iterates over `(rule, ctx)` pairs in a very tight loop, and each rule instance gets its own clone of `ctx`. A smaller `LintContext` means better cache locality and a smaller heap allocation for this vec. I'm hoping to eventually get it small enough to fit in a single cache line.

I made a PR a while ago that did something similar to this one, but instead of using an `Rc`, each `LintContext` stored a read-only reference. This added an extra lifetime parameter, making it slightly unwieldy, but I saw up to 15%/25% perf improvements on local benchmarks. I'll dig around for it and add a link shortly.

### Architecture
![image](https://github.com/user-attachments/assets/9e8352ae-a581-46a3-a578-9eb855d4ebaf)
----
![image](https://github.com/user-attachments/assets/49213cd9-3c31-40dc-97ad-ddf010705ab6)
2024-09-16 04:22:07 +00:00
overlookmotel
17cd9038b7 refactor(ast): move functions to top level in ast macro (#5793)
Pure refactor. Convert nested functions to top level, to make it easier to read.
2024-09-16 04:10:31 +00:00
overlookmotel
cf97f6d1ad refactor(ast): import syn types in ast macro (#5792)
Pure refactor. Import `syn` types with a use statement. This allows shortening the rest of the code, which, in my opinion, makes it easier to read.
2024-09-16 04:10:30 +00:00
Arian94
737ba1d9d8
fix(linter): fix some cases on ``AssignmentExpression` for `unicorn/consistent-function-scoping`` (#5675)
This is to fix the cases mentioned in the comment section of #5365.

In short, it will report these as PASS test cases:

```js
let inner;

function foo1() {
  inner = function() {}
}
function foo2() {
  inner = function() {}
}
```

```js
let inner;

function outer() {
  inner = function() {}
}
```

And report these below as FAIL test cases:

```js
let inner;

function foo1() {
  inner = function smth() {}
}
function foo2() {
  inner = function bar() {}
}
```

```js
let inner;

function outer() {
  inner = function inner() {}
}
```

The case below was already done in #5312 but mentioned in #5365. It is a
FAIL case as well:
```js
function outer() {
  const inner = function inner() {}
}
```

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Don Isaac <donald.isaac@gmail.com>
2024-09-16 00:09:59 -04:00
overlookmotel
dc10eaf047 refactor(ast): split ast macro into multiple files (#5791)
Pure refactor. Split implementation of `#[ast]` macro into multiple files. This means each file works with a single version of `TokenStream`, rather than having `proc_macro::TokenStream` and `proc_macro2::TokenStream` both in use in a single file.
2024-09-16 04:03:49 +00:00
Alexander S.
52c64093f9
feat(prettier): support typescript enums (#5790)
The last failed test is only because of the comments. the oxc prettier
output is:

```
enum Direction {
  Up = 1,
  Down,
  Left,
  Right,
}

enum FileAccess {
  None,
  Read = // constant members
  1 << 1,
  Write = 1 << 2,
  ReadWrite = Read | Write,
  G = // computed member
  "123".length,
}

enum Empty {}

const enum Enum {
  A = 1,
  B = A * 2,
}
```
Expected output:
aa3853b776/tests/format/typescript/enum/__snapshots__/format.test.js.snap (L91-L99)

Hope you can tell more why this happens :)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-09-16 11:58:04 +08:00
Arian94
d1d54a6ef3
fix(eslint-plugin-jsx-a11y(anchor-is-valid)): make the rule work correctly and improve error grammar (#5781)
fixes #5746.
2024-09-15 23:23:33 -04:00
Boshen
213dbc04ff chore(transform_conformance): do not print comments yet (#5788) 2024-09-15 15:48:58 +00:00
Boshen
033b907359 feat(ast)!: apply #[non_exhaustive], must use AstBuilder (#5787) 2024-09-15 15:43:27 +00:00
Boshen
1c1353bed5 refactor(transformer): use AstBuilder instead of using struct constructor (#5778) 2024-09-15 14:58:56 +00:00
kaykdm
148c7a8b82
fix(linter): replace bitwise AND (&) with logical AND (&&) in explici… (#5780)
…t_function_return_type rule

This PR fixes an issue where the Bitwise AND (&) operator was mistakenly
used in a conditional statement, instead of the Logical AND (&&)
operator.
2024-09-15 19:36:13 +08:00
Boshen
e613a3d877 refactor(codegen): prepare to add leading comments by adding a template method pattern (#5784) 2024-09-15 09:41:53 +00:00
Boshen
6dd6f7ca26 refactor(ast): change Comment struct (#5783) 2024-09-15 09:22:58 +00:00
Boshen
512be65fa7 refactor(coverage): remove sourcemap snapshot (#5782) 2024-09-15 08:56:54 +00:00
Boshen
91bb06e6ff
chore(wasm/parser): remove wasm-pack dependency (#5777)
It should be installed via `cargo binstall` for consistency.
2024-09-15 11:27:48 +08:00
overlookmotel
3230ae5842 feat(semantic): add SemanticBuilder::with_excess_capacity (#5762)
Add `SemanticBuilder::with_excess_capacity` method to request that `SemanticBuilder` over-allocate space in `Semantic`'s `Vec`s.

Use this method to reserve 200% extra capacity for transformer to create more scopes, symbols and references.

200% is an unscientific guess of how much extra capacity is required. Obviously it depends on what transforms are enabled and content of the source code.
2024-09-14 15:02:16 +00:00
DonIsaac
f61e8b53dd refactor(linter): impl serde and schemars traits for LintPlugins (#5706) 2024-09-14 11:16:44 +00:00
Boshen
7caae5bca7 refactor(codegen): add GetSpan requirement to Gen trait (#5772) 2024-09-14 11:13:12 +00:00
Boshen
4abfa7682a
refactor(parser): add --ast and --comments to example 2024-09-14 09:02:23 +08:00
Dunqing
36e698b411 perf(transformer): call transform_jsx in exit_expression rather than enter_expression (#5751)
### Difference

In `enter_expression`: Recursive transform JSX
In `exit_expression`: Deep first transform

After the change,  `transform_jsx` still has a lot of room for improvement.
2024-09-13 17:58:41 +00:00
Dunqing
aac83168b3 perf(transformer/react): improve is_componentish_name's implementation (#5769) 2024-09-13 16:23:05 +00:00
Dunqing
3cc38dfc05 fix(transformer/react): react refresh panics when encounter use hook (#5768)
close: #5766

I even forgot that React has a `use` hook in the latest version
2024-09-13 16:17:33 +00:00
Boshen
e8bf30a125 feat(ast): add Comment::real_span (#5764) 2024-09-13 15:28:34 +00:00