Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslnewell committed Dec 17, 2021
1 parent a2a0b63 commit c61d43a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
32 changes: 32 additions & 0 deletions crates/swc_ecma_transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6002,3 +6002,35 @@ test_exec!(
}
"
);

test!(
ts(),
|_| decorators(Config {
legacy: true,
emit_metadata: true,
}),
issue_2461_decorator_design_type_for_union_types,
"const SomeDecorator: PropertyDecorator = () => {}
class ChildItem {
@SomeDecorator
parentId?: string | null
}",
"var _class, _descriptor, _dec;
const SomeDecorator: PropertyDecorator = ()=>{ };
let ChildItem = ((_class = class ChildItem {
constructor(){
_initializerDefineProperty(this, \"parentId\", _descriptor, this);
}
}) || _class, _dec = typeof Reflect !== \"undefined\" && typeof Reflect.metadata === \
\"function\" && Reflect.metadata(\"design:type\", Object), _descriptor = \
_applyDecoratedDescriptor(_class.prototype, \"parentId\", [
SomeDecorator,
_dec
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
}), _class);"
);
Original file line number Diff line number Diff line change
Expand Up @@ -225,41 +225,37 @@ impl Fold for Metadata<'_> {
return p;
}

if let Some(name) = p
let expr_or_spread = p
.type_ann
.as_ref()
.map(|ty| &ty.type_ann)
.map(|type_ann| match &**type_ann {
TsType::TsTypeRef(r) => Some(r),
TsType::TsTypeRef(type_ref) => match &type_ref.type_name {
TsEntityName::Ident(type_name) => {
let kind = self.enums.get(&type_name.to_id());
match kind {
Some(EnumKind::Mixed) => Some(quote_ident!("Object").as_arg()),
Some(EnumKind::Str) => Some(quote_ident!("String").as_arg()),
Some(EnumKind::Num) => Some(quote_ident!("Number").as_arg()),
_ => None,
}
}
_ => None,
},
TsType::TsUnionOrIntersectionType(_union) => Some(quote_ident!("Object").as_arg()),
_ => None,
})
.flatten()
.map(|r| match &r.type_name {
TsEntityName::TsQualifiedName(_) => None,
TsEntityName::Ident(i) => Some(i),
})
.flatten()
{
if let Some(kind) = self.enums.get(&name.to_id()) {
let dec = self.create_metadata_design_decorator(
"design:type",
match kind {
EnumKind::Mixed => quote_ident!("Object").as_arg(),
EnumKind::Str => quote_ident!("String").as_arg(),
EnumKind::Num => quote_ident!("Number").as_arg(),
},
);
p.decorators.push(dec);
return p;
}
}
.flatten();

let dec = self.create_metadata_design_decorator(
"design:type",
serialize_type(self.class_name, p.type_ann.as_ref()).as_arg(),
if let Some(type_arg) = expr_or_spread {
type_arg
} else {
serialize_type(self.class_name, p.type_ann.as_ref()).as_arg()
},
);
p.decorators.push(dec);

p
}
}
Expand Down

0 comments on commit c61d43a

Please sign in to comment.