mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(isolated_declaration): support sourcemap option (#5170)
closes #4313
This commit is contained in:
parent
1e8d3dde3b
commit
72740b3f78
5 changed files with 59 additions and 17 deletions
9
napi/transform/index.d.ts
vendored
9
napi/transform/index.d.ts
vendored
|
|
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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>,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue