Commit graph

7193 commits

Author SHA1 Message Date
camc314
38ba2f640c chore(linter): remove some unused commented code (#7800) 2024-12-11 08:55:21 +00:00
Nicholas Rayburn
06e6d387fa
fix(linter): Fix unicorn/prefer-query-selector to use the correct replacement for getElementsByClassName (#7796)
Note: This uses a regex to replace multiple instances of whitespace with
` .`. May not be the most performant, so if there's a simple alternative
I can change to that instead.

cc @camc314, I know this was assigned to you but I just wanted to throw
something quick together while I had a minute. Feel free to use this, or
decline it and write your own.

Fixes #7794.

---------

Co-authored-by: Cameron Clark <cameron.clark@hey.com>
2024-12-11 16:49:40 +08:00
overlookmotel
bb22c67974 fix(transformer/class-properties): fix ScopeIds in static prop initializers (#7791)
Code in static property initializers moves from inside the class to outside. Update parent `ScopeId`s for first level scopes in initializers.
2024-12-11 03:18:29 +00:00
Dunqing
4a524ed150 chore(transformer): support passing babel options path in example (#7790)
Sometimes we want to use Babel test's options to test some code, If the example can get the TransformerOptions from Babel options, then we can easily do it
2024-12-11 00:33:22 +00:00
overlookmotel
caa57f1649 fix(transformer/class-properties): fix scope flags in static prop initializers (#7786)
Code in static property initializers moves from inside the class to outside. If environment outside the class is not strict mode, then scopes within the initializer become sloppy mode. Update `ScopeFlags` for scopes in static prop initializers accordingly.

We're following Babel for now, but this isn't actually correct. The initializers should be wrapped in a strict mode IIFE to maintain their strict mode behavior. But at least semantic data is now correct for the output.
2024-12-10 23:57:43 +00:00
overlookmotel
4a3bca8354 fix(semantic): fix identifying strict mode arrow functions (#7785)
Semantic analysis was not marking an arrow function containing a `"use strict"` directive as `ScopeFlags::StrictMode`. Fix that.
2024-12-10 23:57:42 +00:00
overlookmotel
f7900ab246 feat(ast): add ArrowFunctionExpression::has_use_strict_directive method (#7784)
Add `ArrowFunctionExpression::has_use_strict_directive` method to go with `Function::has_use_strict_directive` etc.
2024-12-10 23:57:41 +00:00
overlookmotel
96a26d3b29 refactor(ast)!: rename is_strict methods to has_use_strict_directive (#7783)
The name `is_strict` was misleading for these methods, because it doesn't tell you if the e.g. function *is* strict mode, only whether it contains a `"use strict"` directive. `Function::is_strict` might return `false` for a function which does have strict mode semantics because it's e.g. in an ESM file.

Rename these methods to `has_use_strict_directive` to better reflect what they do.

For `Program`, change the method to only check for `"use strict"` directive and not to look at `source_type`. `Semantic` should be the source of truth on strict/sloppy mode of AST nodes. It's cheaper to look up the `ScopeFlags` than to iterate over `directives`, so don't encourage this anti-pattern by providing a "rival" method.
2024-12-10 23:57:40 +00:00
oxc-bot
bde753b4ee
release(oxlint): v0.15.0 (#7782)
## [0.15.0] - 2024-12-10

- 39b9c5d linter: [**BREAKING**] Remove unmaintained security plugin
(#7773) (Boshen)

### Features

- 065f7dc linter: Support `expectTypeOf`, `assert` and `assertType` in
`vitest/expect-expect` (#7742) (Yuichiro Yamashita)
- 3d5f0a1 linter/no_restricted_imports: Add the no_restricted_imports
rules (#7629) (Guillaume Piedigrossi)

### Bug Fixes

- ad27b20 linter: Only resolve esm files for import plugin (#7720)
(Boshen)
- 5e6053f linter: False positive in `eslint/yoda` (#7719) (dalaoshu)

### Refactor

- c6a19aa linter: Remove unused `serde` features (#7738) (Boshen)
- b9a2b35 linter: Remove `aho-corasick` (#7718) (Boshen)

### Testing

- 62f0a22 linter: Port `react-jsx-uses-vars` rules to no_unused_vars
(#7731) (Tyler Earls)
- 02f9903 linter: Add regression tests for `import/namespace` (#7723)
(dalaoshu)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-12-11 00:58:03 +08:00
oxc-bot
bd572c609b
release(crates): v0.40.1 (#7781)
## [0.40.1] - 2024-12-10

### Bug Fixes

- 18d0ce3 napi: Rename `Error` to `OxcError` to avoid name collision
(#7780) (Boshen)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-12-11 00:35:34 +08:00
Boshen
18d0ce3c06
fix(napi): rename Error to OxcError to avoid name collision (#7780) 2024-12-11 00:30:07 +08:00
overlookmotel
4f1ab49440 test(transformer/class-properties): add output.js files to override fixtures (#7777)
In two of the overridden text fixtures for class properties transform, there was no `output.js` file because what was overridden was just `options.json` and `update_fixtures.js` script then generated new output files using Babel with the new options.

That was fine, but doesn't work with #7771. So add `output.js` files to the these overrides too.
2024-12-10 15:25:39 +00:00
oxc-bot
6768f7ded2
release(crates): v0.40.0 (#7775) 2024-12-10 23:04:43 +08:00
overlookmotel
3629a91c4c
docs(napi): fix typo (#7776) 2024-12-10 15:01:26 +00:00
Boshen
39b9c5d01b feat(linter)!: remove unmaintained security plugin (#7773) 2024-12-10 14:29:22 +00:00
Boshen
9157a0ed0f feat(napi/parser): add getUtf16ByteOffset API; update README (#7772) 2024-12-10 14:15:39 +00:00
overlookmotel
7dcf6b4d7c feat(ast, transformer): add AstBuilder::use_strict_directive method (#7770)
Add `AstBuilder::use_strict_directive` method, and use it in transformer.
2024-12-10 13:17:27 +00:00
overlookmotel
98afe6543f refactor(ast): AstBuilder extra methods use SPAN (#7769)
Pure refactor. `SPAN` is shorter than `Span::default()` and is what we use elsewhere.
2024-12-10 13:11:19 +00:00
Boshen
f3cfa7f3dd feat(napi/parser): add get_line_column_number API (#7765) 2024-12-10 12:49:11 +00:00
overlookmotel
016ae92453 refactor(transformer/class-properties): rename file (#7767)
Cosmetic change. Rename file that deals with static property initializers to have a more descriptive name.
2024-12-10 11:16:09 +00:00
overlookmotel
e97a9549be style(data_structures): line breaks (#7766)
Style nit. Add line breaks.
2024-12-10 11:00:12 +00:00
Dunqing
19a8aa5238 test(transformer/class-properties): overrides optional-chain-related tests (#7749)
These tests only variable names difference from Babel's output, which is caused by transforming order.
2024-12-10 10:35:06 +00:00
Dunqing
9cacf64f1d refactor(transformer/class-properties): transform the remaining PrivateFieldExpression in ChainExpression first (#7763)
We need to transform the inner PrivateFieldExpression so that the variable name matches Babel's output as closely as possible.
2024-12-10 10:35:06 +00:00
Dunqing
f42dbdf56b fix(transformer/class-properties): output is not the same with Babel when PrivateFieldExpression is optional (#7762)
Part of https://github.com/oxc-project/oxc/pull/7749
2024-12-10 10:35:05 +00:00
Dunqing
f52b1db821 fix(transformer/class-properties): output is not the same with Babel when callee has optional (#7748)
Part of https://github.com/oxc-project/oxc/pull/7749. It is just an output mismatch problem, not affecting runtime behavior.
2024-12-10 10:35:04 +00:00
Boshen
cf2ee06825 feat(data_structures): add rope (#7764) 2024-12-10 10:30:04 +00:00
Guillaume Piedigrossi
3d5f0a1a0c
feat(linter/no_restricted_imports): add the no_restricted_imports rules (#7629)
add first test cases related to the 'paths' config

Note that the test cases and configuration format is not the same as the
original ESLint rule.
What is the oxc team strategy to develop such a rule? Is it ok to adapt
the config format ?

---

I started a discussion here :
https://github.com/oxc-project/oxc/discussions/7534#discussion-7574282
I copy/paste the content here. Maybe it is more relevant?

I am working to implement [this no-restricted-imports
rule](https://eslint.org/docs/latest/rules/no-restricted-imports).
I have several problems:

How to handle multiple format configuration in rust?
The eslint config can be: "fs", ["fs"], or {paths: [{name: "fs"}]}. But
Rust needs only one type. I don't know how to do this in rust.

Is it ok to cover only the {paths: [{name: "fs"}]} case ?

How to parse this config with the from_configuration method?
Here is what I have done:

```
   fn from_configuration(value: serde_json::Value) -> Self {
        let mut paths = Vec::new();
        let mut patterns = Vec::new();

        if let Some(obj) = value.as_object() {
            // Handle paths array
            if let Some(paths_value) = obj.get("paths") {
                if let Some(paths_array) = paths_value.as_array() {
                    for path_value in paths_array {
                        if let Ok(path) = serde_json::from_value(path_value.clone()) {
                            paths.push(path);
                        }
                    }
                }
            }

            // Handle patterns array
            if let Some(patterns_value) = obj.get("patterns") {
                if let Some(patterns_array) = patterns_value.as_array() {
                    for pattern_value in patterns_array {
                        if let Ok(pattern) = serde_json::from_value(pattern_value.clone()) {
                            patterns.push(pattern);
                        }
                    }
                }
            }
        }

        Self { paths, patterns }
    }
````
But here is my result:
```
[RestrictedPath { name: "foo", import_names: None, message: None }]

-------- rule config --------
{
  "paths": [
    {
      "name": "foo",
      "importNames": [
        "AllowedObject"
      ]
    }
  ]
}
```
Note the "None" values
2024-12-10 17:00:15 +08:00
Boshen
a222f2b055 fix(codegen): print delete 2e308 as delete (0, Infinity) (#7761)
closes #7736
2024-12-10 08:37:50 +00:00
Boshen
4bd3d103d3 feat(napi/parser): introduce experimental MagicString (#7529)
Hold magic string instance on the Rust side for utf8 string manipulation.
2024-12-10 08:33:47 +00:00
overlookmotel
2e69720ba0 feat(transformer/class-properties): support private_fields_as_properties assumption (#7717)
Support `private_fields_as_properties` assumption in class properties transform. This assumption is also enabled by the transform's `loose` option.

Optional chain (e.g. `this?.#prop`) is not yet implemented, but all other usages of private fields are supported. We'll handle optional chain in a follow-on PR.
2024-12-10 02:28:31 +00:00
overlookmotel
5d6fa259f4 refactor(traverse)!: remove TraverseCtx::is_static (#7760)
`TraverseCtx::is_static` was only used in logical assignment operators transform, and #7745 removed that usage. So remove this method. `TransformCtx::duplicate_expression` fulfills the same role.
2024-12-10 02:28:30 +00:00
overlookmotel
e010b6a7a0 feat(transformer/logical-assignment-operators): no temp vars for literals (#7759)
`TransformCtx::duplicate_expression` (introduced in #7754) don't create temp vars for literals. This produces more compact output for the logical assignment operators transform.

This diverges from Babel (it's better!) so add an override for one of Babel's fixtures. Also add further tests for all literal types.
2024-12-10 02:28:28 +00:00
overlookmotel
8f8b4c31ad test(transformer): enable fixture overrides for logical-assignment-operators transform (#7758)
Enable overriding Babel's fixtures for logical assignment operators transform.
2024-12-10 02:28:27 +00:00
overlookmotel
e48769a45d fix(transformer/logic-assignment-operator): always create IdentifierReferences with ReferenceId (#7745)
Use `TransformCtx::duplicate_expression` (introduced in #7754) to decide when to create temp vars for member expression object and computed property.

This fixes a bug where `IdentifierReference`s created when transforming `key` in `object[key] &&= value` were created without a `ReferenceId` (due to `clone_in`).

We didn't catch this before because Babel's test fixtures only cover `object[key++] &&= value` not the simpler `object[key] &&= value`. Add tests for this.
2024-12-10 02:28:26 +00:00
overlookmotel
9c2a1b61b9 refactor(transformer): duplicate_expression do not produce temp var for super (#7757)
`TransformCtx::duplicate_expression` method introduced in #7754 don't create a temp var for `super`. This prepares it for use in logical assignment operator transform (#7745).
2024-12-10 02:28:25 +00:00
overlookmotel
a750ebc324 refactor(transformer): duplicate_expression take mutated_symbol_needs_temp_var param (#7756)
`TransformCtx::duplicate_expression` method introduced in #7754 take a param `mutated_symbol_needs_temp_var`.

If `true`, `duplicate_expression` will create a temp var for an `IdentifierReference` whose's symbol is assigned to elsewhere in the AST. If `false`, it won't.

This is required because different transforms which use `duplicate_expression` have different requirements.
2024-12-10 02:28:24 +00:00
overlookmotel
78dff7d232 feat(semantic): add SymbolTable::symbol_is_mutated method (#7755)
Add `SymbolTable::symbol_is_mutated` method. Returns `true` if the symbol is not a `const` and has any references with `ReferenceFlags::Write`.
2024-12-10 02:28:23 +00:00
overlookmotel
b500f5527a refactor(transformer): introduce TransformCtx::duplicate_expression (#7754)
Move implementation of `duplicate_object` from the class properties transform into `TransformCtx::duplicate_expression`, so it can also be used in other transforms.
2024-12-10 02:28:21 +00:00
overlookmotel
3c1b2bf439 refactor(isolated_declarations): use NONE in AST builder calls (#7752)
Pure refactor. `NONE` is shorter than `None::<TSTypeAnnotation<'a>>`.
2024-12-09 16:36:27 +00:00
overlookmotel
75ba4a9e7a refactor(transformer): use NONE in AST builder calls (#7751)
Pure refactor. `NONE` is shorter than `None::<TSThisParameter<'a>>`.
2024-12-09 16:30:26 +00:00
overlookmotel
7e0f7eb67b refactor(transformer/class-properties): prefer contains to intersects for bitflags (#7747)
Tiny refactor. `contains` is clearer than `intersects`, and they produce equivalent assembly when argument is a single flag value, and statically knowable.

https://godbolt.org/z/bTdfbv3f8
2024-12-09 16:09:24 +00:00
overlookmotel
463fc5f2de perf(transformer/logic-assignment-operator): inline enter_expression visitor (#7744)
Optimization. Inline `enter_expression` visitor, and reduce the size of the function, to keep the path for "nothing to do here, bail out" as fast and compact as possible.

From CodSpeed results, it looks like `enter_expression` was already being inlined as perf gain is not the usual +5% we see for inlining `enter_expression` when it wasn't already. But reducing the size of the visitor function still nets a +1% perf gain.
2024-12-09 15:33:23 +00:00
overlookmotel
97acd88793 test(transformer/class-properties): add more tests (#7743)
Babel's tests only cover transforming `this.#prop &&= value` with logical assignment operators transform also enabled. Add tests for just class properties transform alone.
2024-12-09 14:17:12 +00:00
Boshen
2803aec521 feat(napi/transform): return helpers information (#7737)
closes #7599
2024-12-09 14:07:40 +00:00
Yuichiro Yamashita
065f7dcb9d
feat(linter): support expectTypeOf, assert and assertType in vitest/expect-expect (#7742)
[Vitest](https://vitest.dev/api/expect.html) has 4 assertions.
[expect](https://vitest.dev/api/expect),
[expectTypeOf](https://vitest.dev/api/expect-typeof),
[assert](https://vitest.dev/api/assert) and
[assertType](https://vitest.dev/api/assert-type).

But now only `expect`. Therefore this PR supports there rest of
assertions.
And jest doesn't have such assertions, so I added branching based on the
test framework.
2024-12-09 22:07:25 +08:00
overlookmotel
5806942dda docs(transformer/class-properties): correct doc comment (#7741)
Fix incorrect doc comment.
2024-12-09 12:50:05 +00:00
Boshen
1a67cde0de
build(linter): fix feature unification (#7740) 2024-12-09 19:21:22 +08:00
Boshen
c254a5b12c test(napi/transform): use the expect API instead of assert (#7739) 2024-12-09 09:28:01 +00:00
Boshen
c6a19aa478 refactor(linter): remove unused serde features (#7738) 2024-12-09 09:16:32 +00:00
Boshen
b701232098 fix(codegen): print quote correctly for directive (#7735)
closes #7734
2024-12-09 07:17:02 +00:00