feat(transformer): add transform-typescript boilerplate (#2866)

This commit is contained in:
Boshen 2024-03-30 20:48:35 +08:00 committed by GitHub
parent c1a2958a5a
commit 293b9f482a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 69 additions and 17 deletions

3
Cargo.lock generated
View file

@ -1752,6 +1752,8 @@ dependencies = [
"oxc_tasks_common",
"oxc_transformer",
"pico-args",
"serde",
"serde_json",
"walkdir",
]
@ -1766,6 +1768,7 @@ dependencies = [
"oxc_parser",
"oxc_semantic",
"oxc_span",
"serde",
]
[[package]]

View file

@ -25,6 +25,8 @@ oxc_allocator = { workspace = true }
oxc_semantic = { workspace = true }
oxc_diagnostics = { workspace = true }
serde = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }
oxc_codegen = { workspace = true }

View file

@ -42,7 +42,7 @@ fn main() {
.semantic;
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();
let printed = Codegen::<false>::new("", &source_text, CodegenOptions::default())

View file

@ -5,7 +5,8 @@
//! * <https://babel.dev/docs/presets>
//! * <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_ast::ast::Program;
@ -13,7 +14,13 @@ use oxc_diagnostics::Error;
use oxc_semantic::Semantic;
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)]
pub struct Transformer<'a> {
@ -21,6 +28,8 @@ pub struct Transformer<'a> {
source_type: SourceType,
semantic: Semantic<'a>,
options: TransformOptions,
typescript: TypeScript,
}
impl<'a> Transformer<'a> {
@ -30,7 +39,7 @@ impl<'a> Transformer<'a> {
semantic: Semantic<'a>,
options: TransformOptions,
) -> Self {
Self { allocator, source_type, semantic, options }
Self { allocator, source_type, semantic, options, typescript: TypeScript::default() }
}
/// # Errors

View file

@ -1,3 +0,0 @@
#[allow(clippy::empty_structs_with_brackets)]
#[derive(Debug, Default, Clone)]
pub struct TransformOptions {}

View 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 }
}
}

View file

@ -232,7 +232,7 @@ impl Oxc {
.build_module_record(PathBuf::new(), program)
.build(program)
.semantic;
let options = TransformOptions {};
let options = TransformOptions::default();
let result =
Transformer::new(&allocator, source_type, semantic, options).build(program);
if let Err(errs) = result {

View file

@ -29,6 +29,9 @@ oxc_codegen = { workspace = true }
oxc_transformer = { workspace = true }
oxc_tasks_common = { workspace = true }
oxc_diagnostics = { workspace = true }
walkdir = { workspace = true }
pico-args = { workspace = true }
indexmap = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

View file

@ -10,9 +10,9 @@ use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::{SourceType, VALID_EXTENSIONS};
use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions};
use oxc_transformer::{TransformOptions, Transformer};
// use serde::de::DeserializeOwned;
// use serde_json::Value;
use oxc_transformer::{TransformOptions, Transformer, TypeScriptOptions};
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::{fixture_root, root, TestRunnerEnv};
@ -78,12 +78,16 @@ pub trait TestCase {
fn path(&self) -> &Path;
fn transform_options(&self) -> TransformOptions {
// fn get_options<T: Default + DeserializeOwned>(value: Option<Value>) -> T {
// value.and_then(|v| serde_json::from_value::<T>(v).ok()).unwrap_or_default()
// }
// let options = self.options();
TransformOptions {}
fn get_options<T: Default + DeserializeOwned>(value: Option<Value>) -> T {
value.and_then(|v| serde_json::from_value::<T>(v).ok()).unwrap_or_default()
}
let options = self.options();
TransformOptions {
typescript: options
.get_plugin("transform-typescript")
.map(get_options::<TypeScriptOptions>)
.unwrap_or_default(),
}
}
fn skip_test_case(&self) -> bool {