Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings): Apply resolver to the output of parse apis #6118

Merged
merged 9 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions bindings/binding_core_node/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use swc_core::{
Compiler,
},
common::{comments::Comments, FileName},
ecma::{transforms::base::resolver, visit::VisitMutWith},
node::{deserialize_json, get_deserialized, MapErr},
};

Expand Down Expand Up @@ -53,14 +54,18 @@ impl Task for ParseTask {
};

let program = try_with(self.c.cm.clone(), false, ErrorFormat::Normal, |handler| {
self.c.parse_js(
let mut p = self.c.parse_js(
fm,
handler,
options.target,
options.syntax,
options.is_module,
comments,
)
)?;

p.visit_mut_with(&mut resolver(Mark::new(), Mark::new()));

Ok(p)
})
.convert_err()?;

Expand Down Expand Up @@ -97,14 +102,18 @@ impl Task for ParseFileTask {
None
};

self.c.parse_js(
let mut p = self.c.parse_js(
fm,
handler,
options.target,
options.syntax,
options.is_module,
comments,
)
);

p.visit_mut_with(&mut resolver(Mark::new(), Mark::new()));

p
})
})
.convert_err()?;
Expand Down Expand Up @@ -169,14 +178,18 @@ pub fn parse_sync(src: String, opts: Buffer, filename: Option<String>) -> napi::
None
};

c.parse_js(
let mut p = c.parse_js(
fm,
handler,
options.target,
options.syntax,
options.is_module,
comments,
)
)?;

p.visit_mut_with(&mut resolver(Mark::new(), Mark::new()));

Ok(p)
})
})
.convert_err()?;
Expand All @@ -202,14 +215,17 @@ pub fn parse_file_sync(path: String, opts: Buffer) -> napi::Result<String> {
None
};

c.parse_js(
let mut p = c.parse_js(
fm,
handler,
options.target,
options.syntax,
options.is_module,
comments,
)
)?;
p.visit_mut_with(&mut resolver(Mark::new(), Mark::new()));

Ok(p)
})
}
.convert_err()?;
Expand Down
61 changes: 35 additions & 26 deletions bindings/binding_core_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ use swc_core::{
js_sys::{JsString, Promise},
noop, Options, ParseOptions, SourceMapsConfig,
},
common::{comments, errors::Handler, sync::Lrc, FileName, SourceMap, GLOBALS},
ecma::ast::{EsVersion, Program},
common::{comments, errors::Handler, sync::Lrc, FileName, Mark, SourceMap, GLOBALS},
ecma::{
ast::{EsVersion, Program},
transforms::base::resolver,
visit::VisitMutWith,
},
};
use wasm_bindgen::{prelude::*, JsCast};
mod types;
Expand Down Expand Up @@ -93,30 +97,35 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
let c = compiler();
try_with_handler(c.cm.clone(), Default::default(), |handler| {
c.run(|| {
let opts: ParseOptions = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
};
let fm = c.cm.new_source_file(FileName::Anon, s.into());
let cmts = c.comments().clone();
let comments = if opts.comments {
Some(&cmts as &dyn comments::Comments)
} else {
None
};
let program = anyhow::Context::context(
c.parse_js(
fm,
handler,
opts.target,
opts.syntax,
opts.is_module,
comments,
),
"failed to parse code",
)?;
anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
GLOBALS.set(&Default::default(), || {
let opts: ParseOptions = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
};
let fm = c.cm.new_source_file(FileName::Anon, s.into());
let cmts = c.comments().clone();
let comments = if opts.comments {
Some(&cmts as &dyn comments::Comments)
} else {
None
};
let program = anyhow::Context::context(
c.parse_js(
fm,
handler,
opts.target,
opts.syntax,
opts.is_module,
comments,
),
"failed to parse code",
)?;

program.visit_mut_with(&mut resolver(Mark::new(), Mark::new()));

anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
})
})
})
.map_err(|e| convert_err(e, None))
Expand Down
21 changes: 6 additions & 15 deletions crates/swc_css_codegen/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ impl VisitMut for NormalizeTest {
fn visit_mut_at_rule(&mut self, n: &mut AtRule) {
n.visit_mut_children_with(self);

match &mut n.name {
AtRuleName::Ident(ident) => {
ident.value = ident.value.to_lowercase().into();
}
_ => {}
if let AtRuleName::Ident(ident) = &mut n.name {
ident.value = ident.value.to_lowercase().into();
}
}

Expand Down Expand Up @@ -205,11 +202,8 @@ impl VisitMut for NormalizeTest {
fn visit_mut_keyframe_selector(&mut self, n: &mut KeyframeSelector) {
n.visit_mut_children_with(self);

match n {
KeyframeSelector::Ident(ident) => {
ident.value = ident.value.to_lowercase().into();
}
_ => {}
if let KeyframeSelector::Ident(ident) = n {
ident.value = ident.value.to_lowercase().into();
}
}

Expand Down Expand Up @@ -297,11 +291,8 @@ impl VisitMut for NormalizeTest {
fn visit_mut_declaration(&mut self, n: &mut Declaration) {
n.visit_mut_children_with(self);

match &mut n.name {
DeclarationName::Ident(name) => {
name.value = name.value.to_lowercase().into();
}
_ => {}
if let DeclarationName::Ident(name) = &mut n.name {
name.value = name.value.to_lowercase().into();
}
}

Expand Down