Transform private property accesses in static prop initializers. e.g.:
Input:
```js
class C {
static #x = 123;
static y = this.#x;
}
```
Transformed:
```js
class C {}
var _x = { _: 123 };
babelHelpers.defineProperty(C, "y", babelHelpers.assertClassBrand(C, C, _x)._);
```
`this.#x` has been transformed to `babelHelpers.assertClassBrand(C, C, _x)._`.
- Remove `format!` macro
- Rename string related macros, `&'static`: `text!` and `'a`: `dynamic_text!`
- Apply `wrap!` macro instead of manually using `enter|leave_node`
- Introduce `/ir` directory and move `Doc`, `impl Display`, `DocBuilder`, etc.
I'm not yet determined how to, how deep to use macro, but this is first stepping stone... 🥌
Add a no-op placeholder method for transforming private fields as `AssignmentPattern`s. e.g. `[object.#prop] = []`.
Implementation will be added later.
Fix edge case in class static properties transform. When transforming `super()` in class constructor, stop searching when hit a `TSModuleBlock`. `TSModuleBlock` is essentially a function, so `super()` there is invalid.
implementing the rule [prefer set
has](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-set-has.md)
from unicorn.
I put the fix as dangerous fix because they was no test for the fix on
the unicorn part. I did add some test for this but i'm not sure that i'm
covering everything.
---------
Co-authored-by: Boshen <boshenc@gmail.com>
Co-authored-by: Cameron Clark <cameron.clark@hey.com>
Add class properties transform.
Implementation is incomplete. Notable missing parts:
* Scopes are not updated where property initializers move from class body into class constructor / `_super` function.
* Does not handle binding shadowing problems when property initializers move from class body into class constructor.
* `this` and references to class name in static property initializers need to be transformed to point to a temp var.
* Not all usages of private properties are supported (see below).
* Code which is moved to outside of class body is not transformed by other transforms for class declarations (works OK for class expressions). This includes static property initializers, static blocks, and computed property/method keys.
* Only basic checks for whether computed property/method keys may have side effects.
* Numerous other small issues noted in TODO comments through the code.
### Private properties
Currently does not handle the following usages of private properties:
```js
class Class {
#prop;
static #static;
method() {
object?.#prop;
object?.#prop();
[object.#prop] = [1];
({x: object.#prop} = {x: 1});
object.#prop`xyz`;
object?.#static;
object?.#static();
[object.#static] = [1];
({x: object.#static} = {x: 1});
object.#static`xyz`;
}
}
```
A test case for `new Object()` has been commented out:
This is due to the test configuration specifying `globals: { Object:
"off" }`.
This approach follows the example set by the no_new_wrappers rule.
[Reference
Code](bf839c1dfa/crates/oxc_linter/src/rules/eslint/no_new_wrappers.rs (L88))
---------
Co-authored-by: no-yan <63000297+no-yan@users.noreply.github.com>
Co-authored-by: Cameron Clark <cameron.clark@hey.com>
closes#7434
i couldn't test this. but what's happening is semaitc is being build
without scope child ids. When exhaustive_deps tries to access it its an
empty vec - hence the panic
When I was handling plugin-related diagnostics in `rolldown`, I
encountered this issue.
If there is a define key like `a.6.b`, the error message states that
<code>\`a.6.b\` is not an identifier</code>. However, it should actually
be that <code>\`6\` is not an identifier</code>.
`IdentifierReference::is_global_reference` and `MaybeBoundIdentifier::from_identifier_reference` both look up the symbol for the identifier. Do this lookup only once, rather than twice.
Remove uesless const declaration, since they need to access flags, so
they should never be evaluated in compiling time.
Or does this keyword have some other functions I missed?
Fixed a bunch of semantic errors by removing moving binding logic 😢. This plugin constructs a complex `for-await` replacement so that I wrongly move to an incorrect scope.
- fixes https://github.com/oxc-project/oxc/issues/7365
currently, we are matching globs on the absolute path to the file, which means that in order to properly match, all globs would need to start with `**/`. this is not consistent with the behavior in ESLint, which is defined here: https://eslint.org/docs/v8.x/use/configure/configuration-files#how-do-overrides-work
> **The patterns are applied against the file path relative to the directory of the config file.** For example, if your config file has the path `/Users/john/workspace/any-project/.eslintrc.js` and the file you want to lint has the path `/Users/john/workspace/any-project/lib/util.js`, then the pattern provided in `.eslintrc.js` is executed against the relative path `lib/util.js`.
This PR adds the ability to store the path to the configuration file along with the configuration data, so that we can later use it to resolve the relative paths of the files in the overrides section.
I'm not exactly sure if this will still make sense with nested configuration files, but I think it makes sense to store a path to each configuration file, alongside where the overrides are stored.
`IdentifierReference::is_global_reference` and `MaybeBoundIdentifier::from_identifier_reference` both look up the symbol for the identifier. Do this lookup only once, rather than twice.
close: #7338close: #7344
The `SymbolFlags::Export` is Initially used to solve `ExportSpecifier` that is not `IdentifierReference` that causes we cannot determine whether a Binding is not used everywhere by `Semantic`.
Since #3820 this problem is solved, so we don't need `SymbolFlags::Export` no longer. Also, removing this can help us easier to pass the `Semantic` check in `Transformer`
related: #4655
This PR implements a rule to detect Promises inside error-first
callbacks, preventing the mixed usage of callbacks and Promises.
Example of problematic code:
```javascript
a(function(err) { doThing().then(a) });
^^^^^^^^^^^^^^
```
[Original
implementation](266ddbb030/rules/no-promise-in-callback.js)
follow up to https://github.com/oxc-project/oxc/issues/6896
for improved compatibility with ESLint, this tries to match the behavior of plugin overrides so that plugins can be enabled for certain paths. this does not allow disabling plugins.
Re-order enum variants of `AssignmentOperator`, `BinaryOperator` and `UnaryOperator`.
* `Exponential` moved to after `Remainder` (so with the rest of the arithmetic operators).
* `Shift*` operators follow arithmetic operators.
* `AssignmentOperator::Bitwise*` ops moved to before `Logical*` ops (so all ops which correspond to `BinaryOperator`s are together).
* `*Or` always before `*And`.
* Plus/Addition always before Minus/Subtraction.
The purpose is to make the various methods on these types maximally efficient:
1. Group together variants so that `AssignmentOperator::is_*` methods can be executed with the minimum number of operations (essentially `variant - min <= max`).
2. Align the variants of `AssignmentOperator` and `BinaryOperator` so that conversion methods added in #7350 become very cheap too (essentially `if variant - min <= max { Some(variant + offset) } else { None }`).
`impl GetAddress for Function` was added as a hack, in the absence of another way to get the `Address` of a `&Function` (https://github.com/oxc-project/backlog/issues/140).
Remove it, and use `Address:from_ptr` instead in JSX Refresh transform, which is only place using it.
Use `AstBuilder::vec_from_array` introduced in #7331 in the transformer, in place of creating a `Vec` with `Vec::with_capacity` and then pushing values to it.
Because we lack specialization in stable Rust, `Vec::from_iter_in` is unable to take advantage of the fact that `[T; N]` has a statically knowable size.
Introduce `Vec::from_array_in` for this case, which should be able to create the `Vec` with a single static-sized memcpy, or may allow the compiler to see that it can construct the array directly in the arena, rather than construct on stack and then copy to the arena.
Also add a corresponding `AstBuilder::vec_from_array` method, and use it in various places in codebase.
This function receives an owned `oxc_allocator::Vec`. No need to copy the contents to a new `Vec` with `AstBuilder::vec_from_iter`, can just use the original.
Move the cheap "does arguments need to be transformed" checks introduced in #7321 into `enter_identifier_reference` and `enter_binding_identifier`, and mark those methods `#[inline]`. These hot paths can then usually execute without a function call.
This wins back the other half of the perf hit of #7234.
Track whether `arguments` needs to be transformed. If it doesn't, can skip expensive checks for whether `IdentifierReference`s and `BindingIdentifier`s names are `arguments` or not.
This recovers about half the performance hit of #7234.
Key `super` getter/setter hash map with original property name `&str`, instead of generated binding name. This allows a couple of improvements:
* Generate binding names for `super` getters/setters lazily, only when they're created, rather than every time you encounter a `super`.
* Don't allocate strings into arena which are never used as part of AST. Use `CompactString` for building those strings instead.
Optimize this function in various ways:
* Return a static `Atom` (rather than allocating into arena) if no `property`.
* Reserve capacity for `ArenaString` at start.
* Make path for unicode identifiers (very rare case) `#[cold]` and `#[inline(never)]`.
* Slightly faster uppercase conversion.
Fix due to this plugin transforming the async method and async arrow function, it caused arguments no longer point the original function.
For example:
Before
```js
class Cls {
async method() {
() => {
console.log(arguments)
}
}
}
```
After:
```js
class Cls {
method() {
var _arguments = arguments;
return babelHelpers.asyncToGenerator(function* () {
() => {
console.log(_arguments);
};
})();
}
}
```
In this way, the `_arguments` is its original function's arguments
### For performance regression
It seems we need to check the IdentifierReference and BindingIdentifier if it's an `arguments`, that causes a significant regression, we may need a cheap way to do checking
- fixes https://github.com/oxc-project/oxc/issues/6896
When the `plugins` config property is specified, it will overwrite the default plugins array. This allows the plugins list to be easily customized and allows for disabling default plugins at the same time as enabling non-default ones.
- example: `{ "plugins": [] }` will enable no plugins.
- example: `{ }` will enable default plugins.
- example: `{ "plugins": ["typescript", "import"] }` will enable only the import and typescript plugins.
`TransformCtx::helper_call` and `TransformCtx::helper_call_expr` take a `Span`. Sometimes the helper call replaces some original code, and should have the same span as the original code.
It's a common pattern in transformer to call `ctx.create_ident_reference()` and then convert to an `Expression` with `Expression::Identifier(ctx.ast.alloc(ident))`.
Add methods to do this in a single method call.
Similar to #7289. Check if `span.end` is 0 before doing `span.end - 1`, to prevent arithmetic overflow.
Also changed all checks to `span.end > 0`, just for consistency.
`clone_expression` was unnecessarily looking up the `SymbolId` of references multiple times. Use `BoundIdentifier` instead to avoid that.
Notes:
* `create_conditional_expression` has to take all parts as `Expression`s just to support `this ?? something`.
* `TraverseCtx::is_static` also handles `Super`, but can skip that in this case as `super ?? something` is not valid syntax.
part of #4655
### Summary
Logic has been added to handle bind functions, unifying the
implementations of `unicorn/no-useless-promise-resolve-reject` and
`promise/no-return-wrap`.
This enables detection of the following code:
```javascript
foo().then((function() { return Promise.resolve(4) }).bind(this))
```
Merging this PR will allow this rule to pass all test cases of
promise/no-return-wrap without options. Additionally, merging #7274 will
ensure that all test cases are passed.
Remove `TraverseCtx::clone_identifier_reference`.
This API encourages unnecessary repeated lookups of the `SymbolId` for a reference. It is preferable to use `MaybeBoundIdentifier` which only looks up the `SymbolId` once.
`MaybeBoundIdentifier` is similar to `BoundIdentifier`, but can be used where the identifier may or may not have have a `SymbolId` (may or may not be bound).
Typical usage:
```rs
// Create `MaybeBoundIdentifier` from an existing `IdentifierReference`
let binding = MaybeBoundIdentifier::from_identifier_reference(ident, ctx);
// Generate `IdentifierReference`s and insert them into AST
assign_expr.left = binding.create_write_target(ctx);
assign_expr.right = binding.create_read_expression(ctx);
```
Pure refactor. `StringLiteral` definition was sandwiched in the middle of RegExp-related code. Move it higher up in `literal.rs`.
All the rest of the diff is just re-ordering generated code.
I had intended to leave this in until we could rework some of the rule/plugin name resolution code, however this is causing issues with merging https://github.com/oxc-project/oxc/pull/6974. The mutability of `self` is difficult to resolve in that PR without more drastic changes. Since this isn't currently helping us out, I am simply removing the code for now until we can revisit this.
Variants of `TSEnumMemberName` were previously only prefixed with `Static` to avoid naming conflicts with the variants inherited from `Expression` for non-static member names. #7219 removed the variants inherited from `Expression`, so all variants are now static. So we can shorten the names again (back to what they were before `inherit_variants!` was introduced).
#7219 removed all variants of `TSEnumMemberName` except `IdentifierName` and `StringLiteral`. It no longer inherits variants from `Expression`, so we can remove the `inherit_variants!` macro wrapper.
The discriminants no longer need to avoid clashes with `Expression`'s, so they can start at 0.
Firstly, a massive thanks to @alisnic for starting this (incredibly complicated) lint rule in https://github.com/oxc-project/oxc/pull/2637 !
still a draft. current state:
3x false positives (all todo with refs)
3x false negatives (TS will catch these
13x false negatvies todo with refs
1x false negative TODO
closes #2637
relates to #2174
For the test case, Closure Compiler doesn't handle this at all in the REPL! If it's necessary, I will turn it back.
This PR uses builtin `and_then` and `map` method, which is better instead of a lot of `if let Some`.
close#5530
This PR adds support for handling recursive JSX components, allowing
detect functions like the one below:
```javascript
function ListItem({ depth }) {
return <ListItem depth={depth} />
}
```
Some logic is duplicated with the non-JSX case. But the refactor will be
complicated and affects over-all, so we can make it in another PR.
cc: @DonIsaac @camc314
---------
Co-authored-by: Cam McHenry <camchenry@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
for convenience, I've added a new function called `ancestor_kinds` which loops overall `ancestors` and gets their `AstKind`. this is a common pattern in a couple of places. I also did some somewhat related refactoring to remove places where we were manually calling `AstNode::kind` instead of using `ancestor_kinds` or calling `parent_kind`.
for consistency with the `ancestor_ids` function, this changes it to use the same terminology but make it clear it actually returns the node, not just the ID.
this is pure refactor, no functional changes here.
After transformation, super expressions have moved to unexpected places. This PR replaces super expression to call expression, and then inserts the super methods to the top of the method body.
For example:
Before:
```js
class G {
async method() {
super.foo()
}
}
```
After:
```js
class G {
method() {
var _superprop_getFoo = () => super.foo,
_this = this;
return _asyncToGenerator(function* () {
_superprop_getFoo().call(_this);
})();
}
}```
Support for inferring function name from ObjectPropertyValue's key
For example:
```js
({ foo: async function() {} })
```
After this, we will able to infer `foo` for the object method