Skip to content

Commit

Permalink
Fix computed property names of types and abstract members
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Jan 16, 2020
1 parent 1044c5c commit a5ca492
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ namespace ts {
if (
!(useSite.flags & NodeFlags.Ambient) &&
!isPartOfTypeQuery(useSite) &&
!isPartOfPossiblyValidComputedPropertyNameExpression(useSite) &&
!isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite) &&
isExpressionNode(useSite)
) {
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);
Expand Down
15 changes: 13 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1778,11 +1778,22 @@ namespace ts {
return node.kind === SyntaxKind.TypeQuery;
}

export function isPartOfPossiblyValidComputedPropertyNameExpression(node: Node) {
export function isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(node: Node) {
while (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression) {
node = node.parent;
}
return node.kind === SyntaxKind.ComputedPropertyName;
if (node.kind !== SyntaxKind.ComputedPropertyName) {
return false;
}
if (hasModifier(node.parent, ModifierFlags.Abstract)) {
return true;
}
const containerKind = node.parent.parent.kind;
return containerKind === SyntaxKind.InterfaceDeclaration || containerKind === SyntaxKind.TypeLiteral;
}

export function isAbstractDeclarationName(node: Node) {
return isDeclarationName(node) && hasModifier(node, ModifierFlags.Abstract);
}

export function isExternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration & { moduleReference: ExternalModuleReference } {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,35 @@ import type { onInit } from "./framework-hooks";
interface Component {
[onInit]?(): void;
}

type T = {
[onInit]: any;
}

const o = {
[onInit]: 0 // Error
};

class C {
[onInit]: any; // Error (because class fields)
}

class D {
[onInit] = 0; // Error
}

class E {
[onInit]() {} // Error
}

abstract class F {
abstract [onInit](): void;
}

class G {
declare [onInit]: any;
}

declare class H {
[onInit]: any;
}

0 comments on commit a5ca492

Please sign in to comment.