Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Sep 6, 2024
1 parent 4e63451 commit 4cb7f05
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 34 deletions.
23 changes: 0 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/swc_ast_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ version.workspace = true

[dependencies]
anyhow = "1.0.86"
serde = "1.0.210"
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.128"
wasm-bindgen = "0.2.93"

[dependencies.swc_core]
Expand Down
17 changes: 9 additions & 8 deletions crates/swc_ast_viewer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use serde::Deserialize;
use std::{str::FromStr, sync::Arc};
use swc_core::{
self,
Expand Down Expand Up @@ -44,7 +43,10 @@ fn javascript(fm: &SourceFile, errors: &mut Vec<SWCError>, jsx: bool) -> PResult
)
}

#[derive(Default, PartialEq, Deserialize)]
#[wasm_bindgen(typescript_custom_section)]
const TS_FileType: &'static str = r#"type FileType = "js" | "jsx" | "ts" | "tsx";"#;

#[derive(Default, PartialEq)]
enum FileType {
JavaScript,
JavaScriptReact,
Expand Down Expand Up @@ -75,17 +77,16 @@ impl FromStr for FileType {

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "Config")]
#[wasm_bindgen(typescript_type = "FileType")]
pub type File;
}

#[wasm_bindgen]
pub fn ast(input: &str, file_type: Option<File>) -> Result<String, String> {
let file_type: FileType = if let Some(file_type) = file_type {
serde_wasm_bindgen::from_value(file_type.clone()).map_err(|op| op.to_string())?
} else {
Default::default()
};
let file_type: FileType = file_type
.and_then(|file_type| file_type.as_string())
.and_then(|s| s.parse().ok())
.unwrap_or_default();

let cm: Arc<SourceMap> = Default::default();
let fm = cm.new_source_file(Arc::new(FileName::Anon), input.into());
Expand Down

0 comments on commit 4cb7f05

Please sign in to comment.