Skip to content

Commit 5b94698

Browse files
committed
Merge pull request #5294 from Microsoft/fixDecoratorSwitch
Fixes an issue with decorators when compiling using tsc.exe
2 parents 302db0a + 81e784c commit 5b94698

File tree

1 file changed

+88
-93
lines changed

1 file changed

+88
-93
lines changed

src/compiler/checker.ts

Lines changed: 88 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8646,39 +8646,36 @@ namespace ts {
86468646
*/
86478647
function getEffectiveDecoratorFirstArgumentType(node: Node): Type {
86488648
// The first argument to a decorator is its `target`.
8649-
switch (node.kind) {
8650-
case SyntaxKind.ClassDeclaration:
8651-
case SyntaxKind.ClassExpression:
8652-
// For a class decorator, the `target` is the type of the class (e.g. the
8653-
// "static" or "constructor" side of the class)
8649+
if (node.kind === SyntaxKind.ClassDeclaration) {
8650+
// For a class decorator, the `target` is the type of the class (e.g. the
8651+
// "static" or "constructor" side of the class)
8652+
let classSymbol = getSymbolOfNode(node);
8653+
return getTypeOfSymbol(classSymbol);
8654+
}
8655+
8656+
if (node.kind === SyntaxKind.Parameter) {
8657+
// For a parameter decorator, the `target` is the parent type of the
8658+
// parameter's containing method.
8659+
node = node.parent;
8660+
if (node.kind === SyntaxKind.Constructor) {
86548661
let classSymbol = getSymbolOfNode(node);
86558662
return getTypeOfSymbol(classSymbol);
8663+
}
8664+
}
86568665

8657-
case SyntaxKind.Parameter:
8658-
// For a parameter decorator, the `target` is the parent type of the
8659-
// parameter's containing method.
8660-
node = node.parent;
8661-
if (node.kind === SyntaxKind.Constructor) {
8662-
let classSymbol = getSymbolOfNode(node);
8663-
return getTypeOfSymbol(classSymbol);
8664-
}
8665-
8666-
// fall-through
8667-
8668-
case SyntaxKind.PropertyDeclaration:
8669-
case SyntaxKind.MethodDeclaration:
8670-
case SyntaxKind.GetAccessor:
8671-
case SyntaxKind.SetAccessor:
8672-
// For a property or method decorator, the `target` is the
8673-
// "static"-side type of the parent of the member if the member is
8674-
// declared "static"; otherwise, it is the "instance"-side type of the
8675-
// parent of the member.
8676-
return getParentTypeOfClassElement(<ClassElement>node);
8677-
8678-
default:
8679-
Debug.fail("Unsupported decorator target.");
8680-
return unknownType;
8666+
if (node.kind === SyntaxKind.PropertyDeclaration ||
8667+
node.kind === SyntaxKind.MethodDeclaration ||
8668+
node.kind === SyntaxKind.GetAccessor ||
8669+
node.kind === SyntaxKind.SetAccessor) {
8670+
// For a property or method decorator, the `target` is the
8671+
// "static"-side type of the parent of the member if the member is
8672+
// declared "static"; otherwise, it is the "instance"-side type of the
8673+
// parent of the member.
8674+
return getParentTypeOfClassElement(<ClassElement>node);
86818675
}
8676+
8677+
Debug.fail("Unsupported decorator target.");
8678+
return unknownType;
86828679
}
86838680

86848681
/**
@@ -8698,57 +8695,54 @@ namespace ts {
86988695
*/
86998696
function getEffectiveDecoratorSecondArgumentType(node: Node) {
87008697
// The second argument to a decorator is its `propertyKey`
8701-
switch (node.kind) {
8702-
case SyntaxKind.ClassDeclaration:
8703-
Debug.fail("Class decorators should not have a second synthetic argument.");
8704-
return unknownType;
8698+
if (node.kind === SyntaxKind.ClassDeclaration) {
8699+
Debug.fail("Class decorators should not have a second synthetic argument.");
8700+
return unknownType;
8701+
}
87058702

8706-
case SyntaxKind.Parameter:
8707-
node = node.parent;
8708-
if (node.kind === SyntaxKind.Constructor) {
8709-
// For a constructor parameter decorator, the `propertyKey` will be `undefined`.
8710-
return anyType;
8711-
}
8703+
if (node.kind === SyntaxKind.Parameter) {
8704+
node = node.parent;
8705+
if (node.kind === SyntaxKind.Constructor) {
8706+
// For a constructor parameter decorator, the `propertyKey` will be `undefined`.
8707+
return anyType;
8708+
}
87128709

87138710
// For a non-constructor parameter decorator, the `propertyKey` will be either
87148711
// a string or a symbol, based on the name of the parameter's containing method.
8712+
}
87158713

8716-
// fall-through
8717-
8718-
case SyntaxKind.PropertyDeclaration:
8719-
case SyntaxKind.MethodDeclaration:
8720-
case SyntaxKind.GetAccessor:
8721-
case SyntaxKind.SetAccessor:
8722-
// The `propertyKey` for a property or method decorator will be a
8723-
// string literal type if the member name is an identifier, number, or string;
8724-
// otherwise, if the member name is a computed property name it will
8725-
// be either string or symbol.
8726-
let element = <ClassElement>node;
8727-
switch (element.name.kind) {
8728-
case SyntaxKind.Identifier:
8729-
case SyntaxKind.NumericLiteral:
8730-
case SyntaxKind.StringLiteral:
8731-
return getStringLiteralType(<StringLiteral>element.name);
8732-
8733-
case SyntaxKind.ComputedPropertyName:
8734-
let nameType = checkComputedPropertyName(<ComputedPropertyName>element.name);
8735-
if (allConstituentTypesHaveKind(nameType, TypeFlags.ESSymbol)) {
8736-
return nameType;
8737-
}
8738-
else {
8739-
return stringType;
8740-
}
8741-
8742-
default:
8743-
Debug.fail("Unsupported property name.");
8744-
return unknownType;
8745-
}
8714+
if (node.kind === SyntaxKind.PropertyDeclaration ||
8715+
node.kind === SyntaxKind.MethodDeclaration ||
8716+
node.kind === SyntaxKind.GetAccessor ||
8717+
node.kind === SyntaxKind.SetAccessor) {
8718+
// The `propertyKey` for a property or method decorator will be a
8719+
// string literal type if the member name is an identifier, number, or string;
8720+
// otherwise, if the member name is a computed property name it will
8721+
// be either string or symbol.
8722+
let element = <ClassElement>node;
8723+
switch (element.name.kind) {
8724+
case SyntaxKind.Identifier:
8725+
case SyntaxKind.NumericLiteral:
8726+
case SyntaxKind.StringLiteral:
8727+
return getStringLiteralType(<StringLiteral>element.name);
87468728

8729+
case SyntaxKind.ComputedPropertyName:
8730+
let nameType = checkComputedPropertyName(<ComputedPropertyName>element.name);
8731+
if (allConstituentTypesHaveKind(nameType, TypeFlags.ESSymbol)) {
8732+
return nameType;
8733+
}
8734+
else {
8735+
return stringType;
8736+
}
87478737

8748-
default:
8749-
Debug.fail("Unsupported decorator target.");
8750-
return unknownType;
8738+
default:
8739+
Debug.fail("Unsupported property name.");
8740+
return unknownType;
8741+
}
87518742
}
8743+
8744+
Debug.fail("Unsupported decorator target.");
8745+
return unknownType;
87528746
}
87538747

87548748
/**
@@ -8761,31 +8755,32 @@ namespace ts {
87618755
function getEffectiveDecoratorThirdArgumentType(node: Node) {
87628756
// The third argument to a decorator is either its `descriptor` for a method decorator
87638757
// or its `parameterIndex` for a paramter decorator
8764-
switch (node.kind) {
8765-
case SyntaxKind.ClassDeclaration:
8766-
Debug.fail("Class decorators should not have a third synthetic argument.");
8767-
return unknownType;
8768-
8769-
case SyntaxKind.Parameter:
8770-
// The `parameterIndex` for a parameter decorator is always a number
8771-
return numberType;
8758+
if (node.kind === SyntaxKind.ClassDeclaration) {
8759+
Debug.fail("Class decorators should not have a third synthetic argument.");
8760+
return unknownType;
8761+
}
87728762

8773-
case SyntaxKind.PropertyDeclaration:
8774-
Debug.fail("Property decorators should not have a third synthetic argument.");
8775-
return unknownType;
8763+
if (node.kind === SyntaxKind.Parameter) {
8764+
// The `parameterIndex` for a parameter decorator is always a number
8765+
return numberType;
8766+
}
87768767

8777-
case SyntaxKind.MethodDeclaration:
8778-
case SyntaxKind.GetAccessor:
8779-
case SyntaxKind.SetAccessor:
8780-
// The `descriptor` for a method decorator will be a `TypedPropertyDescriptor<T>`
8781-
// for the type of the member.
8782-
let propertyType = getTypeOfNode(node);
8783-
return createTypedPropertyDescriptorType(propertyType);
8768+
if (node.kind === SyntaxKind.PropertyDeclaration) {
8769+
Debug.fail("Property decorators should not have a third synthetic argument.");
8770+
return unknownType;
8771+
}
87848772

8785-
default:
8786-
Debug.fail("Unsupported decorator target.");
8787-
return unknownType;
8773+
if (node.kind === SyntaxKind.MethodDeclaration ||
8774+
node.kind === SyntaxKind.GetAccessor ||
8775+
node.kind === SyntaxKind.SetAccessor) {
8776+
// The `descriptor` for a method decorator will be a `TypedPropertyDescriptor<T>`
8777+
// for the type of the member.
8778+
let propertyType = getTypeOfNode(node);
8779+
return createTypedPropertyDescriptorType(propertyType);
87888780
}
8781+
8782+
Debug.fail("Unsupported decorator target.");
8783+
return unknownType;
87898784
}
87908785

87918786
/**

0 commit comments

Comments
 (0)