Skip to content

Commit

Permalink
chore(napi): make transformer options optional (#3989)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Jul 1, 2024
1 parent 253eea1 commit 0b56d50
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
38 changes: 23 additions & 15 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@
/* auto-generated by NAPI-RS */

export interface TypeScriptBindingOptions {
jsxPragma: string
jsxPragmaFrag: string
onlyRemoveTypeImports: boolean
allowNamespaces: boolean
allowDeclareFields: boolean
jsxPragma?: string
jsxPragmaFrag?: string
onlyRemoveTypeImports?: boolean
allowNamespaces?: boolean
allowDeclareFields?: boolean
}
export interface ReactBindingOptions {
runtime: 'classic' | 'automatic'
development: boolean
throwIfNamespace: boolean
pure: boolean
runtime?: 'classic' | 'automatic'
development?: boolean
throwIfNamespace?: boolean
pure?: boolean
importSource?: string
pragma?: string
pragmaFrag?: string
useBuiltIns?: boolean
useSpread?: boolean
}
export interface ArrowFunctionsBindingOptions {
spec: boolean
spec?: boolean
}
export interface Es2015BindingOptions {
arrowFunction?: ArrowFunctionsBindingOptions
}
export interface TransformBindingOptions {
typescript: TypeScriptBindingOptions
react: ReactBindingOptions
es2015: Es2015BindingOptions
typescript?: TypeScriptBindingOptions
react?: ReactBindingOptions
es2015?: Es2015BindingOptions
/**
* Enable Sourcemap
*
* * `true` to generate a sourcemap for the code and include it in the result object.
*
* Default: false
*/
sourcemap?: boolean
}
export interface Sourcemap {
file?: string
Expand All @@ -45,10 +53,10 @@ export interface TransformResult {
map?: Sourcemap
errors: Array<string>
}
export function transform(filename: string, sourceText: string, options: TransformBindingOptions): TransformResult
function transform(filename: string, sourceText: string, options?: TransformBindingOptions | undefined | null): TransformResult
export interface IsolatedDeclarationsResult {
sourceText: string
errors: Array<string>
}
/** TypeScript Isolated Declarations for Standalone DTS Emit */
export function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
2 changes: 1 addition & 1 deletion napi/transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod transformer;
pub mod transformer;

use napi_derive::napi;

Expand Down
71 changes: 37 additions & 34 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,35 @@ use oxc_transformer::{

#[napi(object)]
pub struct TypeScriptBindingOptions {
pub jsx_pragma: String,
pub jsx_pragma_frag: String,
pub only_remove_type_imports: bool,
pub allow_namespaces: bool,
pub allow_declare_fields: bool,
pub jsx_pragma: Option<String>,
pub jsx_pragma_frag: Option<String>,
pub only_remove_type_imports: Option<bool>,
pub allow_namespaces: Option<bool>,
pub allow_declare_fields: Option<bool>,
}

impl From<TypeScriptBindingOptions> for TypeScriptOptions {
fn from(options: TypeScriptBindingOptions) -> Self {
let ops = TypeScriptOptions::default();
TypeScriptOptions {
jsx_pragma: options.jsx_pragma.into(),
jsx_pragma_frag: options.jsx_pragma_frag.into(),
only_remove_type_imports: options.only_remove_type_imports,
allow_namespaces: options.allow_namespaces,
allow_declare_fields: options.allow_declare_fields,
jsx_pragma: options.jsx_pragma.map(Into::into).unwrap_or(ops.jsx_pragma),
jsx_pragma_frag: options.jsx_pragma_frag.map(Into::into).unwrap_or(ops.jsx_pragma_frag),
only_remove_type_imports: options
.only_remove_type_imports
.unwrap_or(ops.only_remove_type_imports),
allow_namespaces: options.allow_namespaces.unwrap_or(ops.allow_namespaces),
allow_declare_fields: options.allow_declare_fields.unwrap_or(ops.allow_declare_fields),
}
}
}

#[napi(object)]
pub struct ReactBindingOptions {
#[napi(ts_type = "'classic' | 'automatic'")]
pub runtime: String,
pub development: bool,
pub throw_if_namespace: bool,
pub pure: bool,
pub runtime: Option<String>,
pub development: Option<bool>,
pub throw_if_namespace: Option<bool>,
pub pure: Option<bool>,
pub import_source: Option<String>,
pub pragma: Option<String>,
pub pragma_frag: Option<String>,
Expand All @@ -47,14 +50,15 @@ pub struct ReactBindingOptions {

impl From<ReactBindingOptions> for ReactOptions {
fn from(options: ReactBindingOptions) -> Self {
let ops = ReactOptions::default();
ReactOptions {
runtime: match options.runtime.as_str() {
"classic" => ReactJsxRuntime::Classic,
runtime: match options.runtime.as_deref() {
Some("classic") => ReactJsxRuntime::Classic,
/* "automatic" */ _ => ReactJsxRuntime::Automatic,
},
development: options.development,
throw_if_namespace: options.throw_if_namespace,
pure: options.pure,
development: options.development.unwrap_or(ops.development),
throw_if_namespace: options.throw_if_namespace.unwrap_or(ops.throw_if_namespace),
pure: options.pure.unwrap_or(ops.pure),
import_source: options.import_source,
pragma: options.pragma,
pragma_frag: options.pragma_frag,
Expand All @@ -67,12 +71,12 @@ impl From<ReactBindingOptions> for ReactOptions {

#[napi(object)]
pub struct ArrowFunctionsBindingOptions {
pub spec: bool,
pub spec: Option<bool>,
}

impl From<ArrowFunctionsBindingOptions> for ArrowFunctionsOptions {
fn from(options: ArrowFunctionsBindingOptions) -> Self {
ArrowFunctionsOptions { spec: options.spec }
ArrowFunctionsOptions { spec: options.spec.unwrap_or_default() }
}
}

Expand All @@ -89,23 +93,23 @@ impl From<ES2015BindingOptions> for ES2015Options {

#[napi(object)]
pub struct TransformBindingOptions {
pub typescript: TypeScriptBindingOptions,
pub react: ReactBindingOptions,
pub es2015: ES2015BindingOptions,
/// Enable Sourcemaps
pub typescript: Option<TypeScriptBindingOptions>,
pub react: Option<ReactBindingOptions>,
pub es2015: Option<ES2015BindingOptions>,
/// Enable Sourcemap
///
/// * `true` to generate a sourcemap for the code and include it in the result object.
///
/// Default: false
pub sourcemaps: bool,
pub sourcemap: Option<bool>,
}

impl From<TransformBindingOptions> for TransformOptions {
fn from(options: TransformBindingOptions) -> Self {
TransformOptions {
typescript: options.typescript.into(),
react: options.react.into(),
es2015: options.es2015.into(),
typescript: options.typescript.map(Into::into).unwrap_or_default(),
react: options.react.map(Into::into).unwrap_or_default(),
es2015: options.es2015.map(Into::into).unwrap_or_default(),
..TransformOptions::default()
}
}
Expand All @@ -124,7 +128,6 @@ pub struct Sourcemap {
#[napi(object)]
pub struct TransformResult {
pub source_text: String,
/// Sourcemap
pub map: Option<Sourcemap>,
pub errors: Vec<String>,
}
Expand All @@ -134,9 +137,9 @@ pub struct TransformResult {
pub fn transform(
filename: String,
source_text: String,
options: TransformBindingOptions,
options: Option<TransformBindingOptions>,
) -> TransformResult {
let sourcemaps = options.sourcemaps;
let sourcemap = options.as_ref().is_some_and(|x| x.sourcemap.unwrap_or_default());
let mut errors = vec![];

let source_path = Path::new(&filename);
Expand All @@ -148,7 +151,7 @@ pub fn transform(
}

let mut program = parser_ret.program;
let transform_options = TransformOptions::from(options);
let transform_options = options.map(Into::into).unwrap_or_default();
let ret = Transformer::new(
&allocator,
source_path,
Expand All @@ -164,7 +167,7 @@ pub fn transform(
}

let mut codegen = CodeGenerator::new();
if sourcemaps {
if sourcemap {
codegen = codegen.enable_source_map(source_path.to_string_lossy().as_ref(), &source_text);
}
let ret = codegen.build(&program);
Expand Down

0 comments on commit 0b56d50

Please sign in to comment.