fix(napi/transform): fix 'typescript.declaration' option not working (#7012)

fixes #7010
This commit is contained in:
Boshen 2024-10-30 03:21:08 +00:00
parent a6fcd812b3
commit d15e408256
3 changed files with 34 additions and 7 deletions

View file

@ -22,3 +22,9 @@ pub struct IsolatedDeclarationsOptions {
pub sourcemap: Option<bool>, 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() }
}
}

View file

@ -6,6 +6,7 @@ use napi_derive::napi;
use oxc::{ use oxc::{
codegen::CodegenReturn, codegen::CodegenReturn,
diagnostics::OxcDiagnostic, diagnostics::OxcDiagnostic,
isolated_declarations::IsolatedDeclarationsOptions,
napi::{ napi::{
source_map::SourceMap, source_map::SourceMap,
transform::{TransformOptions, TransformResult}, transform::{TransformOptions, TransformResult},
@ -20,6 +21,8 @@ use crate::errors::wrap_diagnostics;
#[derive(Default)] #[derive(Default)]
struct Compiler { struct Compiler {
transform_options: oxc::transformer::TransformOptions, transform_options: oxc::transformer::TransformOptions,
isolated_declaration_options: Option<oxc::isolated_declarations::IsolatedDeclarationsOptions>,
sourcemap: bool, sourcemap: bool,
printed: String, printed: String,
@ -37,6 +40,13 @@ struct Compiler {
impl Compiler { impl Compiler {
fn new(options: Option<TransformOptions>) -> Result<Self, Vec<OxcDiagnostic>> { fn new(options: Option<TransformOptions>) -> Result<Self, Vec<OxcDiagnostic>> {
let mut options = options; 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 sourcemap = options.as_ref().and_then(|o| o.sourcemap).unwrap_or_default();
let define = options let define = options
@ -76,8 +86,10 @@ impl Compiler {
let transform_options = let transform_options =
options.map(oxc::transformer::TransformOptions::from).unwrap_or_default(); options.map(oxc::transformer::TransformOptions::from).unwrap_or_default();
Ok(Self { Ok(Self {
transform_options, transform_options,
isolated_declaration_options,
sourcemap, sourcemap,
printed: String::default(), printed: String::default(),
printed_sourcemap: None, printed_sourcemap: None,
@ -103,6 +115,10 @@ impl CompilerInterface for Compiler {
Some(self.transform_options.clone()) Some(self.transform_options.clone())
} }
fn isolated_declaration_options(&self) -> Option<IsolatedDeclarationsOptions> {
self.isolated_declaration_options
}
fn define_options(&self) -> Option<ReplaceGlobalDefinesConfig> { fn define_options(&self) -> Option<ReplaceGlobalDefinesConfig> {
self.define.clone() self.define.clone()
} }
@ -173,6 +189,7 @@ pub fn transform(
} }
} }
}; };
compiler.compile(&source_text, source_type, source_path); compiler.compile(&source_text, source_type, source_path);
TransformResult { TransformResult {

View file

@ -3,26 +3,31 @@ import { assert, describe, it } from 'vitest';
import oxc from './index.js'; import oxc from './index.js';
describe('transform', () => { describe('transform', () => {
const code = 'class A<T> {}'; const code = 'export class A<T> {}';
it('matches output', () => { it('matches output', () => {
const ret = oxc.transform('test.ts', code, { sourcemap: true }); const ret = oxc.transform('test.ts', code, { sourcemap: true });
assert.deepEqual(ret, { assert.deepEqual(ret, {
code: 'class A {}\n', code: 'export class A {}\n',
errors: [], errors: [],
map: { map: {
mappings: 'AAAA,MAAM,EAAK,CAAE', mappings: 'AAAA,OAAO,MAAM,EAAK,CAAE',
names: [], names: [],
sources: ['test.ts'], sources: ['test.ts'],
sourcesContent: ['class A<T> {}'], sourcesContent: ['export class A<T> {}'],
version: 3, version: 3,
}, },
}); });
}); });
it('lang', () => { it('uses the `lang` option', () => {
const ret = oxc.transform('test.vue', code, { lang: 'ts' }); 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');
}); });
}); });
@ -35,7 +40,6 @@ describe('react refresh plugin', () => {
it('matches output', () => { it('matches output', () => {
const ret = oxc.transform('test.tsx', code, { jsx: { refresh: {} } }); const ret = oxc.transform('test.tsx', code, { jsx: { refresh: {} } });
console.log(ret.code);
assert.equal( assert.equal(
ret.code, ret.code,
`import { useState } from "react"; `import { useState } from "react";