From 06064c97cc111790154db3d35b8afcc06114cfc8 Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 30 Oct 2024 11:04:21 +0800 Subject: [PATCH] fix(napi/transform): fix 'typescript.declaration' option not working fixes #7010 --- crates/oxc/src/napi/isolated_declarations.rs | 6 ++++++ napi/transform/src/transformer.rs | 17 +++++++++++++++++ napi/transform/test/transform.test.mjs | 18 +++++++++++------- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/oxc/src/napi/isolated_declarations.rs b/crates/oxc/src/napi/isolated_declarations.rs index db2583e872354..7e57b1c1c928f 100644 --- a/crates/oxc/src/napi/isolated_declarations.rs +++ b/crates/oxc/src/napi/isolated_declarations.rs @@ -22,3 +22,9 @@ pub struct IsolatedDeclarationsOptions { pub sourcemap: Option, } + +impl From for oxc_isolated_declarations::IsolatedDeclarationsOptions { + fn from(options: IsolatedDeclarationsOptions) -> Self { + Self { strip_internal: options.strip_internal.unwrap_or_default() } + } +} diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index 28f194ddf6336..be8f239b0aebc 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -6,6 +6,7 @@ use napi_derive::napi; use oxc::{ codegen::CodegenReturn, diagnostics::OxcDiagnostic, + isolated_declarations::IsolatedDeclarationsOptions, napi::{ source_map::SourceMap, transform::{TransformOptions, TransformResult}, @@ -20,6 +21,8 @@ use crate::errors::wrap_diagnostics; #[derive(Default)] struct Compiler { transform_options: oxc::transformer::TransformOptions, + isolated_declaration_options: Option, + sourcemap: bool, printed: String, @@ -37,6 +40,13 @@ struct Compiler { impl Compiler { fn new(options: Option) -> Result> { 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 @@ -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, @@ -103,6 +115,10 @@ impl CompilerInterface for Compiler { Some(self.transform_options.clone()) } + fn isolated_declaration_options(&self) -> Option { + self.isolated_declaration_options + } + fn define_options(&self) -> Option { self.define.clone() } @@ -173,6 +189,7 @@ pub fn transform( } } }; + compiler.compile(&source_text, source_type, source_path); TransformResult { diff --git a/napi/transform/test/transform.test.mjs b/napi/transform/test/transform.test.mjs index d59cd79c3fe55..7b5e71586193f 100644 --- a/napi/transform/test/transform.test.mjs +++ b/napi/transform/test/transform.test.mjs @@ -3,26 +3,31 @@ import { assert, describe, it } from 'vitest'; import oxc from './index.js'; describe('transform', () => { - const code = 'class A {}'; + const code = 'export class A {}'; 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 {}'], + sourcesContent: ['export class A {}'], 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 {}\n'); }); }); @@ -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";