Skip to content

Commit 978dd37

Browse files
committed
fix(miniprogram-typescript): fix some code gen issue
1 parent e55c424 commit 978dd37

File tree

3 files changed

+91
-16
lines changed

3 files changed

+91
-16
lines changed

glass-easel-miniprogram-typescript/src/server.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,19 @@ export class Server {
268268
return tsc.sys.readDirectory(fullPath, extensions, exclude, include, depth)
269269
},
270270
readFile: (fullPath, encoding) => {
271+
if (fullPath === this.options.templateBackendConfigPath) {
272+
if (this.options.templateBackendConfig !== null) {
273+
return this.options.templateBackendConfig
274+
}
275+
}
271276
return tsc.sys.readFile(fullPath, encoding)
272277
},
273278
fileExists: (fullPath) => {
279+
if (fullPath === this.options.templateBackendConfigPath) {
280+
if (this.templateBackendConfig !== null) {
281+
return true
282+
}
283+
}
274284
return tsc.sys.fileExists(fullPath)
275285
},
276286
getDirectories: (directoryName) => {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,30 @@ impl Expression {
12701270
}
12711271
}
12721272

1273+
pub fn has_multiple_static_or_dynamic_parts(&self) -> bool {
1274+
match self {
1275+
Expression::Plus {
1276+
left,
1277+
right,
1278+
location: _,
1279+
} => {
1280+
let split = if let Expression::ToStringWithoutUndefined { .. }
1281+
| Expression::LitStr { .. } = &**left
1282+
{
1283+
true
1284+
} else if let Expression::ToStringWithoutUndefined { .. }
1285+
| Expression::LitStr { .. } = &**right
1286+
{
1287+
true
1288+
} else {
1289+
false
1290+
};
1291+
split
1292+
}
1293+
_ => false,
1294+
}
1295+
}
1296+
12731297
pub(super) fn validate_scopes(
12741298
&self,
12751299
ps: &mut ParseState,

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

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,28 @@ pub(crate) fn generate_tmpl_converted_expr(
5252
w.write_line(|w| w.write_str(ts_env))?;
5353
w.write_line(|w| w.write_str(runtime))?;
5454
for script in tree.globals.scripts.iter() {
55-
w.add_scope_with_ts_keyword_escape(&script.module_name().name, &PRESERVED_VAR_NAMES);
55+
let location = &script.module_name().location;
56+
let scope_name = &script.module_name().name;
57+
w.add_scope_with_ts_keyword_escape(scope_name, &PRESERVED_VAR_NAMES);
58+
w.write_line(|w| {
59+
let pos = location.start;
60+
write_token_series(
61+
["const "],
62+
&(pos..pos),
63+
w,
64+
)?;
65+
w.write_token_state(
66+
scope_name,
67+
Some(scope_name),
68+
&location,
69+
StringifierLineState::Normal,
70+
)?;
71+
write_token_series(
72+
["=", "{", "}", "as", "{", "[", "k", ":", "string", "]", ":", "any", "}", ";"],
73+
&(pos..pos),
74+
w,
75+
)
76+
})?;
5677
}
5778

5879
// TODO tree.globals.imports
@@ -96,11 +117,13 @@ impl ConvertedExprWriteBlock for Node {
96117
&self,
97118
w: &mut StringifierBlock<'s, 't, W>,
98119
) -> FmtResult {
99-
match self {
100-
Self::Text(x) => write_dynamic_value(x, w),
101-
Self::Element(x) => x.converted_expr_write(w),
102-
Self::Comment(_) | Self::UnknownMetaTag(_) => Ok(()),
103-
}
120+
w.new_scope_space(|w| {
121+
match self {
122+
Self::Text(x) => write_dynamic_value(x, w),
123+
Self::Element(x) => x.converted_expr_write(w),
124+
Self::Comment(_) | Self::UnknownMetaTag(_) => Ok(()),
125+
}
126+
})
104127
}
105128
}
106129

@@ -643,14 +666,20 @@ impl ConvertedExprWriteInline for Value {
643666
),
644667
Self::Dynamic {
645668
expression,
646-
double_brace_location: _,
669+
double_brace_location,
647670
binding_map_keys: _,
648671
} => {
672+
let has_multi_segs = expression.has_multiple_static_or_dynamic_parts();
649673
let mut prev_loc = None;
650674
expression.for_each_static_or_dynamic_part::<std::fmt::Error>(|part, loc| {
651675
if let Some(prev_loc) = prev_loc.take() {
652676
w.write_token_state("+", None, &prev_loc, StringifierLineState::Normal)?;
653677
}
678+
let is_static_str = if let Expression::LitStr { .. } = part { true } else { false };
679+
let need_paren = !is_static_str && has_multi_segs;
680+
if need_paren {
681+
w.write_token_state("(", None, &double_brace_location.0, StringifierLineState::Normal)?;
682+
}
654683
if let Expression::ToStringWithoutUndefined { value, location } = part {
655684
value.converted_expr_write(w)?;
656685
prev_loc = Some(location.end..location.end);
@@ -659,6 +688,9 @@ impl ConvertedExprWriteInline for Value {
659688
let pos = loc.end;
660689
prev_loc = Some(pos..pos);
661690
}
691+
if need_paren {
692+
w.write_token_state(")", None, &double_brace_location.1, StringifierLineState::Normal)?;
693+
}
662694
Ok(())
663695
})
664696
}
@@ -858,15 +890,15 @@ mod test {
858890
#[test]
859891
fn composed_text_node() {
860892
let src = r#"Hello {{ world }}!"#;
861-
let expect = r#""Hello "+data.world+"!";"#;
893+
let expect = r#""Hello "+(data.world)+"!";"#;
862894
let (out, sm) = convert(src);
863895
assert_eq!(out, expect);
864896
assert_eq!(find_token(&sm, 0, 0), Some((0, 0)));
865897
assert_eq!(find_token(&sm, 0, 8), Some((0, 6)));
866-
assert_eq!(find_token(&sm, 0, 9), Some((0, 9)));
867-
assert_eq!(find_token(&sm, 0, 14), Some((0, 9)));
868-
assert_eq!(find_token(&sm, 0, 19), Some((0, 17)));
869-
assert_eq!(find_token(&sm, 0, 23), Some((0, 18)));
898+
assert_eq!(find_token(&sm, 0, 10), Some((0, 9)));
899+
assert_eq!(find_token(&sm, 0, 15), Some((0, 9)));
900+
assert_eq!(find_token(&sm, 0, 21), Some((0, 17)));
901+
assert_eq!(find_token(&sm, 0, 25), Some((0, 18)));
870902
}
871903

872904
#[test]
@@ -961,7 +993,7 @@ mod test {
961993
#[test]
962994
fn element_class_single() {
963995
let src = r#"<view class="a {{ b }}" />"#;
964-
let expect = r#"{const _tag_=tags['view'];var _class_:string="a "+data.b;}"#;
996+
let expect = r#"{const _tag_=tags['view'];var _class_:string="a "+(data.b);}"#;
965997
let (out, sm) = convert(src);
966998
assert_eq!(out, expect);
967999
assert_eq!(find_token(&sm, 0, 26), Some((0, 6)));
@@ -1107,11 +1139,12 @@ mod test {
11071139
#[test]
11081140
fn element_with_scripts() {
11091141
let src = r#"<view let:a="{{ b.c() }}" /><wxs module="b">;</wxs>"#;
1110-
let expect = r#"{const _tag_=tags['view'];const a=b?.c?.();}"#;
1142+
let expect = r#"const b={}as{[k:string]:any};{const _tag_=tags['view'];const a=b?.c?.();}"#;
11111143
let (out, sm) = convert(src);
11121144
assert_eq!(out, expect);
1113-
assert_eq!(find_token(&sm, 0, 32), Some((0, 10)));
1114-
assert_eq!(find_token(&sm, 0, 34), Some((0, 16)));
1145+
assert_eq!(find_token(&sm, 0, 6), Some((0, 41)));
1146+
assert_eq!(find_token(&sm, 0, 61), Some((0, 10)));
1147+
assert_eq!(find_token(&sm, 0, 63), Some((0, 16)));
11151148
}
11161149

11171150
#[test]
@@ -1218,6 +1251,14 @@ mod test {
12181251
assert_eq!(find_token(&sm, 0, 202), Some((0, 1)));
12191252
}
12201253

1254+
#[test]
1255+
fn mixed_scopes() {
1256+
let src = r#"<view wx:for="{{ list }}" wx:for-item="v" wx:for-index="k" hidden="{{ k && v }}" /><view let:a="{{ b }}">{{ a }}</view>"#;
1257+
let expect = r#"{const _for_=data.list;const v=0 as unknown as _ForItem_<typeof _for_>;const k=0 as unknown as _ForIndex_<typeof _for_>;{const _tag_=tags['view'];_tag_.hidden=k&&v;}}{const _tag_=tags['view'];const a=data.b;a;}"#;
1258+
let (out, _) = convert(src);
1259+
assert_eq!(out, expect);
1260+
}
1261+
12211262
#[test]
12221263
fn if_branches() {
12231264
let src = r#"<block wx:if="{{ a }}" /><block wx:elif="{{ b }}" /><block wx:else />"#;

0 commit comments

Comments
 (0)