Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/generated/ast_changes_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ src:
- 'napi/parser/generated/lazy/walk.mjs'
- 'napi/parser/generated/visit/keys.mjs'
- 'napi/parser/generated/visit/types.mjs'
- 'napi/parser/generated/visit/visitor.d.ts'
- 'napi/parser/generated/visit/visitor.d.mts'
- 'napi/parser/generated/visit/walk.mjs'
- 'napi/parser/src/generated/assert_layouts.rs'
- 'napi/parser/src/generated/derive_estree.rs'
Expand Down
2 changes: 1 addition & 1 deletion apps/oxlint/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const parserFilePaths = [
*/
'generated/deserialize/ts.mjs',
'generated/visit/types.mjs',
'generated/visit/visitor.d.ts',
'generated/visit/visitor.d.mts',
'generated/visit/walk.mjs',
];

Expand Down
2 changes: 1 addition & 1 deletion apps/oxlint/src-js/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface Visitor {
}
*/

import type { VisitorObject as Visitor } from '../../dist/generated/visit/visitor.d.ts';
import type { VisitorObject as Visitor } from '../../dist/generated/visit/visitor.d.mts';
export type { Visitor };

// Hook function that runs before traversal.
Expand Down
1 change: 1 addition & 0 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"napi/{parser,transform,minify,playground}/**/wasi-worker.mjs",
"napi/{parser,transform,minify,playground}/**/browser.js",
"napi/parser/src-js/bindings.mjs",
"napi/parser/src-js/index.d.mts",
"npm/*/package.json",
"npm/oxlint/configuration_schema.json",
"npm/oxc-wasm/**",
Expand Down
8 changes: 4 additions & 4 deletions napi/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src-js/index.mjs",
"browser": "src-js/wasm.mjs",
"scripts": {
"build-dev": "napi build --esm --platform --js bindings.mjs --output-dir src-js",
"build-dev": "napi build --esm --platform --js bindings.mjs --dts index.d.mts --output-dir src-js",
"build-test": "pnpm run build-dev --profile coverage",
"build": "pnpm run build-dev --features allocator --release",
"postbuild-dev": "node scripts/patch.mjs",
Expand Down Expand Up @@ -46,10 +46,10 @@
"generated/lazy/walk.mjs",
"generated/visit/keys.mjs",
"generated/visit/types.mjs",
"generated/visit/visitor.d.ts",
"generated/visit/visitor.d.mts",
"generated/visit/walk.mjs",
"src-js/bindings.mjs",
"src-js/index.d.ts",
"src-js/index.d.mts",
"src-js/index.mjs",
"src-js/wasm.mjs",
"src-js/webcontainer-fallback.js",
Expand Down Expand Up @@ -107,6 +107,6 @@
"fs": false
}
},
"dtsHeaderFile": "src-js/header.d.ts"
"dtsHeaderFile": "src-js/header.d.mts"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
/* eslint-disable */

import type { Program } from '@oxc-project/types';
import type { VisitorObject } from '../generated/visit/visitor.d.ts';
import type { VisitorObject } from '../generated/visit/visitor.d.mts';

export * from '@oxc-project/types';

export { VisitorObject };

export const visitorKeys: Record<string, string[]>;

