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

Add a fixer for "unnecessary-constructor" rule. #4694

Merged
merged 2 commits into from May 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/rules/unnecessaryConstructorRule.ts
Expand Up @@ -19,6 +19,7 @@ import { isConstructorDeclaration, isParameterProperty } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
import { Replacement } from "../language/rule/rule";

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
Expand Down Expand Up @@ -55,15 +56,28 @@ const isAccessRestrictingConstructor = (node: ts.ConstructorDeclaration): boolea
// If this has any modifier that isn't public, it's doing something
node.modifiers.some(modifier => modifier.kind !== ts.SyntaxKind.PublicKeyword);

const containsDecorator = (node: ts.ConstructorDeclaration): boolean =>
node.parameters.some(p => p.decorators !== undefined);

function walk(context: Lint.WalkContext) {
const callback = (node: ts.Node): void => {
if (
isConstructorDeclaration(node) &&
isEmptyConstructor(node) &&
!containsConstructorParameter(node) &&
!containsDecorator(node) &&
!isAccessRestrictingConstructor(node)
) {
context.addFailureAtNode(node, Rule.FAILURE_STRING);
const replacements = [];
// Since only one constructor implementation is allowed and the one found above is empty, all other overloads can be safely removed.
for (const maybeConstructor of node.parent.members) {
if (isConstructorDeclaration(maybeConstructor)) {
replacements.push(
Replacement.deleteFromTo(maybeConstructor.pos, maybeConstructor.end),
);
}
}
context.addFailureAtNode(node, Rule.FAILURE_STRING, replacements);
} else {
ts.forEachChild(node, callback);
}
Expand Down
69 changes: 69 additions & 0 deletions test/rules/unnecessary-constructor/test.ts.fix
@@ -0,0 +1,69 @@
export class ExportedClass {
}

class PublicConstructor {
}

class ProtectedConstructor {
protected constructor() { }
}

class PrivateConstructor {
private constructor() { }
}

class SameLine { }

class WithPrecedingMember {
public member: string;
}

class WithFollowingMethod {
public method() {}
}

const classExpression = class {
}

class ContainsContents {
constructor() {
console.log("Hello!");
}
}

class CallsSuper extends PublicConstructor {
constructor() {
super();
}
}

class ContainsParameter {
}

class PrivateContainsParameter {
private constructor(x: number) { }
}

class ProtectedContainsParameter {
protected constructor(x: number) { }
}

class ContainsParameterDeclaration {
constructor(public x: number) { }
}

class ContainsParameterAndParameterDeclaration {
constructor(x: number, public y: number) { }
}

class ConstructorOverload {
}

interface IConstructorSignature {
new(): {};
}

class DecoratedParameters {
constructor(@Optional x: number) {}
}

4 changes: 4 additions & 0 deletions test/rules/unnecessary-constructor/test.ts.lint
Expand Up @@ -79,4 +79,8 @@ interface IConstructorSignature {
new(): {};
}

class DecoratedParameters {
constructor(@Optional x: number) {}
}

[0]: Remove unnecessary empty constructor.