Skip to content

Commit

Permalink
fix(napi/transform): fix 'typescript.declaration' option not working
Browse files Browse the repository at this point in the history
fixes #7010
  • Loading branch information
Boshen committed Oct 30, 2024
1 parent a6fcd81 commit 06064c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
6 changes: 6 additions & 0 deletions crates/oxc/src/napi/isolated_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ pub struct IsolatedDeclarationsOptions {

pub sourcemap: Option<bool>,
}

impl From<IsolatedDeclarationsOptions> for oxc_isolated_declarations::IsolatedDeclarationsOptions {
fn from(options: IsolatedDeclarationsOptions) -> Self {
Self { strip_internal: options.strip_internal.unwrap_or_default() }
}
}
17 changes: 17 additions & 0 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use napi_derive::napi;
use oxc::{
codegen::CodegenReturn,
diagnostics::OxcDiagnostic,
isolated_declarations::IsolatedDeclarationsOptions,
napi::{
source_map::SourceMap,
transform::{TransformOptions, TransformResult},
Expand All @@ -20,6 +21,8 @@ use crate::errors::wrap_diagnostics;
#[derive(Default)]
struct Compiler {
transform_options: oxc::transformer::TransformOptions,
isolated_declaration_options: Option<oxc::isolated_declarations::IsolatedDeclarationsOptions>,

sourcemap: bool,

printed: String,
Expand All @@ -37,6 +40,13 @@ struct Compiler {
impl Compiler {
fn new(options: Option<TransformOptions>) -> Result<Self, Vec<OxcDiagnostic>> {
let mut options = options;

let isolated_declaration_options = options
.as_ref()
.and_then(|o| o.typescript.as_ref())
.and_then(|o| o.declaration)
.map(oxc::isolated_declarations::IsolatedDeclarationsOptions::from);

let sourcemap = options.as_ref().and_then(|o| o.sourcemap).unwrap_or_default();

let define = options
Expand Down Expand Up @@ -76,8 +86,10 @@ impl Compiler {

let transform_options =
options.map(oxc::transformer::TransformOptions::from).unwrap_or_default();

Ok(Self {
transform_options,
isolated_declaration_options,
sourcemap,
printed: String::default(),
printed_sourcemap: None,
Expand All @@ -103,6 +115,10 @@ impl CompilerInterface for Compiler {
Some(self.transform_options.clone())
}

fn isolated_declaration_options(&self) -> Option<IsolatedDeclarationsOptions> {
self.isolated_declaration_options
}

fn define_options(&self) -> Option<ReplaceGlobalDefinesConfig> {
self.define.clone()
}
Expand Down Expand Up @@ -173,6 +189,7 @@ pub fn transform(
}
}
};

compiler.compile(&source_text, source_type, source_path);

TransformResult {
Expand Down
18 changes: 11 additions & 7 deletions napi/transform/test/transform.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ import { assert, describe, it } from 'vitest';
import oxc from './index.js';

describe('transform', () => {
const code = 'class A<T> {}';
const code = 'export class A<T> {}';

it('matches output', () => {
const ret = oxc.transform('test.ts', code, { sourcemap: true });
assert.deepEqual(ret, {
code: 'class A {}\n',
code: 'export class A {}\n',
errors: [],
map: {
mappings: 'AAAA,MAAM,EAAK,CAAE',
mappings: 'AAAA,OAAO,MAAM,EAAK,CAAE',
names: [],
sources: ['test.ts'],
sourcesContent: ['class A<T> {}'],
sourcesContent: ['export class A<T> {}'],
version: 3,
},
});
});

it('lang', () => {
it('uses the `lang` option', () => {
const ret = oxc.transform('test.vue', code, { lang: 'ts' });
assert.equal(ret.code, 'class A {}\n');
assert.equal(ret.code, 'export class A {}\n');
});

it('uses the `declaration option`', () => {
const ret = oxc.transform('test.ts', code, { typescript: { declaration: true } });
assert.equal(ret.declaration, 'export declare class A<T> {}\n');
});
});

Expand All @@ -35,7 +40,6 @@ describe('react refresh plugin', () => {

it('matches output', () => {
const ret = oxc.transform('test.tsx', code, { jsx: { refresh: {} } });
console.log(ret.code);
assert.equal(
ret.code,
`import { useState } from "react";
Expand Down

0 comments on commit 06064c9

Please sign in to comment.