Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix callable-types should handle new(): T just like (): T #4352

Merged
merged 9 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/language/formatter/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export interface IFormatterMetadata {

export type ConsumerType = "human" | "machine";

// See https://github.com/palantir/tslint/issues/4364
// TODO: Enable the `callable-types` rule below. Currently, it
// produces a lint error in several locations complaining about the
// `no-inferred-empty-object-type` rule.
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
export interface FormatterConstructor {
// tslint:disable-next-line:callable-types
new (): IFormatter;
}

Expand Down
5 changes: 3 additions & 2 deletions src/rules/callableTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import {
getChildOfKind,
isCallSignatureDeclaration,
isConstructSignatureDeclaration,
isIdentifier,
isInterfaceDeclaration,
isTypeLiteralNode,
Expand Down Expand Up @@ -58,7 +59,7 @@ function walk(ctx: Lint.WalkContext<void>) {
) {
const member = node.members[0];
if (
isCallSignatureDeclaration(member) &&
(isConstructSignatureDeclaration(member) || isCallSignatureDeclaration(member)) &&
// avoid bad parse
member.type !== undefined
) {
Expand Down Expand Up @@ -96,7 +97,7 @@ function noSupertype(node: ts.InterfaceDeclaration): boolean {
}

function renderSuggestion(
call: ts.CallSignatureDeclaration,
call: ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration,
parent: ts.InterfaceDeclaration | ts.TypeLiteralNode,
sourceFile: ts.SourceFile,
): string {
Expand Down
4 changes: 3 additions & 1 deletion test/rules/callable-types/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ interface K {
}

// handle modifiers
export type I = () => void;
export type I0 = () => void;

export type I1 = new () => void;

export type T = () => void

9 changes: 7 additions & 2 deletions test/rules/callable-types/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ interface K {
}

// handle modifiers
export interface I {
export interface I0 {
(): void;
~~~~~~~~~ [interface % ('type I = () => void;')]
~~~~~~~~~ [interface % ('type I0 = () => void;')]
}

export interface I1 {
new (): void;
~~~~~~~~~~~~~ [interface % ('type I1 = new () => void;')]
}

export type T = {
Expand Down