diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index e451937b7..e8ab830fc 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -18,11 +18,16 @@ export interface Es2015BindingOptions { } /** TypeScript Isolated Declarations for Standalone DTS Emit */ -export declare function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult +export declare function isolatedDeclaration(filename: string, sourceText: string, options: IsolatedDeclarationsOptions): IsolatedDeclarationsResult + +export interface IsolatedDeclarationsOptions { + sourcemap: boolean +} export interface IsolatedDeclarationsResult { - sourceText: string errors: Array + sourceText: string + sourceMap?: SourceMap } /** diff --git a/napi/transform/src/isolated_declaration.rs b/napi/transform/src/isolated_declaration.rs index c9609bfde..04e6a27ef 100644 --- a/napi/transform/src/isolated_declaration.rs +++ b/napi/transform/src/isolated_declaration.rs @@ -5,27 +5,43 @@ use oxc_codegen::CodegenReturn; use oxc_isolated_declarations::IsolatedDeclarations; use oxc_span::SourceType; -use crate::context::TransformContext; +use crate::{context::TransformContext, SourceMap, TransformOptions}; #[napi(object)] pub struct IsolatedDeclarationsResult { - pub source_text: String, - // TODO: should we expose source maps? - // pub source_map: Option, pub errors: Vec, + pub source_text: String, + pub source_map: Option, +} + +#[napi(object)] +pub struct IsolatedDeclarationsOptions { + pub sourcemap: bool, } /// TypeScript Isolated Declarations for Standalone DTS Emit #[allow(clippy::needless_pass_by_value)] #[napi] -pub fn isolated_declaration(filename: String, source_text: String) -> IsolatedDeclarationsResult { +pub fn isolated_declaration( + filename: String, + source_text: String, + options: IsolatedDeclarationsOptions, +) -> IsolatedDeclarationsResult { let source_type = SourceType::from_path(&filename).unwrap_or_default().with_typescript(true); let allocator = Allocator::default(); - let ctx = TransformContext::new(&allocator, &filename, &source_text, source_type, None); + let ctx = TransformContext::new( + &allocator, + &filename, + &source_text, + source_type, + Some(TransformOptions { sourcemap: Some(options.sourcemap), ..Default::default() }), + ); let transformed_ret = build_declarations(&ctx); + IsolatedDeclarationsResult { - source_text: transformed_ret.source_text, errors: ctx.take_and_render_reports(), + source_text: transformed_ret.source_text, + source_map: options.sourcemap.then(|| transformed_ret.source_map.map(Into::into)).flatten(), } } diff --git a/napi/transform/src/options.rs b/napi/transform/src/options.rs index d462b03a5..b7bbcdb51 100644 --- a/napi/transform/src/options.rs +++ b/napi/transform/src/options.rs @@ -169,6 +169,7 @@ impl From for ES2015Options { /// /// @see {@link transform} #[napi(object)] +#[derive(Default)] pub struct TransformOptions { #[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")] pub source_type: Option, diff --git a/napi/transform/test.mjs b/napi/transform/test.mjs index c7bbcc6e8..4e365352f 100644 --- a/napi/transform/test.mjs +++ b/napi/transform/test.mjs @@ -1,15 +1,25 @@ -import oxc from './index.js'; -import assert from 'assert'; +import oxc from "./index.js"; +import assert from "assert"; -console.log(`Testing on ${process.platform}-${process.arch}`) +console.log(`Testing on ${process.platform}-${process.arch}`); -test(oxc.isolatedDeclaration("test.ts", "class A {}"), "declare class A {}\n"); +test(oxc.isolatedDeclaration("test.ts", "class A {}", { sourcemap: true }), { + sourceText: "declare class A {}\n", + sourceMap: { + mappings: "AAAA,cAAM,EAAE,CAAE", + names: [], + sources: ["test.ts"], + sourcesContent: ["class A {}"], + }, +}); function test(ret, expected) { console.log(ret.sourceText); + console.log(ret.sourceMap); for (const error of ret.errors) { - console.log(error) + console.log(error); } - assert.equal(ret.sourceText, expected); + assert.equal(ret.sourceText, expected.sourceText); + assert.deepEqual(ret.sourceMap, expected.sourceMap); assert(ret.errors.length == 0); } diff --git a/npm/oxc-transform/README.md b/npm/oxc-transform/README.md index 10a1178b4..71a8b1db9 100644 --- a/npm/oxc-transform/README.md +++ b/npm/oxc-transform/README.md @@ -12,16 +12,26 @@ This is still in alpha and may yield incorrect results, feel free to [submit a b import assert from 'assert'; import oxc from 'oxc-transform'; -const { sourceText, errors } = oxc.isolatedDeclaration("test.ts", "class A {}"); +const { sourceMap, sourceText, errors } = oxc.isolatedDeclaration("test.ts", "class A {}", { sourcemap: true }); assert.equal(sourceText, "declare class A {}\n"); +assert.deepEqual(ret.sourceMap, { + mappings: "AAAA,cAAM,EAAE,CAAE", + names: [], + sources: ["test.ts"], + sourcesContent: ["class A {}"], +}); assert(errors.length == 0); ``` ### API ```typescript -export function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult +export function isolatedDeclaration(filename: string, sourceText: string, options: IsolatedDeclarationsOptions): IsolatedDeclarationsResult + +export interface IsolatedDeclarationsOptions { + sourcemap: boolean +} export interface IsolatedDeclarationsResult { sourceText: string