Skip to content

Commit

Permalink
feat(napi/transform): add TransformerOptions::assumptions (#7601)
Browse files Browse the repository at this point in the history
so that I can use `setPublicClassFields` for #6735 when it's ready.
  • Loading branch information
sapphi-red authored Dec 3, 2024
1 parent 802233d commit 5864352
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/oxc/src/napi/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub struct TransformOptions {
/// @see {@link SourceMap}
pub sourcemap: Option<bool>,

/// Set assumptions in order to produce smaller output.
pub assumptions: Option<CompilerAssumptions>,

/// Configure how TypeScript is transformed.
pub typescript: Option<TypeScriptOptions>,

Expand Down Expand Up @@ -114,6 +117,7 @@ impl TryFrom<TransformOptions> for oxc_transformer::TransformOptions {
};
Ok(Self {
cwd: options.cwd.map(PathBuf::from).unwrap_or_default(),
assumptions: options.assumptions.map(Into::into).unwrap_or_default(),
typescript: options
.typescript
.map(oxc_transformer::TypeScriptOptions::from)
Expand All @@ -125,6 +129,36 @@ impl TryFrom<TransformOptions> for oxc_transformer::TransformOptions {
}
}

#[napi(object)]
#[derive(Default, Debug)]
pub struct CompilerAssumptions {
pub ignore_function_length: Option<bool>,
pub no_document_all: Option<bool>,
pub object_rest_no_symbols: Option<bool>,
pub pure_getters: Option<bool>,
pub set_public_class_fields: Option<bool>,
}

impl From<CompilerAssumptions> for oxc_transformer::CompilerAssumptions {
fn from(value: CompilerAssumptions) -> Self {
let ops = oxc_transformer::CompilerAssumptions::default();
Self {
ignore_function_length: value
.ignore_function_length
.unwrap_or(ops.ignore_function_length),
no_document_all: value.no_document_all.unwrap_or(ops.no_document_all),
object_rest_no_symbols: value
.object_rest_no_symbols
.unwrap_or(ops.object_rest_no_symbols),
pure_getters: value.pure_getters.unwrap_or(ops.pure_getters),
set_public_class_fields: value
.set_public_class_fields
.unwrap_or(ops.set_public_class_fields),
..ops
}
}
}

#[napi(object)]
#[derive(Default)]
pub struct TypeScriptOptions {
Expand Down
10 changes: 10 additions & 0 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export interface ArrowFunctionsOptions {
spec?: boolean
}

export interface CompilerAssumptions {
ignoreFunctionLength?: boolean
noDocumentAll?: boolean
objectRestNoSymbols?: boolean
pureGetters?: boolean
setPublicClassFields?: boolean
}

export interface Es2015Options {
/** Transform arrow functions into function expressions. */
arrowFunction?: ArrowFunctionsOptions
Expand Down Expand Up @@ -199,6 +207,8 @@ export interface TransformOptions {
* @see {@link SourceMap}
*/
sourcemap?: boolean
/** Set assumptions in order to produce smaller output. */
assumptions?: CompilerAssumptions
/** Configure how TypeScript is transformed. */
typescript?: TypeScriptOptions
/** Configure how TSX and JSX are transformed. */
Expand Down

0 comments on commit 5864352

Please sign in to comment.