Skip to content

Commit

Permalink
feat: support sourcemap option for isolated_declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Aug 24, 2024
1 parent d29042e commit 0027821
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
9 changes: 7 additions & 2 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
30 changes: 23 additions & 7 deletions napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
1 change: 1 addition & 0 deletions napi/transform/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
22 changes: 16 additions & 6 deletions napi/transform/test.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 12 additions & 2 deletions npm/oxc-transform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0027821

Please sign in to comment.