Do not remove empty function calls if the function is async or generator or args can cause type error.
From test262:
```js
function fn({}) {}
assert.throws(TypeError, function() {
fn(null);
});
```
#12193 made it so semantic analysis is run on all files (inc. `.d.ts`), so now `AstNodes::program` now always returns `Some`.
We always unwrap the value returned by `AstNodes::program`, so move the `unwrap` into the method itself.
* close https://github.com/oxc-project/oxc/issues/12460
Injecting top-level statements after the last import statement is unsound because once there is an import statement in the last line, it would be broken if other code relies on the injected statements. Learn more to see the following example.
I suppose the reason we did this is that we had a test case that misled us. I overrode that test with the expected output.
For example:
Input:
```js
import { observer } from 'mobx-react-lite'
import { useFoo } from './useFoo'
export const BazComponent = observer(function BazComponent_() {
const foo = useFoo()
return (
<>
{foo}
{bar}
</>
)
})
import { bar } from './bar'
```
Before output:
```js
import { observer } from "mobx-react-lite";
import { useFoo } from "./useFoo";
export const BazComponent = _s(observer(_c = _s(function BazComponent_() {
_s();
const foo = useFoo();
return /* @__PURE__ */ _jsxs(_Fragment, { children: [foo, bar] });
}, "useFoo{foo}", false, function() {
return [useFoo];
})), "useFoo{foo}", false, function() {
return [useFoo];
});
_c2 = BazComponent;
import { bar } from "./bar";
import { Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
var _s = $RefreshSig$();
var _c, _c2;
$RefreshReg$(_c, "BazComponent$observer");
$RefreshReg$(_c2, "BazComponent");
```
After output:
```js
import { observer } from "mobx-react-lite";
import { useFoo } from "./useFoo";
import { Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
var _s = $RefreshSig$();
export const BazComponent = _s(observer(_c = _s(function BazComponent_() {
_s();
const foo = useFoo();
return /* @__PURE__ */ _jsxs(_Fragment, { children: [foo, bar] });
}, "useFoo{foo}", false, function() {
return [useFoo];
})), "useFoo{foo}", false, function() {
return [useFoo];
});
_c2 = BazComponent;
import { bar } from "./bar";
var _c, _c2;
$RefreshReg$(_c, "BazComponent$observer");
$RefreshReg$(_c2, "BazComponent");
```
The difference is that `import { Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; var _s = $RefreshSig$();` has injected after `import { useFoo } from "./useFoo";`
* close https://github.com/rolldown/rolldown/issues/5374
Example:
Input:
```ts
function myPropertyDecorator() {
return (target: any, propertyKey: number) => {
console.log(target.constructor.modelName, propertyKey);
};
}
class Test {
public static modelName = 'Test';
@myPropertyDecorator()
public myProperty = 1;
}
const instance = new Test();
export default instance;
```
Output diff:
```diff
function myPropertyDecorator() {
return (target, propertyKey) => {
console.log(target.constructor.modelName, propertyKey);
};
}
class Test {
constructor() {
babelHelpers.defineProperty(this, "myProperty", 1);
}
}
-babelHelpers.decorate([myPropertyDecorator()], Test.prototype, "myProperty", void 0);
-babelHelpers.defineProperty(Test, "modelName", "Test");
+babelHelpers.defineProperty(Test, "modelName", "Test");
+babelHelpers.decorate([myPropertyDecorator()], Test.prototype, "myProperty", void 0);
const instance = new Test();
export default instance;
```
The cause is that we transform class fields and decorators in two different plugins, and the decorator plugin should be transformed first, and then run the class-properties plugin, because class-properties might erase some decorator information. However, this cause transformed decorators injected before transformed class fields.
This PR fixes this problem by collecting all transformed decorators that wait for injection, and injecting them in batch after the `class-properties` plugin has run.
* close https://github.com/rolldown/rolldown/issues/5326
Regression introduced by #12212. The transform execution shift causes the bug because the `class-properties` plugin always assumes that all fields accessed can be transformed. However, the prior change breaks the assumption.
This PR reverts some changes to remove the ts-only field early, which respects that `class-properties` assumption
Part of #11582
The filter logic added in the previous PR was not complete, so I fixed it.
Also added more non-testable case.
~~For `parser_typescript.snap`, 2 "Expect Syntax Error" entries are newly reported, but they will be handled in the future task.~~
Input:
```ts
class DeclareFields {
@dec declare name: string;
@dec declare output: number;
}
```
Before output:
```ts
class DeclareFields {}
```
After output:
```ts
class DeclareFields {}
babelHelpers.decorate([dec], DeclareFields.prototype, "name", void 0);
babelHelpers.decorate([dec], DeclareFields.prototype, "output", void 0);
```
The `declare` fields with decorators should be transformed, but they
would be removed in the typescript plugin before legacy decorators
transform them, so this PR is to adjust the transform order and allow
`declare` fields can be transformed
Follow on to #12087
> We might be able to improve perf of AstNodes::ancestor_ids a little by
using a custom iterator, instead of std::iter::successors.
> Also, node_id == NodeId::DUMMY would be a cheaper check than parent_id
== node_id. The NodeId of Program in practice is always NodeId::DUMMY
(0), but we don't have a static guarantee of that at present. But we
could get that guarantee by adding assert!(self.parent_ids.is_empty())
to the top of AstNodes::add_program_node (or maybe just debug_assert!).
> @overlookmotel in
https://github.com/oxc-project/oxc/pull/12087#issuecomment-3041419201
My changes:
- added constant NodeId::ROOT (same value as NodeId::DUMMY, but leads to
more intuitive code)
- added asserts in `add_program_node` that nodes are empty and node kind
that's being added is Program
- ancestor_kinds and ancestors now build upon ancestor_ids, making
std::iter::successors implementation obsolete
- make AstNodeParentIter simpler by iterating over node ids and by
making use of previous assertions of `add_program_node`
- remove root, root_node and root_node_mut functions and replace their
use with either new program function or NodeId::ROOT
Results in general much less code, especially less boilerplate code in
the linter around getting the program node. Also might hopefully yield
some small benchmark improvements.
Edit:
Only very minor if not zero improvements on the linter benchmarks :(
I also noticed while working on this that the ancestor iterator methods
where documented as "The first node produced by this iterator is the
first parent of the node pointed to by node_id". That statement was
incorrect because all the iterators didn't yield the parent of node_id,
but node_id itself as first item. I updated the documentation of these
methods to correctly reflect their implementation.
Control printing of comments more precisely via more options:
The logic change here is that jsdoc is no longer part of annotation
comments.
```rust
pub struct CommentOptions {
/// Print normal comments that do not have special meanings.
///
/// At present only statement level comments are printed.
///
/// Default is `true`.
pub normal: bool,
/// Print jsdoc comments.
///
/// * jsdoc: `/** jsdoc */`
///
/// Default is `true`.
pub jsdoc: bool,
/// Print annotation comments.
///
/// * pure: `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`
/// * webpack: `/* webpackChunkName */`
/// * vite: `/* @vite-ignore */`
/// * coverage: `v8 ignore`, `c8 ignore`, `node:coverage`, `istanbul ignore`
///
/// Default is `true`.
pub annotation: bool,
/// Print legal comments.
///
/// * starts with `//!` or `/*!`.
/// * contains `/* @license */` or `/* @preserve */`
///
/// Default is [LegalComment::Inline].
pub legal: LegalComment,
}
```
closes#12030
related https://github.com/rolldown/tsdown/issues/357
Syntactically, this is valid. It is only an error when you get to type checking. The error code itself can be handled as part of https://github.com/oxc-project/oxc/issues/11582, but this fixes an issue where we emitted an error for a case that should definitely be valid, as pointed out in the file itself.
This PR is a follow-up to #11990. After `AstKind::TSTypeName` has been removed, a lot of prior handling of the kind can be removed. The semantic snapshot changes are expected, caused by #12042.
Related to #10307.
Re-arrange fields in ESTree AST, so `start` and `end` are last, instead of first.
Also bump `acorn-test262` submodule to include https://github.com/oxc-project/acorn-test262/pull/54, which rearranges the fields in test snapshots.
Part of #10307. Add `range` option to `oxc-parser`, to add `range` property to nodes in ESTree AST.
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
Remove the custom ESTree serializer for `MethodDefinition`'s `key` field. This serializer was required to replicate a bug in TS-ESLint, which they've now fixed. This fixes the test case which was broken by #11762.
Bump `acorn-test262` submodule, which bumps `@typescript-eslint/parser`. Latest version of TS-ESLint contains fixes for bugs which I reported. Currently this breaks 1 conformance test - will fix in following PR.