Skip to content

Commit

Permalink
Merge pull request #69 from embroider-build/refactor-arguments
Browse files Browse the repository at this point in the history
Refactor options handling
  • Loading branch information
ef4 authored Feb 14, 2024
2 parents ad839b0 + 0af957e commit 8ea2451
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 81 deletions.
23 changes: 1 addition & 22 deletions pkg/node.cjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
const { Preprocessor: WasmPreprocessor } = require("./node/content_tag.cjs");

const defaultOptions = {
inline_source_map: false,
filename: null
};

class Preprocessor {
#preprocessor;

constructor() {
this.#preprocessor = new WasmPreprocessor();
}

process(str, options = {}) {
return this.#preprocessor.process(str, { ...defaultOptions, ...options });
}

parse(str, options = {}) {
return this.#preprocessor.parse(str, { ...defaultOptions, ...options });
}
}
const { Preprocessor } = require("./node/content_tag.cjs");

module.exports.Preprocessor = Preprocessor;
22 changes: 1 addition & 21 deletions pkg/standalone.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import init from "./standalone/content_tag.js";
import { Preprocessor as WasmPreprocessor } from "./standalone/content_tag.js";
export { Preprocessor } from "./standalone/content_tag.js";

await init();

const defaultOptions = {
inline_source_map: false,
filename: null
};

export class Preprocessor {
#preprocessor;

constructor() {
this.#preprocessor = new WasmPreprocessor();
}

process(str, options = {}) {
return this.#preprocessor.process(str, { ...defaultOptions, ...options });
}

parse(str, options = {}) {
return this.#preprocessor.parse(str, { ...defaultOptions, ...options });
}
}
90 changes: 52 additions & 38 deletions src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::{Options, Preprocessor as CorePreprocessor};
use std::{
fmt,
str,
path::PathBuf,
};
use js_sys::Reflect;
use std::{fmt, path::PathBuf, str};
use swc_common::{
errors::Handler,
sync::{Lock, Lrc},
SourceMap, Spanned,
};
use swc_error_reporters::{GraphicalReportHandler, GraphicalTheme, PrettyEmitter};
use wasm_bindgen::prelude::*;
use serde::{Serialize, Deserialize};
use serde_wasm_bindgen::from_value;

#[wasm_bindgen]
extern "C" {
Expand All @@ -21,6 +16,42 @@ extern "C" {

#[wasm_bindgen(js_namespace = JSON, js_name = parse)]
fn json_parse(value: JsValue) -> JsValue;

#[wasm_bindgen(js_name = Boolean)]
fn js_boolean(value: &JsValue) -> bool;

#[wasm_bindgen(js_name = String)]
fn js_string(value: &JsValue) -> String;
}

impl Options {
pub fn new(options: JsValue) -> Self {
if js_boolean(&options) {
// unwrapping here beacuse we already checked truthiness of
// `options`, so the normal case of not passing any options has been
// handled and this will only fail in unusual cases (like a
// Javascript getter throwing)
let option_filename = Reflect::get(&options, &"filename".into()).unwrap();
let filename = if js_boolean(&option_filename) {
Some(PathBuf::from(js_string(&option_filename)))
} else {
None
};

Self {
// unwrap is justified here for the same reasons as commented above
inline_source_map: js_boolean(
&Reflect::get(&options, &"inline_source_map".into()).unwrap(),
),
filename,
}
} else {
Self {
inline_source_map: false,
filename: None,
}
}
}
}

#[wasm_bindgen]
Expand All @@ -32,12 +63,6 @@ pub struct Preprocessor {
// core: Box<CorePreprocessor>,
}

#[derive(Serialize, Deserialize)]
pub struct ProcessOptions {
filename: Option<String>,
inline_source_map: bool,
}

#[derive(Clone, Default)]
struct Writer(Lrc<Lock<String>>);

Expand Down Expand Up @@ -99,17 +124,9 @@ impl Preprocessor {
}

pub fn process(&self, src: String, options: JsValue) -> Result<String, JsValue> {
let options: ProcessOptions = from_value(options)
.map_err(|e| js_error(format!("Options parsing error: {:?}", e).into()))?;

let options = Options::new(options);
let preprocessor = CorePreprocessor::new();
let result = preprocessor.process(
&src,
Options {
filename: options.filename.map(|f| PathBuf::from(f)),
inline_source_map: options.inline_source_map,
},
);
let result = preprocessor.process(&src, options);

match result {
Ok(output) => Ok(output),
Expand All @@ -118,21 +135,18 @@ impl Preprocessor {
}

pub fn parse(&self, src: String, options: JsValue) -> Result<JsValue, JsValue> {
let parse_options: ProcessOptions = from_value(options.clone())
.map_err(|e| js_error(format!("Options parsing error: {:?}", e).into()))?;

let options = Options::new(options);
let preprocessor = CorePreprocessor::new();
let result = preprocessor
.parse(
&src,
Options {
filename: parse_options.filename.map(|f| PathBuf::from(f)),
inline_source_map: parse_options.inline_source_map,
},
)
.map_err(|_err| self.process(src, options).unwrap_err())?;
let serialized = serde_json::to_string(&result)
.map_err(|err| js_error(format!("Unexpected serialization error; please open an issue with the following debug info: {err:#?}").into()))?;
Ok(json_parse(serialized.into()))
let result = preprocessor.parse(&src, options);

match result {
Ok(parsed) => {
match serde_json::to_string(&parsed) {
Ok(serialized) => Ok(json_parse(serialized.into())),
Err(err) => Err(js_error(format!("Unexpected serialization error; please open an issue with the following debug info: {err:#?}").into()))
}
},
Err(err) => Err(as_javascript_error(err, preprocessor.source_map()))
}
}
}

0 comments on commit 8ea2451

Please sign in to comment.