Skip to content

Commit

Permalink
feat(wasm): add transformer target API (#7513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 28, 2024
1 parent 85d4c93 commit f955ba2
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 68 deletions.
2 changes: 0 additions & 2 deletions crates/oxc_wasm/.cargo/config.toml

This file was deleted.

8 changes: 4 additions & 4 deletions crates/oxc_wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "oxc_wasm",
"name": "@oxc/oxc_wasm",
"type": "module",
"collaborators": [
"Boshen <boshenc@gmail.com>",
Expand All @@ -12,9 +12,6 @@
"oxc_wasm.js",
"oxc_wasm.d.ts"
],
"dependencies": {
"@oxc-project/types": "workspace:^"
},
"main": "oxc_wasm.js",
"types": "oxc_wasm.d.ts",
"sideEffects": [
Expand All @@ -29,5 +26,8 @@
],
"scripts": {
"check": "tsc --lib es2020,dom ./oxc_wasm.d.ts"
},
"dependencies": {
"@oxc-project/types": "workspace:^"
}
}
60 changes: 37 additions & 23 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use oxc::{
allocator::Allocator,
ast::{ast::Program, Comment as OxcComment, CommentKind, Visit},
codegen::{CodeGenerator, CodegenOptions},
diagnostics::Error,
minifier::{CompressOptions, Minifier, MinifierOptions},
parser::{ParseOptions, Parser, ParserReturn},
semantic::{
Expand Down Expand Up @@ -71,9 +70,11 @@ pub struct Oxc {
#[wasm_bindgen(readonly, skip_typescript, js_name = "prettierIrText")]
pub prettier_ir_text: String,

#[serde(skip)]
comments: Vec<Comment>,

diagnostics: RefCell<Vec<Error>>,
#[serde(skip)]
diagnostics: RefCell<Vec<oxc::diagnostics::OxcDiagnostic>>,

#[serde(skip)]
serializer: serde_wasm_bindgen::Serializer,
Expand Down Expand Up @@ -120,21 +121,24 @@ impl Oxc {
.diagnostics
.borrow()
.iter()
.flat_map(|error| {
let Some(labels) = error.labels() else { return vec![] };
labels
.map(|label| {
OxcDiagnostic {
start: label.offset(),
end: label.offset() + label.len(),
severity: format!("{:?}", error.severity().unwrap_or_default()),
message: format!("{error}"),
}
.serialize(&self.serializer)
.unwrap()
.flat_map(|error| match &error.labels {
Some(labels) => labels
.iter()
.map(|label| OxcDiagnostic {
start: label.offset(),
end: label.offset() + label.len(),
severity: format!("{:?}", error.severity),
message: format!("{error}"),
})
.collect::<Vec<_>>()
.collect::<Vec<_>>(),
None => vec![OxcDiagnostic {
start: 0,
end: 0,
severity: format!("{:?}", error.severity),
message: format!("{error}"),
}],
})
.map(|v| v.serialize(&self.serializer).unwrap())
.collect::<Vec<_>>())
}

Expand Down Expand Up @@ -169,7 +173,7 @@ impl Oxc {
let _linter_options = linter_options.unwrap_or_default();
let minifier_options = minifier_options.unwrap_or_default();
let _codegen_options = codegen_options.unwrap_or_default();
let _transform_options = transform_options.unwrap_or_default();
let transform_options = transform_options.unwrap_or_default();
let control_flow_options = control_flow_options.unwrap_or_default();

let allocator = Allocator::default();
Expand Down Expand Up @@ -222,7 +226,7 @@ impl Oxc {
});
if run_options.syntax.unwrap_or_default() {
self.save_diagnostics(
errors.into_iter().chain(semantic_ret.errors).map(Error::from).collect::<Vec<_>>(),
errors.into_iter().chain(semantic_ret.errors).collect::<Vec<_>>(),
);
}

Expand All @@ -242,13 +246,23 @@ impl Oxc {
}

if run_options.transform.unwrap_or_default() {
let options = TransformOptions::enable_all();
let options = transform_options
.target
.as_ref()
.and_then(|target| {
TransformOptions::from_target(target)
.map_err(|err| {
self.save_diagnostics(vec![oxc::diagnostics::OxcDiagnostic::error(
err,
)]);
})
.ok()
})
.unwrap_or_default();
let result = Transformer::new(&allocator, &path, &options)
.build_with_symbols_and_scopes(symbols, scopes, &mut program);
if !result.errors.is_empty() {
self.save_diagnostics(
result.errors.into_iter().map(Error::from).collect::<Vec<_>>(),
);
self.save_diagnostics(result.errors.into_iter().collect::<Vec<_>>());
}
}

Expand Down Expand Up @@ -294,7 +308,7 @@ impl Oxc {
.build(program);
let semantic = Rc::new(semantic_ret.semantic);
let linter_ret = Linter::default().run(path, Rc::clone(&semantic));
let diagnostics = linter_ret.into_iter().map(|e| Error::from(e.error)).collect();
let diagnostics = linter_ret.into_iter().map(|e| e.error).collect();
self.save_diagnostics(diagnostics);
}
}
Expand Down Expand Up @@ -389,7 +403,7 @@ impl Oxc {
writer.scope_text
}

fn save_diagnostics(&self, diagnostics: Vec<Error>) {
fn save_diagnostics(&self, diagnostics: Vec<oxc::diagnostics::OxcDiagnostic>) {
self.diagnostics.borrow_mut().extend(diagnostics);
}

Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_wasm/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ pub struct OxcLinterOptions {}
#[derive(Debug, Default, Clone, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
#[serde(rename_all = "camelCase")]
// allow empty object for future compatibility
#[allow(clippy::empty_structs_with_brackets)]
pub struct OxcTransformerOptions {}
pub struct OxcTransformerOptions {
#[tsify(optional)]
pub target: Option<String>,
}

#[derive(Debug, Default, Clone, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ watch-wasm:
just watch 'just build-wasm dev'

build-wasm mode="release":
wasm-pack build --out-dir ../../npm/oxc-wasm --target web --{{mode}} --scope oxc crates/oxc_wasm
echo '*.wasm' > npm/oxc-wasm/.gitignore
wasm-pack build crates/oxc_wasm --mode no-install --no-pack --target web --scope oxc --out-dir ../../npm/oxc-wasm --{{mode}}
cp crates/oxc_wasm/package.json npm/oxc-wasm/package.json
rm npm/oxc-wasm/.gitignore

# Generate the JavaScript global variables. See `tasks/javascript_globals`
javascript-globals:
Expand Down
1 change: 0 additions & 1 deletion npm/oxc-wasm/.gitignore

This file was deleted.

58 changes: 29 additions & 29 deletions npm/oxc-wasm/oxc_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
* @returns {any}
*/
export function browserslist(query: string, opts: any): any;

import type { Program, Span } from "@oxc-project/types";
export * from "@oxc-project/types";


export interface Oxc {
ast: Program;
ir: string;
controlFlowGraph: string;
symbols: SymbolTable;
scopeText: string;
codegenText: string;
formattedText: string;
prettierFormattedText: string;
prettierIrText: string;
}

export interface Comment {
type: CommentType;
value: string;
start: number;
end: number;
}

export type CommentType = "Line" | "Block";

export interface OxcOptions {
run?: OxcRunOptions;
parser?: OxcParserOptions;
Expand Down Expand Up @@ -37,7 +63,9 @@ export interface OxcParserOptions {

export interface OxcLinterOptions {}

export interface OxcTransformerOptions {}
export interface OxcTransformerOptions {
target?: string;
}

export interface OxcCodegenOptions {
indentation?: number;
Expand Down Expand Up @@ -66,34 +94,6 @@ export interface OxcCompressOptions {
}


import type { Program, Span } from "@oxc-project/types";
export * from "@oxc-project/types";


export interface Oxc {
ast: Program;
ir: string;
controlFlowGraph: string;
symbols: SymbolTable;
scopeText: string;
codegenText: string;
formattedText: string;
prettierFormattedText: string;
prettierIrText: string;
comments: Comment[];
diagnostics: Error[];
}

export interface Comment {
type: CommentType;
value: string;
start: number;
end: number;
}

export type CommentType = "Line" | "Block";


export type IndexVec<I, T> = Array<T>;
export type CompactStr = string;

Expand Down
8 changes: 4 additions & 4 deletions npm/oxc-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "oxc_wasm",
"name": "@oxc/oxc_wasm",
"type": "module",
"collaborators": [
"Boshen <boshenc@gmail.com>",
Expand All @@ -12,9 +12,6 @@
"oxc_wasm.js",
"oxc_wasm.d.ts"
],
"dependencies": {
"@oxc-project/types": "workspace:^"
},
"main": "oxc_wasm.js",
"types": "oxc_wasm.d.ts",
"sideEffects": [
Expand All @@ -29,5 +26,8 @@
],
"scripts": {
"check": "tsc --lib es2020,dom ./oxc_wasm.d.ts"
},
"dependencies": {
"@oxc-project/types": "workspace:^"
}
}

0 comments on commit f955ba2

Please sign in to comment.