Skip to content

Commit 3ccda45

Browse files
committed
feat(napi/transform)!: align output API sourceText -> code with babel
And `sourceMap` -> `map` closes #5397
1 parent 3cb9452 commit 3ccda45

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

napi/transform/index.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ export interface Es2015BindingOptions {
1818
}
1919

2020
/** TypeScript Isolated Declarations for Standalone DTS Emit */
21-
export declare function isolatedDeclaration(filename: string, sourceText: string, options: IsolatedDeclarationsOptions): IsolatedDeclarationsResult
21+
function isolatedDeclaration(filename: string, sourceText: string, options: IsolatedDeclarationsOptions): IsolatedDeclarationsResult
2222

2323
export interface IsolatedDeclarationsOptions {
2424
sourcemap: boolean
2525
}
2626

2727
export interface IsolatedDeclarationsResult {
28+
code: string
29+
map?: SourceMap
2830
errors: Array<string>
29-
sourceText: string
30-
sourceMap?: SourceMap
3131
}
3232

3333
/**
@@ -136,7 +136,7 @@ export interface SourceMap {
136136
* @returns an object containing the transformed code, source maps, and any
137137
* errors that occurred during parsing or transformation.
138138
*/
139-
export declare function transform(filename: string, sourceText: string, options?: TransformOptions | undefined | null): TransformResult
139+
function transform(filename: string, sourceText: string, options?: TransformOptions | undefined | null): TransformResult
140140

141141
/**
142142
* Options for transforming a JavaScript or TypeScript file.
@@ -180,13 +180,13 @@ export interface TransformResult {
180180
*
181181
* If parsing failed, this will be an empty string.
182182
*/
183-
sourceText: string
183+
code: string
184184
/**
185185
* The source map for the transformed code.
186186
*
187187
* This will be set if {@link TransformOptions#sourcemap} is `true`.
188188
*/
189-
sourceMap?: SourceMap
189+
map?: SourceMap
190190
/**
191191
* The `.d.ts` declaration file for the transformed code. Declarations are
192192
* only generated if `declaration` is set to `true` and a TypeScript file

napi/transform/src/isolated_declaration.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use crate::{context::TransformContext, SourceMap, TransformOptions};
99

1010
#[napi(object)]
1111
pub struct IsolatedDeclarationsResult {
12+
pub code: String,
13+
pub map: Option<SourceMap>,
1214
pub errors: Vec<String>,
13-
pub source_text: String,
14-
pub source_map: Option<SourceMap>,
1515
}
1616

1717
#[napi(object)]
@@ -39,9 +39,9 @@ pub fn isolated_declaration(
3939
let transformed_ret = build_declarations(&ctx);
4040

4141
IsolatedDeclarationsResult {
42+
code: transformed_ret.source_text,
43+
map: options.sourcemap.then(|| transformed_ret.source_map.map(Into::into)).flatten(),
4244
errors: ctx.take_and_render_reports(),
43-
source_text: transformed_ret.source_text,
44-
source_map: options.sourcemap.then(|| transformed_ret.source_map.map(Into::into)).flatten(),
4545
}
4646
}
4747

napi/transform/src/transformer.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ use oxc_semantic::SemanticBuilder;
77
use oxc_span::SourceType;
88
use oxc_transformer::Transformer;
99

10-
// NOTE: use JSDoc syntax for all doc comments, not rustdoc.
10+
// NOTE: Use JSDoc syntax for all doc comments, not rustdoc.
11+
// NOTE: Types must be aligned with [@types/babel__core](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/babel__core/index.d.ts).
1112

1213
#[napi(object)]
1314
pub struct TransformResult {
1415
/// The transformed code.
1516
///
1617
/// If parsing failed, this will be an empty string.
17-
pub source_text: String,
18+
pub code: String,
1819

1920
/// The source map for the transformed code.
2021
///
2122
/// This will be set if {@link TransformOptions#sourcemap} is `true`.
22-
pub source_map: Option<SourceMap>,
23+
pub map: Option<SourceMap>,
2324

2425
/// The `.d.ts` declaration file for the transformed code. Declarations are
2526
/// only generated if `declaration` is set to `true` and a TypeScript file
@@ -54,7 +55,7 @@ pub struct TransformResult {
5455
///
5556
/// @returns an object containing the transformed code, source maps, and any
5657
/// errors that occurred during parsing or transformation.
57-
#[allow(clippy::needless_pass_by_value, dead_code)]
58+
#[allow(clippy::needless_pass_by_value)]
5859
#[napi]
5960
pub fn transform(
6061
filename: String,
@@ -89,8 +90,8 @@ pub fn transform(
8990
.map_or((None, None), |d| (Some(d.source_text), d.source_map.map(Into::into)));
9091

9192
TransformResult {
92-
source_text: transpile_result.source_text,
93-
source_map: transpile_result.source_map.map(Into::into),
93+
code: transpile_result.source_text,
94+
map: transpile_result.source_map.map(Into::into),
9495
declaration,
9596
declaration_map,
9697
errors: ctx.take_and_render_reports(),

0 commit comments

Comments
 (0)