feat(napi/transform)!: align output API sourceText -> code with babel (#5398)

And `sourceMap` -> `map`

closes #5397
This commit is contained in:
Boshen 2024-09-02 07:53:46 +00:00
parent 3cb9452338
commit b1d0075359
3 changed files with 17 additions and 16 deletions

View file

@ -18,16 +18,16 @@ export interface Es2015BindingOptions {
} }
/** TypeScript Isolated Declarations for Standalone DTS Emit */ /** TypeScript Isolated Declarations for Standalone DTS Emit */
export declare function isolatedDeclaration(filename: string, sourceText: string, options: IsolatedDeclarationsOptions): IsolatedDeclarationsResult function isolatedDeclaration(filename: string, sourceText: string, options: IsolatedDeclarationsOptions): IsolatedDeclarationsResult
export interface IsolatedDeclarationsOptions { export interface IsolatedDeclarationsOptions {
sourcemap: boolean sourcemap: boolean
} }
export interface IsolatedDeclarationsResult { export interface IsolatedDeclarationsResult {
code: string
map?: SourceMap
errors: Array<string> errors: Array<string>
sourceText: string
sourceMap?: SourceMap
} }
/** /**
@ -136,7 +136,7 @@ export interface SourceMap {
* @returns an object containing the transformed code, source maps, and any * @returns an object containing the transformed code, source maps, and any
* errors that occurred during parsing or transformation. * errors that occurred during parsing or transformation.
*/ */
export declare function transform(filename: string, sourceText: string, options?: TransformOptions | undefined | null): TransformResult function transform(filename: string, sourceText: string, options?: TransformOptions | undefined | null): TransformResult
/** /**
* Options for transforming a JavaScript or TypeScript file. * Options for transforming a JavaScript or TypeScript file.
@ -180,13 +180,13 @@ export interface TransformResult {
* *
* If parsing failed, this will be an empty string. * If parsing failed, this will be an empty string.
*/ */
sourceText: string code: string
/** /**
* The source map for the transformed code. * The source map for the transformed code.
* *
* This will be set if {@link TransformOptions#sourcemap} is `true`. * This will be set if {@link TransformOptions#sourcemap} is `true`.
*/ */
sourceMap?: SourceMap map?: SourceMap
/** /**
* The `.d.ts` declaration file for the transformed code. Declarations are * The `.d.ts` declaration file for the transformed code. Declarations are
* only generated if `declaration` is set to `true` and a TypeScript file * only generated if `declaration` is set to `true` and a TypeScript file

View file

@ -9,9 +9,9 @@ use crate::{context::TransformContext, SourceMap, TransformOptions};
#[napi(object)] #[napi(object)]
pub struct IsolatedDeclarationsResult { pub struct IsolatedDeclarationsResult {
pub code: String,
pub map: Option<SourceMap>,
pub errors: Vec<String>, pub errors: Vec<String>,
pub source_text: String,
pub source_map: Option<SourceMap>,
} }
#[napi(object)] #[napi(object)]
@ -39,9 +39,9 @@ pub fn isolated_declaration(
let transformed_ret = build_declarations(&ctx); let transformed_ret = build_declarations(&ctx);
IsolatedDeclarationsResult { IsolatedDeclarationsResult {
code: transformed_ret.source_text,
map: options.sourcemap.then(|| transformed_ret.source_map.map(Into::into)).flatten(),
errors: ctx.take_and_render_reports(), 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

@ -7,19 +7,20 @@ use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType; use oxc_span::SourceType;
use oxc_transformer::Transformer; use oxc_transformer::Transformer;
// NOTE: use JSDoc syntax for all doc comments, not rustdoc. // NOTE: Use JSDoc syntax for all doc comments, not rustdoc.
// NOTE: Types must be aligned with [@types/babel__core](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/babel__core/index.d.ts).
#[napi(object)] #[napi(object)]
pub struct TransformResult { pub struct TransformResult {
/// The transformed code. /// The transformed code.
/// ///
/// If parsing failed, this will be an empty string. /// If parsing failed, this will be an empty string.
pub source_text: String, pub code: String,
/// The source map for the transformed code. /// The source map for the transformed code.
/// ///
/// This will be set if {@link TransformOptions#sourcemap} is `true`. /// This will be set if {@link TransformOptions#sourcemap} is `true`.
pub source_map: Option<SourceMap>, pub map: Option<SourceMap>,
/// The `.d.ts` declaration file for the transformed code. Declarations are /// The `.d.ts` declaration file for the transformed code. Declarations are
/// only generated if `declaration` is set to `true` and a TypeScript file /// only generated if `declaration` is set to `true` and a TypeScript file
@ -54,7 +55,7 @@ pub struct TransformResult {
/// ///
/// @returns an object containing the transformed code, source maps, and any /// @returns an object containing the transformed code, source maps, and any
/// errors that occurred during parsing or transformation. /// errors that occurred during parsing or transformation.
#[allow(clippy::needless_pass_by_value, dead_code)] #[allow(clippy::needless_pass_by_value)]
#[napi] #[napi]
pub fn transform( pub fn transform(
filename: String, filename: String,
@ -89,8 +90,8 @@ pub fn transform(
.map_or((None, None), |d| (Some(d.source_text), d.source_map.map(Into::into))); .map_or((None, None), |d| (Some(d.source_text), d.source_map.map(Into::into)));
TransformResult { TransformResult {
source_text: transpile_result.source_text, code: transpile_result.source_text,
source_map: transpile_result.source_map.map(Into::into), map: transpile_result.source_map.map(Into::into),
declaration, declaration,
declaration_map, declaration_map,
errors: ctx.take_and_render_reports(), errors: ctx.take_and_render_reports(),