Commit Graph

811 Commits

Author SHA1 Message Date
oxc-bot a257d9732c release(crates): v0.75.1 (#12057) 2025-07-03 21:51:31 +08:00
overlookmotel a3641d269c fix(napi/parser): remove non-existent methods from TS type defs (#12054)
Fix #11950.

Remove `parseSyncRaw`, `parseAsyncRaw`, and `getBufferOffset` from TS type defs. They're not actually exported from the package.
2025-07-03 13:13:53 +00:00
overlookmotel 016634f810 refactor(ast/estree): introduce serialize_span method (#12013)
In ESTree serializer, reduce repeated code by introducing a `serialize_span` method which outputs `start` and `end`, and also `range` if the `range` option is enabled.
2025-07-01 18:28:10 +00:00
overlookmotel 754f01d79a refactor(ast/estree): move start and end fields to last (#12012)
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.
2025-07-01 17:32:06 +00:00
overlookmotel 4a408c381c docs(napi/parser): document options (#12008)
Add documentation for `oxc-parser`'s options to README. Includes the new `range` option.
2025-07-01 15:53:23 +00:00
overlookmotel 45973119a0 refactor(ast/estree): remove temp vars for ranges from serializer (#12007)
Pure refactor. Simplify serializers by removing `ranges` temp vars.
2025-07-01 15:07:37 +00:00
Cameron f1c18d8db2 chore(napi/parser): remove undefined module.exports (#11968)
`parseWithoutReturn` doesn't exist anywhere, so attempting to assign it
to modules.exports does nothing:

Logging the value of `import * as k from 'oxc-parser'`

```
node example.mjs test.ts
[Module: null prototype] {
  ExportExportNameKind: { Name: 'Name', Default: 'Default', None: 'None' },
  ExportImportNameKind: {
    Name: 'Name',
    All: 'All',
    AllButDefault: 'AllButDefault',
    None: 'None'
  },
  ExportLocalNameKind: { Name: 'Name', Default: 'Default', None: 'None' },
  ImportNameKind: {
    Name: 'Name',
    NamespaceObject: 'NamespaceObject',
    Default: 'Default'
  },
  ParseResult: [Function: ParseResult],
  Severity: { Error: 'Error', Warning: 'Warning', Advice: 'Advice' },
  default: {
    ParseResult: [Function: ParseResult],
    ExportExportNameKind: { Name: 'Name', Default: 'Default', None: 'None' },
    ExportImportNameKind: {
      Name: 'Name',
      All: 'All',
      AllButDefault: 'AllButDefault',
      None: 'None'
    },
    ExportLocalNameKind: { Name: 'Name', Default: 'Default', None: 'None' },
    ImportNameKind: {
      Name: 'Name',
      NamespaceObject: 'NamespaceObject',
      Default: 'Default'
    },
    parseWithoutReturn: undefined,
    Severity: { Error: 'Error', Warning: 'Warning', Advice: 'Advice' },
    parseSync: [Function: parseSync],
    parseAsync: [AsyncFunction: parseAsync],
    experimentalGetLazyVisitor: [Function: experimentalGetLazyVisitor],
    rawTransferSupported: [Function: rawTransferSupported]
  },
  experimentalGetLazyVisitor: [Function: experimentalGetLazyVisitor],
  parseAsync: [AsyncFunction: parseAsync],
  parseSync: [Function: parseSync],
  parseWithoutReturn: undefined,
  rawTransferSupported: [Function: rawTransferSupported]
}
```
2025-06-30 13:19:14 +08:00
oxc-bot d25dc35b19 release(crates): v0.75.0 (#11897) 2025-06-25 19:49:12 +08:00
overlookmotel cf0e18ae6d fix(napi/parser): NodeArray allow setting large integer properties (#11883)
Integers `>= 4294967295` are treated as string properties, not numerical array indexes. Make `NodeArray` behave the same as `Array` for such properties, and allow user to set them.
2025-06-24 15:35:07 +00:00
Bacary Bruno Bodian 9a2548ae9d feat(napi/parser)!: add range option (#11728)
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>
2025-06-24 02:23:27 +01:00
overlookmotel 84fa00629f perf(napi/parser): lazy deser: faster construction of NodeArray iterators (#11870)
Make `NodeArray` iterators (e.g. `NodeArrayValuesIterator`) constructors take a `Proxy<NodeArray>`.

This makes them safe to construct from user code. Therefore can remove the `TOKEN` check which previously blocked user code from constructing these classes. This makes iterating over a `NodeArray` very slightly faster.
2025-06-23 17:38:35 +00:00
overlookmotel fb02e6c9d0 perf(napi/parser): lazy deser: speed up creating NodeArrays (#11869)
`NodeArray` store length as an internal property, rather then creating an underlying `Array` with that length. This is much faster when the array is large.

Produces an 18x speed-up in getting `program.body` for `cal.com.tsx` benchmark fixture, because it has 3942 statements.
2025-06-23 17:21:22 +00:00
overlookmotel 58dfff826d perf(napi/parser): raw deser: remove WeakMap from NodeArray (#11868)
Improve the performance of `NodeArray` by removing the `WeakMap` which was used to get the inner `NodeArray` from a proxy. Use a private `Symbol` instead to perform the same function.
2025-06-23 16:13:31 +00:00
overlookmotel 54f9464ad9 test(napi/parser): add benchmarks for lazy visit alone (#11866)
Add benchmarks for lazy visitor in isolation (i.e. without also parsing).
2025-06-23 12:39:19 +00:00
overlookmotel 6c5ee78b51 perf(napi/parser): lazy visit: cheaper for loop (#11864)
Tiny optimization to lazy visitor. It's marginally cheaper in a loop to compare to 0, rather than to a constant. I also assume that `!== 0` is cheaper than `> 0` as it doesn't involve type coercion.
2025-06-23 12:07:09 +00:00
overlookmotel 97b671f393 test(napi/parser): load internal modules with require in benchmarks (#11865)
Improve raw transfer benchmarks. Use `require` to load internal modules instead of `import`. `oxc-parser` uses `require` internally, so using `import` here was resulting in a lot of code being loaded and instantiated twice.
2025-06-23 11:59:33 +00:00
overlookmotel 0bf7815040 refactor(napi/parser): lazy visitor: pre-calculate count of node types (#11861)
Pure refactor. Pre-calculate number of node types in codegen. This prepares the way for replacing the `Map` with a plain JS object.
2025-06-23 12:46:08 +01:00
oxc-bot 94a418d201 release(crates): v0.74.0 (#11853) 2025-06-23 13:27:08 +08:00
renovate[bot] 15d84be165 chore(deps): lock file maintenance npm packages (#11850) 2025-06-23 13:02:38 +08:00
overlookmotel 93069a5b6c feat(napi/parser): add experimental lazy visitor (#11837)
Add lazy visitor to `oxc-parser`.

This API is experimental and not ready for use yet. AST is not ESTree-compatible at present (same as lazy deserializer).

Usage:

```js
import { parseSync, experimentalGetLazyVisitor } from 'oxc-parser';

// Get `Visitor` class.
// `Visitor` is not exported directly from `oxc-parser` as all code
// related to lazy deserialization is lazy-loaded.
const Visitor = experimentalGetLazyVisitor();

// Define visitor.
// This visitor counts how many identifiers in AST.
let identCount = 0;
const visitor = new Visitor({
  BindingIdentifier(ident) {
    identCount++;
  },
  IdentifierReference(ident) {
    identCount++;
  },
  IdentifierName(ident) {
    identCount++;
  },
});

const code = 'let x, y; x = { z: 2 };';
const { visit, dispose } = parseSync('test.js', code, { experimentalLazy: true });

visit(visitor);

assert(identCount === 4);

// Dispose of AST now we're done with it.
// This returns buffer storing AST data to the pool for reuse.
dispose();
```
2025-06-20 18:23:56 +00:00
overlookmotel 0260308afa refactor(ast_tools): prepare lazy deserializer codegen for visitor (#11836)
Pure refactor. Adapt lazy deserialization codegen in preparation for adding lazy visitor. Involves introducing a `State` struct, renaming things, and moving code around. No substantive changes.
2025-06-20 15:40:48 +00:00
overlookmotel b544be8fad refactor(napi/parser): remove options amendment from prepareRaw (#11828)
Pure refactor (raw transfer). Remove logic for amending `options` objects from `prepareRaw`. It's an unnatural place for it.
2025-06-20 10:11:35 +00:00
overlookmotel 6bbe048c98 perf(napi/parser): do not lazily create TextEncoder (#11817)
Tiny perf optimization to raw transfer. #11807 made this whole file lazy-loaded, so no need to lazily create the `TextEncoder` any more. If the file is imported, it's going to be used.
2025-06-19 14:05:00 +00:00
overlookmotel 4dc8a4e63b docs(napi/parser): add JSDoc comments to all functions (#11814)
Add JSDoc comments to all functions in `napi/parser`.
2025-06-19 14:05:00 +00:00
overlookmotel 9c960cd0d7 refactor(napi/parser): re-order code (#11813)
Pure refactor. Move `parseSync` to be before `parseAsync`, for consistency with all other code in `napi/parser`. Hoist all exports to top of file to make it a little more readable.
2025-06-19 13:21:50 +00:00
overlookmotel aef1770053 perf(napi/parser): destructure bindings on import (#11811)
Small perf optimization to `oxc-parser`. Destructure `bindings` object when importing it. Calling those methods repeatedly later on avoids overhead of an object property lookup each time.

The effect is infinitesimal, except when using lazy deserialization, which is so fast for small files that it makes sense to optimize every step of its call path.
2025-06-19 13:01:22 +00:00
overlookmotel bfed7f21ab refactor(napi/parser): rename file (#11808)
Pure refactor. Rename `raw-transfer/index.js` to `raw-transfer/common.js`. After #11807, it's no longer the entry point, so `index.js` is misleading.
2025-06-19 12:33:11 +00:00
overlookmotel 3a0a673b31 perf(napi/parser): lazy-load raw transfer and lazy deser code (#11807)
Raw transfer and raw lazy deserialization are experimental and not publicly advertised. Lazy-load all code for these features only when they're first used.

Lazy deserialization in particular requires a non-trivial amount of code.

`rawTransferSupported` still needs to be eagerly loaded, as user would call it before calling `parseSync` or `parseAsync`. So move it into a separate module.
2025-06-19 12:33:11 +00:00
overlookmotel 08e666fc2d refactor(ast/estree): add #[estree] attrs to RegExpFlagsAlias (#11794)
Pure refactor. Add more `#[estree]` attributes to `RegExpFlagsAlias` type. These have no effect on generated code for JSON serialization or raw transfer deserialization, as `RegExpFlagsAlias` already has a custom converter. But it simplifies the codegen for lazy deserialization.
2025-06-18 13:02:21 +00:00
oxc-bot 3de6b99990 release(crates): v0.73.2 (#11785) 2025-06-18 13:23:17 +08:00
overlookmotel 21c8852bde perf(napi/parser): faster deserialization of Vecs in raw transfer (#11776)
Small perf optimization to raw transfer. Don't use 2 different counters in the loop when deserializing `Vec`s. Do what Rust's slice iterators do, and use current and end pointers.
2025-06-17 13:56:36 +00:00
overlookmotel a47a6de311 fix(napi/parser): lazy deser: do not expose getElement method from NodeArray (#11777)
`getElement` is an internal method. Don't publicly expose it on `NodeArray` class.
2025-06-17 13:49:09 +00:00
oxc-bot 14f7033f66 release(crates): v0.73.1 (#11774) 2025-06-17 21:17:38 +08:00
LongYinan 81ef44329f feat(napi): add aarch64-linux-android target (#11769)
- Close https://github.com/oxc-project/oxc/issues/11566
2025-06-17 15:46:41 +08:00
overlookmotel 6feab7e748 fix(ast/estree): remove custom serializer for MethodDefinition key field (#11763)
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.
2025-06-16 21:37:42 +00:00
renovate[bot] 9525250ccb chore(deps): lock file maintenance npm packages (#11731) 2025-06-16 10:14:59 +08:00
overlookmotel 20efcd4bbf test(napi/parser): remove unnecessary RUN_SIMPLE_LAZY_TESTS env var (#11703)
This is unnecessary. One env var is sufficient.
2025-06-14 12:16:24 +00:00
overlookmotel 6848b242f3 test(napi/parser): lazy deser: tests for introspection of NodeArrays (#11702)
Add tests for results of `Object.keys`, `Object.getOwnPropertyDescriptors`, and other introspection methods on `NodeArray`s.
2025-06-14 12:16:24 +00:00
overlookmotel fcb3084f74 fix(napi/parser): lazy deser: remove outdated comments (#11699)
Remove outdated comments, and clean up comments for `NodeArray`.
2025-06-14 11:24:39 +00:00
overlookmotel d136acdf53 perf(napi/parser): lazy deser: remove getInternal function in NodeArray (#11698)
Move `getElement` function into `NodeArray` class definition, to avoid extra function call to intermediate `getInternal`. This is a hot path, so streamlining it is important for perf.
2025-06-14 10:55:57 +00:00
overlookmotel a6a82f9995 perf(napi/parser): lazy deser: avoid changing shape of NodeArray prototype (#11697)
Simple object shapes are faster. Adding extra property to `NodeArray.prototype` alters its shape. Prevent this by defining a placeholder method in class definition, and then mutating this property later, rather than adding a new property.
2025-06-14 10:55:57 +00:00
Ulrich Stark d057652c19 refactor(regular-expression): shorten Span construction (#11689) 2025-06-14 12:17:14 +08:00
overlookmotel dfdebc27e3 feat(napi/parser): lazy deserializer NodeArray slice method (#11680)
Implement an optimized `slice` method for `NodeArray` in lazy-deserialized AST. It returns a another `NodeArray`, instead of a plain `Array`, and doesn't deserialize any of the contents.
2025-06-13 18:31:18 +00:00
overlookmotel 2749931a40 fix(napi/parser): lazy deserializer block class constructors correctly (#11679)
Ensure get the appropriate error when user calls a forbidden class constructor for a lazy-deserializing type. Move code for this to a shared module.
2025-06-13 17:39:18 +00:00
overlookmotel 60f754e37c perf(napi/parser): lazily deserialize Vecs (#11678)
Lazy deserialization return `NodeArray`s which deserialize their contents lazily, instead of plain JS arrays.

`program.body.find(stmt => stmt.type === 'ImportDeclaration')` now only deserializes the nodes up until first `ImportDeclaration`, rather than deserializing every statement in the program into an array, and then searching it. Similarly getting `program.body.length` does not deserialize any nodes, and all other array methods are similarly optimized.

Also provide an optimized iterator for the common case of:

```js
for (const stmt of program.body) {
  doStuff(stmt);
}
```
2025-06-13 16:45:15 +00:00
overlookmotel f1f3c30606 refactor(napi/parser): lazy deserializer: prefix local cache property keys with $ (#11673)
Pure refactor. We prefix some property names on `internal` objects with `$` to avoid clashes between static properties (`pos`, `ast`) and cached fields. Switch the prefixing around - now prefix keys for cached fields, so the internal properties are called e.g. `pos` not `$pos`. This shortens generated code, and makes writing implementations simpler.
2025-06-13 12:41:13 +00:00
overlookmotel e523d864ea fix(napi/parser): lazy deserializer locally cache all Vecs and strings (#11667)
Fix a bug in lazy deserializer where `Vec`s were not getting cached because they were wrapped in `Option`. When determining if a node's field needs to be locally cached, drill down through any wrapping `Option`s / `Box`es / `Cell`s / enums.
2025-06-13 10:44:05 +00:00
oxc-bot e7af8c470c release(crates): v0.73.0 (#11669) 2025-06-13 18:33:56 +08:00
overlookmotel 586019549d feat(napi/parser): improved console.log output for lazy deserialized AST (#11642)
Improve the output from `console.log` when printing a lazy-deserialized AST.

Previously `console.log(program)` printed `Program { type: 'Program' }`. Now it prints much the same as a normal (eagerly deserialized) AST:

```
Program {
  type: 'Program',
  start: 0,
  end: 17,
  sourceType: SourceType { sourceType: 'module' },
  hashbang: null,
  body: [
    VariableDeclaration {
      type: 'VariableDeclaration',
      start: 0,
      end: 10,
      kind: 'let',
      declarations: [Array],
      declare: false
    }
  ]
}
```
2025-06-12 16:05:00 +00:00
overlookmotel 931fc739c3 fix(napi/parser): cache nodes in lazy deserialization (#11637)
In lazy-deserialized AST, cache node objects, so accessing the same field twice returns the same object. Previously `assert(binaryExpression.left == binaryExpression.left)` would fail, as each access returned a new object.

`pos` (i.e. memory address) is used as the cache key, with 2 exceptions: `RegExp` and `Program`.

`RegExp` doesn't have a `span: Span` field, so its first field `RegExpPattern` has the same memory address as the `RegExp`. So `pos` would not be a unique key.

```rs
pub struct RegExp<'a> {
    pub pattern: RegExpPattern<'a>,
    pub flags: RegExpFlags,
}

pub struct RegExpPattern<'a> {
    pub text: Atom<'a>,
    pub pattern: Option<Box<'a, Pattern<'a>>>,
}
```

The codegen identifies these cases, and applies a workaround - `pos + 1` is used as the cache key instead of `pos`, so the parent and child have different cache keys.
2025-06-12 14:26:06 +00:00