mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transformer): add transform-typescript boilerplate (#2866)
This commit is contained in:
parent
c1a2958a5a
commit
293b9f482a
9 changed files with 69 additions and 17 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -1752,6 +1752,8 @@ dependencies = [
|
||||||
"oxc_tasks_common",
|
"oxc_tasks_common",
|
||||||
"oxc_transformer",
|
"oxc_transformer",
|
||||||
"pico-args",
|
"pico-args",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"walkdir",
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -1766,6 +1768,7 @@ dependencies = [
|
||||||
"oxc_parser",
|
"oxc_parser",
|
||||||
"oxc_semantic",
|
"oxc_semantic",
|
||||||
"oxc_span",
|
"oxc_span",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ oxc_allocator = { workspace = true }
|
||||||
oxc_semantic = { workspace = true }
|
oxc_semantic = { workspace = true }
|
||||||
oxc_diagnostics = { workspace = true }
|
oxc_diagnostics = { workspace = true }
|
||||||
|
|
||||||
|
serde = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
oxc_parser = { workspace = true }
|
oxc_parser = { workspace = true }
|
||||||
oxc_codegen = { workspace = true }
|
oxc_codegen = { workspace = true }
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ fn main() {
|
||||||
.semantic;
|
.semantic;
|
||||||
|
|
||||||
let program = allocator.alloc(ret.program);
|
let program = allocator.alloc(ret.program);
|
||||||
let transform_options = TransformOptions {};
|
let transform_options = TransformOptions::default();
|
||||||
Transformer::new(&allocator, source_type, semantic, transform_options).build(program).unwrap();
|
Transformer::new(&allocator, source_type, semantic, transform_options).build(program).unwrap();
|
||||||
|
|
||||||
let printed = Codegen::<false>::new("", &source_text, CodegenOptions::default())
|
let printed = Codegen::<false>::new("", &source_text, CodegenOptions::default())
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@
|
||||||
//! * <https://babel.dev/docs/presets>
|
//! * <https://babel.dev/docs/presets>
|
||||||
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts>
|
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts>
|
||||||
|
|
||||||
mod options;
|
// Plugins: <https://babeljs.io/docs/plugins-list>
|
||||||
|
mod typescript;
|
||||||
|
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_ast::ast::Program;
|
use oxc_ast::ast::Program;
|
||||||
|
|
@ -13,7 +14,13 @@ use oxc_diagnostics::Error;
|
||||||
use oxc_semantic::Semantic;
|
use oxc_semantic::Semantic;
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
|
|
||||||
pub use crate::options::TransformOptions;
|
pub use crate::typescript::{TypeScript, TypeScriptOptions};
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct TransformOptions {
|
||||||
|
#[allow(unused)]
|
||||||
|
pub typescript: TypeScriptOptions,
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Transformer<'a> {
|
pub struct Transformer<'a> {
|
||||||
|
|
@ -21,6 +28,8 @@ pub struct Transformer<'a> {
|
||||||
source_type: SourceType,
|
source_type: SourceType,
|
||||||
semantic: Semantic<'a>,
|
semantic: Semantic<'a>,
|
||||||
options: TransformOptions,
|
options: TransformOptions,
|
||||||
|
|
||||||
|
typescript: TypeScript,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Transformer<'a> {
|
impl<'a> Transformer<'a> {
|
||||||
|
|
@ -30,7 +39,7 @@ impl<'a> Transformer<'a> {
|
||||||
semantic: Semantic<'a>,
|
semantic: Semantic<'a>,
|
||||||
options: TransformOptions,
|
options: TransformOptions,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self { allocator, source_type, semantic, options }
|
Self { allocator, source_type, semantic, options, typescript: TypeScript::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Errors
|
/// # Errors
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
#[allow(clippy::empty_structs_with_brackets)]
|
|
||||||
#[derive(Debug, Default, Clone)]
|
|
||||||
pub struct TransformOptions {}
|
|
||||||
34
crates/oxc_transformer/src/typescript/mod.rs
Normal file
34
crates/oxc_transformer/src/typescript/mod.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
//! <https://babeljs.io/docs/babel-plugin-transform-typescript>
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct TypeScriptOptions;
|
||||||
|
|
||||||
|
/// This plugin adds support for the types syntax used by the TypeScript programming language.
|
||||||
|
/// However, this plugin does not add the ability to type-check the JavaScript passed to it.
|
||||||
|
/// For that, you will need to install and set up TypeScript.
|
||||||
|
///
|
||||||
|
/// Note that although the TypeScript compiler tsc actively supports certain JavaScript proposals such as optional chaining (?.),
|
||||||
|
/// nullish coalescing (??) and class properties (this.#x), this preset does not include these features
|
||||||
|
/// because they are not the types syntax available in TypeScript only.
|
||||||
|
/// We recommend using preset-env with preset-typescript if you want to transpile these features.
|
||||||
|
///
|
||||||
|
/// This plugin is included in `preset-typescript`.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// In: `const x: number = 0;`
|
||||||
|
/// Out: `const x = 0;`
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct TypeScript {
|
||||||
|
#[allow(unused)]
|
||||||
|
options: TypeScriptOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TypeScript {
|
||||||
|
pub fn new(options: TypeScriptOptions) -> Self {
|
||||||
|
Self { options }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -232,7 +232,7 @@ impl Oxc {
|
||||||
.build_module_record(PathBuf::new(), program)
|
.build_module_record(PathBuf::new(), program)
|
||||||
.build(program)
|
.build(program)
|
||||||
.semantic;
|
.semantic;
|
||||||
let options = TransformOptions {};
|
let options = TransformOptions::default();
|
||||||
let result =
|
let result =
|
||||||
Transformer::new(&allocator, source_type, semantic, options).build(program);
|
Transformer::new(&allocator, source_type, semantic, options).build(program);
|
||||||
if let Err(errs) = result {
|
if let Err(errs) = result {
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@ oxc_codegen = { workspace = true }
|
||||||
oxc_transformer = { workspace = true }
|
oxc_transformer = { workspace = true }
|
||||||
oxc_tasks_common = { workspace = true }
|
oxc_tasks_common = { workspace = true }
|
||||||
oxc_diagnostics = { workspace = true }
|
oxc_diagnostics = { workspace = true }
|
||||||
|
|
||||||
walkdir = { workspace = true }
|
walkdir = { workspace = true }
|
||||||
pico-args = { workspace = true }
|
pico-args = { workspace = true }
|
||||||
indexmap = { workspace = true }
|
indexmap = { workspace = true }
|
||||||
|
serde = { workspace = true }
|
||||||
|
serde_json = { workspace = true }
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,9 @@ use oxc_parser::Parser;
|
||||||
use oxc_semantic::SemanticBuilder;
|
use oxc_semantic::SemanticBuilder;
|
||||||
use oxc_span::{SourceType, VALID_EXTENSIONS};
|
use oxc_span::{SourceType, VALID_EXTENSIONS};
|
||||||
use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions};
|
use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions};
|
||||||
use oxc_transformer::{TransformOptions, Transformer};
|
use oxc_transformer::{TransformOptions, Transformer, TypeScriptOptions};
|
||||||
// use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
// use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::{fixture_root, root, TestRunnerEnv};
|
use crate::{fixture_root, root, TestRunnerEnv};
|
||||||
|
|
||||||
|
|
@ -78,12 +78,16 @@ pub trait TestCase {
|
||||||
fn path(&self) -> &Path;
|
fn path(&self) -> &Path;
|
||||||
|
|
||||||
fn transform_options(&self) -> TransformOptions {
|
fn transform_options(&self) -> TransformOptions {
|
||||||
// fn get_options<T: Default + DeserializeOwned>(value: Option<Value>) -> T {
|
fn get_options<T: Default + DeserializeOwned>(value: Option<Value>) -> T {
|
||||||
// value.and_then(|v| serde_json::from_value::<T>(v).ok()).unwrap_or_default()
|
value.and_then(|v| serde_json::from_value::<T>(v).ok()).unwrap_or_default()
|
||||||
// }
|
}
|
||||||
|
let options = self.options();
|
||||||
// let options = self.options();
|
TransformOptions {
|
||||||
TransformOptions {}
|
typescript: options
|
||||||
|
.get_plugin("transform-typescript")
|
||||||
|
.map(get_options::<TypeScriptOptions>)
|
||||||
|
.unwrap_or_default(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn skip_test_case(&self) -> bool {
|
fn skip_test_case(&self) -> bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue