From 407429a09cec505909ec04fc4f545bfb0c1215ac Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 9 Jul 2025 09:13:18 +0000 Subject: [PATCH] feat(napi/parser,napi/transform): accept `lang=dts` (#12154) --- crates/oxc_napi/src/lib.rs | 1 + napi/parser/index.d.ts | 4 ++-- napi/parser/src/types.rs | 4 ++-- napi/parser/test/parse.test.ts | 8 +++++++- napi/transform/index.d.ts | 4 ++-- napi/transform/src/transformer.rs | 4 ++-- pnpm-lock.yaml | 2 -- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/oxc_napi/src/lib.rs b/crates/oxc_napi/src/lib.rs index 430573dfc6ee4..6fe70318a5300 100644 --- a/crates/oxc_napi/src/lib.rs +++ b/crates/oxc_napi/src/lib.rs @@ -68,6 +68,7 @@ pub fn get_source_type( Some("jsx") => SourceType::jsx(), Some("ts") => SourceType::ts(), Some("tsx") => SourceType::tsx(), + Some("dts") => SourceType::d_ts(), _ => SourceType::from_path(filename).unwrap_or_default(), }; match source_type { diff --git a/napi/parser/index.d.ts b/napi/parser/index.d.ts index 7ea81ee0e379c..a57863391a57e 100644 --- a/napi/parser/index.d.ts +++ b/napi/parser/index.d.ts @@ -137,8 +137,8 @@ export declare const enum ImportNameKind { export declare function parseAsync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): Promise export interface ParserOptions { - /** Treat the source text as `js`, `jsx`, `ts`, or `tsx`. */ - lang?: 'js' | 'jsx' | 'ts' | 'tsx' + /** Treat the source text as `js`, `jsx`, `ts`, `tsx` or `dts`. */ + lang?: 'js' | 'jsx' | 'ts' | 'tsx' | 'dts' /** Treat the source text as `script` or `module` code. */ sourceType?: 'script' | 'module' | 'unambiguous' | undefined /** diff --git a/napi/parser/src/types.rs b/napi/parser/src/types.rs index 222fcbba18143..7a41ef1e4c528 100644 --- a/napi/parser/src/types.rs +++ b/napi/parser/src/types.rs @@ -7,8 +7,8 @@ use oxc_napi::{Comment, OxcError}; #[napi(object)] #[derive(Default)] pub struct ParserOptions { - /// Treat the source text as `js`, `jsx`, `ts`, or `tsx`. - #[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")] + /// Treat the source text as `js`, `jsx`, `ts`, `tsx` or `dts`. + #[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx' | 'dts'")] pub lang: Option, /// Treat the source text as `script` or `module` code. diff --git a/napi/parser/test/parse.test.ts b/napi/parser/test/parse.test.ts index 7fb2636580a8b..092b1e7c43f94 100644 --- a/napi/parser/test/parse.test.ts +++ b/napi/parser/test/parse.test.ts @@ -2,7 +2,7 @@ import { Worker } from 'node:worker_threads'; import { describe, expect, it, test } from 'vitest'; import { parseAsync, parseSync } from '../index.js'; -import type { ExpressionStatement, ParserOptions, TSTypeAliasDeclaration } from '../index.js'; +import type { ExpressionStatement, ParserOptions, TSTypeAliasDeclaration, VariableDeclaration } from '../index.js'; describe('parse', () => { const code = '/* comment */ foo'; @@ -55,6 +55,12 @@ describe('parse', () => { // Parsed as `await 1` expect((ret.program.body[0] as ExpressionStatement).expression.type).toBe('AwaitExpression'); }); + test('sets lang as dts', () => { + const code = 'declare const foo'; + const ret = parseSync('test', code, { lang: 'dts' }); + expect(ret.errors.length).toBe(0); + expect((ret.program.body[0] as VariableDeclaration).declare).toBe(true); + }); }); describe('TS properties', () => { diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index 90407f524efdd..c1ce33cb8b26a 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -359,8 +359,8 @@ export declare function transform(filename: string, sourceText: string, options? * @see {@link transform} */ export interface TransformOptions { - /** Treat the source text as `js`, `jsx`, `ts`, or `tsx`. */ - lang?: 'js' | 'jsx' | 'ts' | 'tsx' + /** Treat the source text as `js`, `jsx`, `ts`, `tsx`, or `dts`. */ + lang?: 'js' | 'jsx' | 'ts' | 'tsx' | 'dts' /** Treat the source text as `script` or `module` code. */ sourceType?: 'script' | 'module' | 'unambiguous' | undefined /** diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index a2e4ddca2822d..ba962e13e93e9 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -86,8 +86,8 @@ pub struct TransformResult { #[napi(object)] #[derive(Default)] pub struct TransformOptions { - /// Treat the source text as `js`, `jsx`, `ts`, or `tsx`. - #[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")] + /// Treat the source text as `js`, `jsx`, `ts`, `tsx`, or `dts`. + #[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx' | 'dts'")] pub lang: Option, /// Treat the source text as `script` or `module` code. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3b9a9b21dc87d..371afdb96ab71 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -146,8 +146,6 @@ importers: npm/runtime: {} - npm/wasm-web: {} - tasks/compat_data: devDependencies: degit: