mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
fix(napi/transform): fix 'typescript.declaration' option not working (#7012)
fixes #7010
This commit is contained in:
parent
a6fcd812b3
commit
d15e408256
3 changed files with 34 additions and 7 deletions
|
|
@ -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() }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<oxc::isolated_declarations::IsolatedDeclarationsOptions>,
|
||||
|
||||
sourcemap: bool,
|
||||
|
||||
printed: String,
|
||||
|
|
@ -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
|
||||
|
|
@ -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<IsolatedDeclarationsOptions> {
|
||||
self.isolated_declaration_options
|
||||
}
|
||||
|
||||
fn define_options(&self) -> Option<ReplaceGlobalDefinesConfig> {
|
||||
self.define.clone()
|
||||
}
|
||||
|
|
@ -173,6 +189,7 @@ pub fn transform(
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
compiler.compile(&source_text, source_type, source_path);
|
||||
|
||||
TransformResult {
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Reference in a new issue