Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8dbdbef

Browse filesBrowse files
authored
fix(ts/fast-dts): Ensure correct emission of template literals and symbol-keyed properties (#10530)
1 parent ee4bef8 commit 8dbdbef
Copy full SHA for 8dbdbef

File tree

Expand file treeCollapse file tree

5 files changed

+64
-11
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+64
-11
lines changed

‎.changeset/cyan-humans-kick.md

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_typescript: patch
4+
---
5+
6+
fix(ts/fast-dts): Align computed key emission with TypeScript

‎crates/swc_typescript/src/fast_dts/types.rs

Copy file name to clipboardExpand all lines: crates/swc_typescript/src/fast_dts/types.rs
+49-10Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use rustc_hash::{FxHashMap, FxHashSet};
2-
use swc_common::{BytePos, Span, Spanned, DUMMY_SP};
2+
use swc_common::{BytePos, Span, Spanned, SyntaxContext, DUMMY_SP};
33
use swc_ecma_ast::{
4-
ArrayLit, ArrowExpr, Expr, Function, GetterProp, Lit, ObjectLit, Param, Pat, Prop, PropName,
5-
PropOrSpread, Str, Tpl, TsFnOrConstructorType, TsFnParam, TsFnType, TsKeywordTypeKind, TsLit,
6-
TsMethodSignature, TsPropertySignature, TsTupleElement, TsTupleType, TsType, TsTypeAnn,
7-
TsTypeElement, TsTypeLit, TsTypeOperator, TsTypeOperatorOp, UnaryOp,
4+
ArrayLit, ArrowExpr, Expr, Function, GetterProp, Lit, MemberExpr, ObjectLit, Param, Pat, Prop,
5+
PropName, PropOrSpread, Str, Tpl, TsFnOrConstructorType, TsFnParam, TsFnType,
6+
TsKeywordTypeKind, TsLit, TsMethodSignature, TsPropertySignature, TsTupleElement, TsTupleType,
7+
TsType, TsTypeAnn, TsTypeElement, TsTypeLit, TsTypeOperator, TsTypeOperatorOp, UnaryOp,
88
};
9+
use swc_ecma_utils::quote_ident;
910

1011
use super::{
1112
inferrer::ReturnTypeInferrer,
@@ -342,11 +343,48 @@ impl FastDts {
342343

343344
pub(crate) fn transform_property_name_to_expr(&mut self, name: &PropName) -> Expr {
344345
match name {
345-
PropName::Ident(ident) => Expr::Ident(ident.clone().into()),
346-
PropName::Str(str_prop) => Lit::Str(str_prop.clone()).into(),
347-
PropName::Num(num) => Lit::Num(num.clone()).into(),
348-
PropName::Computed(computed) => *computed.expr.clone(),
349-
PropName::BigInt(big_int) => Lit::BigInt(big_int.clone()).into(),
346+
PropName::Ident(ident) => ident.clone().into(),
347+
PropName::Str(str_prop) => str_prop.clone().into(),
348+
PropName::Num(num) => num.clone().into(),
349+
PropName::BigInt(big_int) => big_int.clone().into(),
350+
PropName::Computed(computed) => {
351+
if let Some(prop) = computed.expr.get_global_symbol_prop(self.unresolved_mark) {
352+
let ctxt = SyntaxContext::empty().apply_mark(self.unresolved_mark);
353+
let symbol = quote_ident!(ctxt, "Symbol");
354+
355+
return MemberExpr {
356+
span: name.span(),
357+
obj: symbol.into(),
358+
prop: prop.clone(),
359+
}
360+
.into();
361+
}
362+
363+
if let Expr::Tpl(Tpl {
364+
span,
365+
exprs,
366+
quasis,
367+
}) = computed.expr.as_ref()
368+
{
369+
if exprs.is_empty() {
370+
let str_prop = quasis
371+
.first()
372+
.and_then(|el| el.cooked.as_ref())
373+
.unwrap()
374+
.clone();
375+
376+
let str_prop = Str {
377+
span: *span,
378+
value: str_prop,
379+
raw: None,
380+
};
381+
382+
return str_prop.into();
383+
}
384+
}
385+
386+
computed.expr.as_ref().clone()
387+
}
350388
}
351389
}
352390

@@ -410,6 +448,7 @@ impl FastDts {
410448
pub(crate) fn is_literal(expr: &Expr) -> bool {
411449
match expr {
412450
Expr::Lit(_) => true,
451+
Expr::Tpl(tpl) => tpl.exprs.is_empty(),
413452
Expr::Unary(unary) => Self::can_infer_unary_expr(unary),
414453
_ => false,
415454
}

‎crates/swc_typescript/tests/fixture/symbol-properties.snap

Copy file name to clipboardExpand all lines: crates/swc_typescript/tests/fixture/symbol-properties.snap
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
export declare const foo: {
55
[Symbol.iterator]: () => void;
66
[Symbol.asyncIterator]: () => Promise<void>;
7-
[globalThis.Symbol.iterator]: () => void;
7+
[Symbol.iterator]: () => void;
88
readonly [Symbol.toStringTag]: string;
99
};
1010
export declare abstract class Foo {

‎crates/swc_typescript/tests/fixture/ts-property.snap

Copy file name to clipboardExpand all lines: crates/swc_typescript/tests/fixture/ts-property.snap
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export declare const foo: {
1010
"foo"(): string;
1111
["bar"](): string;
1212
123(): string;
13+
[456](): string;
1314
1n(): string;
15+
["x\ny"](): string;
1416
};
1517
export { };
1618

‎crates/swc_typescript/tests/fixture/ts-property.ts

Copy file name to clipboardExpand all lines: crates/swc_typescript/tests/fixture/ts-property.ts
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ export const foo = {
1515
123(): string {
1616
return "123";
1717
},
18+
[456](): string {
19+
return "456";
20+
},
1821
1n(): string {
1922
return "1n";
2023
},
24+
[`x\ny`](): string {
25+
return "x\ny";
26+
},
2127
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.