Skip to content

Commit 407429a

Browse files
committed
feat(napi/parser,napi/transform): accept lang=dts (#12154)
1 parent 12e4ec7 commit 407429a

File tree

7 files changed

+16
-11
lines changed

7 files changed

+16
-11
lines changed

crates/oxc_napi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn get_source_type(
6868
Some("jsx") => SourceType::jsx(),
6969
Some("ts") => SourceType::ts(),
7070
Some("tsx") => SourceType::tsx(),
71+
Some("dts") => SourceType::d_ts(),
7172
_ => SourceType::from_path(filename).unwrap_or_default(),
7273
};
7374
match source_type {

napi/parser/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ export declare const enum ImportNameKind {
137137
export declare function parseAsync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): Promise<ParseResult>
138138

139139
export interface ParserOptions {
140-
/** Treat the source text as `js`, `jsx`, `ts`, or `tsx`. */
141-
lang?: 'js' | 'jsx' | 'ts' | 'tsx'
140+
/** Treat the source text as `js`, `jsx`, `ts`, `tsx` or `dts`. */
141+
lang?: 'js' | 'jsx' | 'ts' | 'tsx' | 'dts'
142142
/** Treat the source text as `script` or `module` code. */
143143
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
144144
/**

napi/parser/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use oxc_napi::{Comment, OxcError};
77
#[napi(object)]
88
#[derive(Default)]
99
pub struct ParserOptions {
10-
/// Treat the source text as `js`, `jsx`, `ts`, or `tsx`.
11-
#[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")]
10+
/// Treat the source text as `js`, `jsx`, `ts`, `tsx` or `dts`.
11+
#[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx' | 'dts'")]
1212
pub lang: Option<String>,
1313

1414
/// Treat the source text as `script` or `module` code.

napi/parser/test/parse.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Worker } from 'node:worker_threads';
22
import { describe, expect, it, test } from 'vitest';
33

44
import { parseAsync, parseSync } from '../index.js';
5-
import type { ExpressionStatement, ParserOptions, TSTypeAliasDeclaration } from '../index.js';
5+
import type { ExpressionStatement, ParserOptions, TSTypeAliasDeclaration, VariableDeclaration } from '../index.js';
66

77
describe('parse', () => {
88
const code = '/* comment */ foo';
@@ -55,6 +55,12 @@ describe('parse', () => {
5555
// Parsed as `await 1`
5656
expect((ret.program.body[0] as ExpressionStatement).expression.type).toBe('AwaitExpression');
5757
});
58+
test('sets lang as dts', () => {
59+
const code = 'declare const foo';
60+
const ret = parseSync('test', code, { lang: 'dts' });
61+
expect(ret.errors.length).toBe(0);
62+
expect((ret.program.body[0] as VariableDeclaration).declare).toBe(true);
63+
});
5864
});
5965

6066
describe('TS properties', () => {

napi/transform/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ export declare function transform(filename: string, sourceText: string, options?
359359
* @see {@link transform}
360360
*/
361361
export interface TransformOptions {
362-
/** Treat the source text as `js`, `jsx`, `ts`, or `tsx`. */
363-
lang?: 'js' | 'jsx' | 'ts' | 'tsx'
362+
/** Treat the source text as `js`, `jsx`, `ts`, `tsx`, or `dts`. */
363+
lang?: 'js' | 'jsx' | 'ts' | 'tsx' | 'dts'
364364
/** Treat the source text as `script` or `module` code. */
365365
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
366366
/**

napi/transform/src/transformer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ pub struct TransformResult {
8686
#[napi(object)]
8787
#[derive(Default)]
8888
pub struct TransformOptions {
89-
/// Treat the source text as `js`, `jsx`, `ts`, or `tsx`.
90-
#[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")]
89+
/// Treat the source text as `js`, `jsx`, `ts`, `tsx`, or `dts`.
90+
#[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx' | 'dts'")]
9191
pub lang: Option<String>,
9292

9393
/// Treat the source text as `script` or `module` code.

pnpm-lock.yaml

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)