diff --git a/src/rules/unnecessaryConstructorRule.ts b/src/rules/unnecessaryConstructorRule.ts index f060c617da8..cb7d1ff0b74 100644 --- a/src/rules/unnecessaryConstructorRule.ts +++ b/src/rules/unnecessaryConstructorRule.ts @@ -45,22 +45,23 @@ export class Rule extends Lint.Rules.AbstractRule { const isEmptyConstructor = (node: ts.ConstructorDeclaration): boolean => node.body !== undefined && node.body.statements.length === 0; -const containsConstructorParameter = (node: ts.ConstructorDeclaration): boolean => { - for (const parameter of node.parameters) { - if (isParameterProperty(parameter)) { - return true; - } - } +const containsConstructorParameter = (node: ts.ConstructorDeclaration): boolean => + // If this has any parameter properties + node.parameters.some(isParameterProperty); - return false; -}; +const isAccessRestrictingConstructor = (node: ts.ConstructorDeclaration): boolean => + // No modifiers implies public + node.modifiers != undefined && + // If this has any modifier that isn't public, it's doing something + node.modifiers.some(modifier => modifier.kind !== ts.SyntaxKind.PublicKeyword); function walk(context: Lint.WalkContext) { const callback = (node: ts.Node): void => { if ( isConstructorDeclaration(node) && isEmptyConstructor(node) && - !containsConstructorParameter(node) + !containsConstructorParameter(node) && + !isAccessRestrictingConstructor(node) ) { context.addFailureAtNode(node, Rule.FAILURE_STRING); } else { diff --git a/test/rules/unnecessary-constructor/test.ts.lint b/test/rules/unnecessary-constructor/test.ts.lint index 9e700464694..ff619f7cc6f 100644 --- a/test/rules/unnecessary-constructor/test.ts.lint +++ b/test/rules/unnecessary-constructor/test.ts.lint @@ -10,12 +10,10 @@ class PublicConstructor { class ProtectedConstructor { protected constructor() { } - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] } class PrivateConstructor { private constructor() { } - ~~~~~~~~~~~~~~~~~~~~~~~~~ [0] } class SameLine { constructor() { } } @@ -55,6 +53,14 @@ class ContainsParameter { ~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] } +class PrivateContainsParameter { + private constructor(x: number) { } +} + +class ProtectedContainsParameter { + protected constructor(x: number) { } +} + class ContainsParameterDeclaration { constructor(public x: number) { } }