feat(isolated_declaration): support sourcemap option (#5170)

closes #4313
This commit is contained in:
dalaoshu 2024-08-25 13:32:47 +08:00 committed by GitHub
parent 1e8d3dde3b
commit 72740b3f78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 17 deletions

View file

@ -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<string>
sourceText: string
sourceMap?: SourceMap
}
/**

View file

@ -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<SourceMap>,
pub errors: Vec<String>,
pub source_text: String,
pub source_map: Option<SourceMap>,
}
#[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(),
}
}

View file

@ -169,6 +169,7 @@ impl From<ES2015BindingOptions> for ES2015Options {
///
/// @see {@link transform}
#[napi(object)]
#[derive(Default)]
pub struct TransformOptions {
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
pub source_type: Option<String>,

View file

@ -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);
}

View file

@ -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