diff --git a/napi/parser/header.js b/napi/parser/header.js new file mode 100644 index 000000000..a89dec6b1 --- /dev/null +++ b/napi/parser/header.js @@ -0,0 +1,4 @@ +/* auto-generated by NAPI-RS */ +/* eslint-disable */ + +export * from '@oxc-project/types'; diff --git a/napi/parser/index.d.ts b/napi/parser/index.d.ts index 615f6528d..d2bc98de3 100644 --- a/napi/parser/index.d.ts +++ b/napi/parser/index.d.ts @@ -1,5 +1,7 @@ /* auto-generated by NAPI-RS */ /* eslint-disable */ + +export * from '@oxc-project/types'; export interface Comment { type: 'Line' | 'Block' value: string @@ -7,7 +9,27 @@ export interface Comment { end: number } -export interface ExportSpecifier { +export interface ModuleLexer { + imports: Array + exports: Array + /** + * ESM syntax detection + * + * The use of ESM syntax: import / export statements and `import.meta` + */ + hasModuleSyntax: boolean + /** Facade modules that only use import / export syntax */ + facade: boolean +} + +/** + * # Panics + * + * * Tokio crashes + */ +export declare function moduleLexerAsync(sourceText: string, options?: ParserOptions | undefined | null): Promise + +export interface ModuleLexerExportSpecifier { /** Exported name */ n: string /** Local name, or undefined. */ @@ -22,7 +44,7 @@ export interface ExportSpecifier { le?: number } -export interface ImportSpecifier { +export interface ModuleLexerImportSpecifier { /** * Module name * @@ -54,26 +76,6 @@ export interface ImportSpecifier { a: number } -export interface ModuleLexer { - imports: Array - exports: Array - /** - * ESM syntax detection - * - * The use of ESM syntax: import / export statements and `import.meta` - */ - hasModuleSyntax: boolean - /** Facade modules that only use import / export syntax */ - facade: boolean -} - -/** - * # Panics - * - * * Tokio crashes - */ -export declare function moduleLexerAsync(sourceText: string, options?: ParserOptions | undefined | null): Promise - /** * Outputs the list of exports and locations of import specifiers, * including dynamic import and import meta handling. diff --git a/napi/parser/package.json b/napi/parser/package.json index 9cb600e16..10737c734 100644 --- a/napi/parser/package.json +++ b/napi/parser/package.json @@ -3,10 +3,7 @@ "private": true, "scripts": { "build": "napi build --platform --release --js bindings.js", - "test": "vitest run ./test" - }, - "engines": { - "node": ">=14.*" + "test": "vitest --typecheck run ./test" }, "napi": { "binaryName": "parser", @@ -19,6 +16,10 @@ "aarch64-unknown-linux-musl", "x86_64-apple-darwin", "aarch64-apple-darwin" - ] + ], + "dtsHeaderFile": "header.js" + }, + "dependencies": { + "@oxc-project/types": "workspace:^" } } diff --git a/napi/parser/src/module_lexer.rs b/napi/parser/src/module_lexer.rs index 39f53852c..28155751b 100644 --- a/napi/parser/src/module_lexer.rs +++ b/napi/parser/src/module_lexer.rs @@ -7,7 +7,7 @@ use oxc_module_lexer::ImportType; use crate::{parse, ParserOptions}; #[napi(object)] -pub struct ImportSpecifier { +pub struct ModuleLexerImportSpecifier { /// Module name /// /// To handle escape sequences in specifier strings, the .n field of imported specifiers will be provided where possible. @@ -40,7 +40,7 @@ pub struct ImportSpecifier { } #[napi(object)] -pub struct ExportSpecifier { +pub struct ModuleLexerExportSpecifier { /// Exported name pub n: String, @@ -60,7 +60,7 @@ pub struct ExportSpecifier { pub le: Option, } -impl<'a> From> for ImportSpecifier { +impl<'a> From> for ModuleLexerImportSpecifier { #[allow(clippy::cast_lossless)] fn from(i: oxc_module_lexer::ImportSpecifier) -> Self { Self { @@ -80,7 +80,7 @@ impl<'a> From> for ImportSpecifier { } } -impl<'a> From> for ExportSpecifier { +impl<'a> From> for ModuleLexerExportSpecifier { fn from(e: oxc_module_lexer::ExportSpecifier) -> Self { Self { n: e.n.to_string(), @@ -95,9 +95,9 @@ impl<'a> From> for ExportSpecifier { #[napi(object)] pub struct ModuleLexer { - pub imports: Vec, + pub imports: Vec, - pub exports: Vec, + pub exports: Vec, /// ESM syntax detection /// @@ -113,8 +113,8 @@ fn module_lexer(source_text: &str, options: &ParserOptions) -> ModuleLexer { let allocator = Allocator::default(); let ret = parse(&allocator, source_text, options); let module_lexer = oxc_module_lexer::ModuleLexer::new().build(&ret.program); - let imports = module_lexer.imports.into_iter().map(ImportSpecifier::from).collect(); - let exports = module_lexer.exports.into_iter().map(ExportSpecifier::from).collect(); + let imports = module_lexer.imports.into_iter().map(ModuleLexerImportSpecifier::from).collect(); + let exports = module_lexer.exports.into_iter().map(ModuleLexerExportSpecifier::from).collect(); ModuleLexer { imports, exports, diff --git a/napi/parser/test/module_lexer.test.mjs b/napi/parser/test/module_lexer.test.ts similarity index 100% rename from napi/parser/test/module_lexer.test.mjs rename to napi/parser/test/module_lexer.test.ts diff --git a/napi/parser/test/parse.test.mjs b/napi/parser/test/parse.test.ts similarity index 100% rename from napi/parser/test/parse.test.mjs rename to napi/parser/test/parse.test.ts diff --git a/napi/parser/test/parser.test-d.ts b/napi/parser/test/parser.test-d.ts new file mode 100644 index 000000000..74bff1ac9 --- /dev/null +++ b/napi/parser/test/parser.test-d.ts @@ -0,0 +1,13 @@ +import { assertType, describe, it } from 'vitest'; + +import type { Statement } from '../index'; +import * as oxc from '../index'; + +describe('parse', () => { + const code = '/* comment */ foo'; + + it('checks type', async () => { + const ret = oxc.parseSync(code); + assertType(ret.program.body[0]); + }); +}); diff --git a/napi/parser/tsconfig.json b/napi/parser/tsconfig.json new file mode 100644 index 000000000..abb88a3e0 --- /dev/null +++ b/napi/parser/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "module": "Preserve", + "moduleResolution": "Bundler", + "target": "ESNext" + } +} diff --git a/package.json b/package.json index 7645e3536..fb9495276 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "devDependencies": { "@napi-rs/cli": "catalog:", + "typescript": "catalog:", "vitest": "catalog:" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d2397522..fd68b5cdb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,6 +9,9 @@ catalogs: '@napi-rs/cli': specifier: 3.0.0-alpha.61 version: 3.0.0-alpha.61 + typescript: + specifier: 5.6.3 + version: 5.6.3 vitest: specifier: 2.1.2 version: 2.1.2 @@ -20,6 +23,9 @@ importers: '@napi-rs/cli': specifier: 'catalog:' version: 3.0.0-alpha.61(@emnapi/runtime@1.2.0) + typescript: + specifier: 'catalog:' + version: 5.6.3 vitest: specifier: 'catalog:' version: 2.1.2(@types/node@22.9.0) @@ -54,7 +60,11 @@ importers: napi/minify: {} - napi/parser: {} + napi/parser: + dependencies: + '@oxc-project/types': + specifier: workspace:^ + version: link:../../npm/oxc-types napi/transform: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 50a625a47..4d64ad332 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -10,3 +10,4 @@ packages: catalog: "@napi-rs/cli": 3.0.0-alpha.61 "vitest": 2.1.2 + "typescript": 5.6.3