export class Visitor {
constructor(visitor: VisitorObject);
visit(program: Program): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
/* eslint-disable */

import type { Program } from '@oxc-project/types';
import type { VisitorObject } from '../generated/visit/visitor.d.ts';
import type { VisitorObject } from '../generated/visit/visitor.d.mts';

export * from '@oxc-project/types';

export { VisitorObject };

export const visitorKeys: Record<string, string[]>;

export class Visitor {
constructor(visitor: VisitorObject);
visit(program: Program): void;
Expand Down
20 changes: 13 additions & 7 deletions napi/parser/test/parse-raw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { basename, join as pathJoin } from 'node:path';
import Tinypool from 'tinypool';
import { describe, expect, it } from 'vitest';

import { parseAsync, parseSync } from '../src-js/index.mjs';
import { parseAsync, parseSync, type TSTypeAliasDeclaration, type VariableDeclaration } from '../src-js/index.mjs';

import {
ACORN_TEST262_DIR_PATH,
Expand Down Expand Up @@ -273,7 +273,8 @@ describe.concurrent('`preserveParens` option', () => {
// @ts-ignore
let ret = parseSync('test.js', code, { experimentalRawTransfer: true, preserveParens: false });
expect(ret.errors.length).toBe(0);
expect(ret.program.body[0].declarations[0].init.type).toBe('BinaryExpression');
const firstStatement = ret.program.body[0] as VariableDeclaration;
expect(firstStatement.declarations[0].init.type).toBe('BinaryExpression');
});

it.concurrent('TS', async () => {
Expand All @@ -282,8 +283,10 @@ describe.concurrent('`preserveParens` option', () => {
// @ts-ignore
let ret = parseSync('test.ts', code, { experimentalRawTransfer: true, preserveParens: false });
expect(ret.errors.length).toBe(0);
expect(ret.program.body[0].declarations[0].init.type).toBe('BinaryExpression');
expect(ret.program.body[1].typeAnnotation.type).toBe('TSStringKeyword');
const firstStatement = ret.program.body[0] as VariableDeclaration;
expect(firstStatement.declarations[0].init.type).toBe('BinaryExpression');
const secondStatement = ret.program.body[1] as TSTypeAliasDeclaration;
expect(secondStatement.typeAnnotation.type).toBe('TSStringKeyword');
});
});

Expand All @@ -294,7 +297,8 @@ describe.concurrent('`preserveParens` option', () => {
// @ts-ignore
let ret = parseSync('test.js', code, { experimentalRawTransfer: true, preserveParens: true });
expect(ret.errors.length).toBe(0);
expect(ret.program.body[0].declarations[0].init.type).toBe('ParenthesizedExpression');
const firstStatement = ret.program.body[0] as VariableDeclaration;
expect(firstStatement.declarations[0].init.type).toBe('ParenthesizedExpression');
});

it.concurrent('TS', async () => {
Expand All @@ -303,8 +307,10 @@ describe.concurrent('`preserveParens` option', () => {
// @ts-ignore
let ret = parseSync('test.ts', code, { experimentalRawTransfer: true, preserveParens: true });
expect(ret.errors.length).toBe(0);
expect(ret.program.body[0].declarations[0].init.type).toBe('ParenthesizedExpression');
expect(ret.program.body[1].typeAnnotation.type).toBe('TSParenthesizedType');
const firstStatement = ret.program.body[0] as VariableDeclaration;
expect(firstStatement.declarations[0].init.type).toBe('ParenthesizedExpression');
const secondStatement = ret.program.body[1] as TSTypeAliasDeclaration;
expect(secondStatement.typeAnnotation.type).toBe('TSParenthesizedType');
});
});
});
15 changes: 10 additions & 5 deletions napi/parser/test/visit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { parseSync, Visitor, visitorKeys } from '../src-js/index.mjs';
import { parseSync, Visitor, visitorKeys, type VisitorObject } from '../src-js/index.mjs';

describe('visit', () => {
it('empty visitor', () => {
Expand All @@ -12,6 +12,7 @@ describe('visit', () => {

describe('invalid visitor', () => {
it('undefined visitor object', () => {
// @ts-ignore
expect(() => new Visitor()).toThrow(new TypeError('Visitor must be an object'));
});

Expand All @@ -20,19 +21,23 @@ describe('visit', () => {
});

it('boolean visitor object', () => {
expect(() => new Visitor(true)).toThrow(new TypeError('Visitor must be an object'));
expect(() => new Visitor(true as unknown as VisitorObject)).toThrow(new TypeError('Visitor must be an object'));
});

it('unknown type in entry', () => {
expect(() => new Visitor({ Foo() {} })).toThrow(new Error("Unknown node type 'Foo' in visitor object"));
expect(() => new Visitor({ Foo() {} } as VisitorObject)).toThrow(
new Error("Unknown node type 'Foo' in visitor object"),
);
});

it('unknown type in exit', () => {
expect(() => new Visitor({ 'Foo:exit'() {} })).toThrow(new Error("Unknown node type 'Foo' in visitor object"));
expect(() => new Visitor({ 'Foo:exit'() {} } as VisitorObject)).toThrow(
new Error("Unknown node type 'Foo' in visitor object"),
);
});

it('invalid postfix', () => {
expect(() => new Visitor({ 'Identifier:foo'() {} })).toThrow(
expect(() => new Visitor({ 'Identifier:foo'() {} } as VisitorObject)).toThrow(
new Error("Unknown node type 'Identifier:foo' in visitor object"),
);
});
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/estree_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Generator for ESTreeVisitGenerator {
code: type_ids_map,
},
Output::Javascript {
path: format!("{NAPI_PARSER_PACKAGE_PATH}/generated/visit/visitor.d.ts"),
path: format!("{NAPI_PARSER_PACKAGE_PATH}/generated/visit/visitor.d.mts"),
code: visitor_type,
},
]
Expand Down
Loading