Skip to content

Commit 387d8d9

Browse files
committed
fmt: cargo
1 parent 3a3bad2 commit 387d8d9

File tree

9 files changed

+569
-140
lines changed

9 files changed

+569
-140
lines changed

glass-easel-template-compiler/src/group.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ impl TmplGroup {
266266
minimize: true,
267267
..Default::default()
268268
};
269-
let mut stringifier = crate::stringify::Stringifier::new(String::new(), path, None, options);
269+
let mut stringifier =
270+
crate::stringify::Stringifier::new(String::new(), path, None, options);
270271
template.stringify_write(&mut stringifier).unwrap();
271272
let (stringify_result, _sourcemap) = stringifier.finish();
272273
Some(stringify_result)
@@ -513,10 +514,16 @@ impl TmplGroup {
513514
}
514515

515516
/// Get a string that used to check TypeScript problems.
516-
pub(crate) fn get_tmpl_converted_expr(&self, path: &str, ts_env: &str) -> Result<(String, SourceMap), TmplError> {
517+
pub(crate) fn get_tmpl_converted_expr(
518+
&self,
519+
path: &str,
520+
ts_env: &str,
521+
) -> Result<(String, SourceMap), TmplError> {
517522
let tree = self.get_tree(path)?;
518523
let env = crate::stringify::typescript::tmpl_converted_expr_runtime_string();
519-
Ok(crate::stringify::typescript::generate_tmpl_converted_expr(tree, ts_env, env))
524+
Ok(crate::stringify::typescript::generate_tmpl_converted_expr(
525+
tree, ts_env, env,
526+
))
520527
}
521528

522529
/// Returns the number of templates in the group.

glass-easel-template-compiler/src/js_bindings.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ impl TmplGroup {
204204

205205
#[wasm_bindgen(js_name = "getTmplConvertedExpr")]
206206
#[doc(hidden)]
207-
pub fn get_tmpl_converted_expr(&mut self, path: &str, ts_env: &str) -> Result<TmplConvertedExpr, JsError> {
207+
pub fn get_tmpl_converted_expr(
208+
&mut self,
209+
path: &str,
210+
ts_env: &str,
211+
) -> Result<TmplConvertedExpr, JsError> {
208212
let (code, source_map) = self.group.get_tmpl_converted_expr(path, ts_env)?;
209213
Ok(TmplConvertedExpr { code, source_map })
210214
}
@@ -224,7 +228,13 @@ impl TmplConvertedExpr {
224228
}
225229

226230
#[wasm_bindgen(js_name = "getSourceLocation")]
227-
pub fn get_source_location(&self, start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Option<Vec<u32>> {
231+
pub fn get_source_location(
232+
&self,
233+
start_line: u32,
234+
start_col: u32,
235+
end_line: u32,
236+
end_col: u32,
237+
) -> Option<Vec<u32>> {
228238
let start = self.source_map.lookup_token(start_line, start_col)?;
229239
let (ret0, ret1) = start.get_src();
230240
if (start_line, start_col) == (end_line, end_col) {
@@ -246,16 +256,27 @@ impl TmplConvertedExpr {
246256
#[wasm_bindgen(js_name = "getTokenAtSourcePosition")]
247257
pub fn get_token_at_source_position(&self, line: u32, col: u32) -> Option<Vec<u32>> {
248258
for token in self.source_map.tokens() {
249-
let Some(name) = token.get_name() else { continue };
259+
let Some(name) = token.get_name() else {
260+
continue;
261+
};
250262
let (src_start_line, src_start_col) = token.get_src();
251-
if src_start_line != line { continue };
263+
if src_start_line != line {
264+
continue;
265+
};
252266
let src_end_line = src_start_line;
253267
let src_end_col = src_start_col + name.len() as u32;
254268
if !(src_start_col..=src_end_col).contains(&col) {
255269
continue;
256270
}
257271
let (dst_start_line, dst_start_col) = token.get_dst();
258-
return Some(vec![src_start_line, src_start_col, src_end_line, src_end_col, dst_start_line, dst_start_col]);
272+
return Some(vec![
273+
src_start_line,
274+
src_start_col,
275+
src_end_line,
276+
src_end_col,
277+
dst_start_line,
278+
dst_start_col,
279+
]);
259280
}
260281
None
261282
}

glass-easel-template-compiler/src/parse/expr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,8 +2190,12 @@ mod test {
21902190
minimize: true,
21912191
..Default::default()
21922192
};
2193-
let mut stringifier =
2194-
crate::stringify::Stringifier::new(String::new(), "test", Some(src), options);
2193+
let mut stringifier = crate::stringify::Stringifier::new(
2194+
String::new(),
2195+
"test",
2196+
Some(src),
2197+
options,
2198+
);
21952199
x.stringify_write(&mut stringifier).unwrap();
21962200
let (s, _) = stringifier.finish();
21972201
s

glass-easel-template-compiler/src/parse/tag.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,10 @@ impl Element {
846846
(ElementKind::Normal { .. }, "class") => AttrPrefixKind::ClassString,
847847
(ElementKind::Normal { .. }, "style") => AttrPrefixKind::StyleString,
848848
(ElementKind::Normal { .. }, x) | (ElementKind::Slot { .. }, x)
849-
if x.starts_with("data-") && x != "data-" => {
850-
AttrPrefixKind::DataHyphen
851-
}
849+
if x.starts_with("data-") && x != "data-" =>
850+
{
851+
AttrPrefixKind::DataHyphen
852+
}
852853
_ => AttrPrefixKind::Normal,
853854
},
854855
Some(x) => match x.name.as_str() {
@@ -4101,7 +4102,10 @@ mod test {
41014102
ParseErrorKind::InvalidScopeName,
41024103
13..15
41034104
);
4104-
case!("<div let:a-b='{{a}}'>{{ aB }}</div>", r#"<div let:aB="{{a}}">{{aB}}</div>"#);
4105+
case!(
4106+
"<div let:a-b='{{a}}'>{{ aB }}</div>",
4107+
r#"<div let:aB="{{a}}">{{aB}}</div>"#
4108+
);
41054109
case!(
41064110
"<div let:a></div>",
41074111
r#"<div let:a/>"#,

glass-easel-template-compiler/src/stringify/expr.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ pub(super) fn expression_strigify_write<W: FmtWrite>(
8080
expression: &Expression,
8181
stringifier: &mut StringifierLine<W>,
8282
accept_level: ExpressionLevel,
83-
filter: &impl Fn(&Expression, &mut StringifierLine<W>, ExpressionLevel) -> Result<bool, std::fmt::Error>,
83+
filter: &impl Fn(
84+
&Expression,
85+
&mut StringifierLine<W>,
86+
ExpressionLevel,
87+
) -> Result<bool, std::fmt::Error>,
8488
) -> FmtResult {
8589
if !filter(expression, stringifier, accept_level)? {
8690
return Ok(());
@@ -181,7 +185,12 @@ pub(super) fn expression_strigify_write<W: FmtWrite>(
181185
colon_location.as_ref().unwrap_or(location),
182186
StringifierLineState::NoSpaceBefore,
183187
)?;
184-
expression_strigify_write(value, stringifier, ExpressionLevel::Cond, filter)?;
188+
expression_strigify_write(
189+
value,
190+
stringifier,
191+
ExpressionLevel::Cond,
192+
filter,
193+
)?;
185194
}
186195
}
187196
ObjectFieldKind::Spread { location, value } => {
@@ -191,7 +200,12 @@ pub(super) fn expression_strigify_write<W: FmtWrite>(
191200
location,
192201
StringifierLineState::NoSpaceAfter,
193202
)?;
194-
expression_strigify_write(value, stringifier, ExpressionLevel::Cond, filter)?;
203+
expression_strigify_write(
204+
value,
205+
stringifier,
206+
ExpressionLevel::Cond,
207+
filter,
208+
)?;
195209
}
196210
}
197211
}
@@ -218,7 +232,12 @@ pub(super) fn expression_strigify_write<W: FmtWrite>(
218232
}
219233
match field {
220234
ArrayFieldKind::Normal { value } => {
221-
expression_strigify_write(value, stringifier, ExpressionLevel::Cond, filter)?;
235+
expression_strigify_write(
236+
value,
237+
stringifier,
238+
ExpressionLevel::Cond,
239+
filter,
240+
)?;
222241
}
223242
ArrayFieldKind::Spread { location, value } => {
224243
stringifier.write_token_state(
@@ -227,7 +246,12 @@ pub(super) fn expression_strigify_write<W: FmtWrite>(
227246
location,
228247
StringifierLineState::NoSpaceAfter,
229248
)?;
230-
expression_strigify_write(value, stringifier, ExpressionLevel::Cond, filter)?;
249+
expression_strigify_write(
250+
value,
251+
stringifier,
252+
ExpressionLevel::Cond,
253+
filter,
254+
)?;
231255
}
232256
ArrayFieldKind::EmptySlot => {
233257
if index == fields.len() - 1 {
@@ -620,7 +644,9 @@ impl StringifyLine for Expression {
620644
&self,
621645
stringifier: &mut StringifierLine<'s, 't, 'u, W>,
622646
) -> FmtResult {
623-
expression_strigify_write(self, stringifier, ExpressionLevel::Cond, &|_, _, _| Ok(true))
647+
expression_strigify_write(self, stringifier, ExpressionLevel::Cond, &|_, _, _| {
648+
Ok(true)
649+
})
624650
}
625651
}
626652

@@ -637,8 +663,12 @@ mod tests {
637663

638664
fn case(src: &str) {
639665
let (template, _) = crate::parse::parse("TEST", src);
640-
let mut stringifier =
641-
crate::stringify::Stringifier::new(String::new(), "test", Some(src), Default::default());
666+
let mut stringifier = crate::stringify::Stringifier::new(
667+
String::new(),
668+
"test",
669+
Some(src),
670+
Default::default(),
671+
);
642672
template.stringify_write(&mut stringifier).unwrap();
643673
let (stringify_result, _sourcemap) = stringifier.finish();
644674
assert_eq!(stringify_result.as_str(), &format!("{}\n", src));

glass-easel-template-compiler/src/stringify/mod.rs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,59 @@ pub(crate) mod typescript;
1111

1212
fn is_typescript_keyword(s: &str) -> bool {
1313
const TS_KEYWORDS: [&'static str; 53] = [
14-
"break", "case", "catch", "class", "const", "continue", "debugger", "default",
15-
"delete", "do", "else", "export", "extends", "finally", "for", "function",
16-
"if", "import", "in", "instanceof", "new", "return", "super", "switch",
17-
"this", "throw", "try", "typeof", "var", "void", "while", "with", "yield",
18-
"enum", "await", "implements", "interface", "let", "package", "private",
19-
"protected", "public", "static", "arguments", "eval",
20-
"type", "namespace", "module", "declare", "as", "is", "keyof", "readonly"
14+
"break",
15+
"case",
16+
"catch",
17+
"class",
18+
"const",
19+
"continue",
20+
"debugger",
21+
"default",
22+
"delete",
23+
"do",
24+
"else",
25+
"export",
26+
"extends",
27+
"finally",
28+
"for",
29+
"function",
30+
"if",
31+
"import",
32+
"in",
33+
"instanceof",
34+
"new",
35+
"return",
36+
"super",
37+
"switch",
38+
"this",
39+
"throw",
40+
"try",
41+
"typeof",
42+
"var",
43+
"void",
44+
"while",
45+
"with",
46+
"yield",
47+
"enum",
48+
"await",
49+
"implements",
50+
"interface",
51+
"let",
52+
"package",
53+
"private",
54+
"protected",
55+
"public",
56+
"static",
57+
"arguments",
58+
"eval",
59+
"type",
60+
"namespace",
61+
"module",
62+
"declare",
63+
"as",
64+
"is",
65+
"keyof",
66+
"readonly",
2167
];
2268
TS_KEYWORDS.contains(&s)
2369
}

glass-easel-template-compiler/src/stringify/stringifier.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ pub struct Stringifier<'s, W: FmtWrite> {
2626
}
2727

2828
impl<'s, W: FmtWrite> Stringifier<'s, W> {
29-
pub fn new(w: W, source_path: &'s str, source: Option<&'s str>, options: StringifyOptions) -> Self {
29+
pub fn new(
30+
w: W,
31+
source_path: &'s str,
32+
source: Option<&'s str>,
33+
options: StringifyOptions,
34+
) -> Self {
3035
let smb = if options.source_map {
3136
let mut smb = SourceMapBuilder::new(Some(source_path));
3237
let source_id = smb.add_source(source_path);
@@ -110,8 +115,15 @@ impl<'s, 't, W: FmtWrite> StringifierBlock<'s, 't, W> {
110115
&self.scope_names[i]
111116
}
112117

113-
pub(super) fn add_scope_with_ts_keyword_escape(&mut self, name: &CompactString, extra_preserved: &[&'static str]) -> CompactString {
114-
let name = if super::is_typescript_keyword(name) || extra_preserved.contains(&name.as_str()) || name.starts_with("_") {
118+
pub(super) fn add_scope_with_ts_keyword_escape(
119+
&mut self,
120+
name: &CompactString,
121+
extra_preserved: &[&'static str],
122+
) -> CompactString {
123+
let name = if super::is_typescript_keyword(name)
124+
|| extra_preserved.contains(&name.as_str())
125+
|| name.starts_with("_")
126+
{
115127
compact_str::format_compact!("${}", name)
116128
} else {
117129
name.clone()
@@ -253,7 +265,12 @@ impl<'s, 't, 'u, W: FmtWrite> StringifierLine<'s, 't, 'u, W> {
253265
) -> FmtResult {
254266
let name = format!("{}{}", prefix, self.block.get_scope_name(index));
255267
let src_name = self.block.get_scope_name(index).to_string();
256-
self.write_token_state(&name, Some(&src_name), location, StringifierLineState::Normal)
268+
self.write_token_state(
269+
&name,
270+
Some(&src_name),
271+
location,
272+
StringifierLineState::Normal,
273+
)
257274
}
258275

259276
pub(super) fn write_token(

0 commit comments

Comments
 (0)