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

Add the option allow-empty-functions in no-empty #3624

Merged
merged 1 commit into from
Apr 18, 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
35 changes: 31 additions & 4 deletions src/rules/noEmptyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import * as ts from "typescript";
import * as Lint from "../index";

const ALLOW_EMPTY_CATCH = "allow-empty-catch";
const ALLOW_EMPTY_FUNCTIONS = "allow-empty-functions";

interface Options {
allowEmptyCatch: boolean;
allowEmptyFunctions: boolean;
}

export class Rule extends Lint.Rules.AbstractRule {
Expand All @@ -34,12 +36,29 @@ export class Rule extends Lint.Rules.AbstractRule {
descriptionDetails: "Blocks with a comment inside are not considered empty.",
rationale: "Empty blocks are often indicators of missing code.",
optionsDescription: Lint.Utils.dedent`
If \`${ALLOW_EMPTY_CATCH}\` is specified, then catch blocks are allowed to be empty.`,
If \`${ALLOW_EMPTY_CATCH}\` is specified, then catch blocks are allowed to be empty.
If \`${ALLOW_EMPTY_FUNCTIONS}\` is specified, then function definitions are allowed to be empty.`,
options: {
type: "string",
enum: [ALLOW_EMPTY_CATCH],
type: "array",
items: {
anyOf: [
{
type: "string",
enum: [ALLOW_EMPTY_CATCH],
},
{
type: "string",
enum: [ALLOW_EMPTY_FUNCTIONS],
},
],
},
},
optionExamples: [true, [true, ALLOW_EMPTY_CATCH]],
optionExamples: [
true,
[true, ALLOW_EMPTY_CATCH],
[true, ALLOW_EMPTY_FUNCTIONS],
[true, ALLOW_EMPTY_CATCH, ALLOW_EMPTY_FUNCTIONS],
],
type: "functionality",
typescriptOnly: false,
};
Expand All @@ -50,6 +69,7 @@ export class Rule extends Lint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, {
allowEmptyCatch: this.ruleArguments.indexOf(ALLOW_EMPTY_CATCH) !== -1,
allowEmptyFunctions: this.ruleArguments.indexOf(ALLOW_EMPTY_FUNCTIONS) !== -1,
});
}
}
Expand All @@ -76,6 +96,13 @@ function isExcluded(node: ts.Node, options: Options): boolean {
return true;
}

if (options.allowEmptyFunctions &&
(node.kind === ts.SyntaxKind.FunctionDeclaration ||
node.kind === ts.SyntaxKind.FunctionExpression ||
node.kind === ts.SyntaxKind.ArrowFunction)) {
return true;
}

return isConstructorDeclaration(node) &&
(
/* If constructor is private or protected, the block is allowed to be empty.
Expand Down
10 changes: 8 additions & 2 deletions test/rules/no-empty/allow-empty-catch/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ if (x === 2) {
}
~ [block is empty]

function testFunction() {
~
function testFunction1() {
~

~nil
}
~ [block is empty]

const testFunction2 = () => { };
~~~ [block is empty]

const testFunction3 = function() {}
~~ [block is empty]

for (var x = 0; x < 1; ++x) { }
~~~ [block is empty]

Expand Down
73 changes: 73 additions & 0 deletions test/rules/no-empty/allow-empty-functions/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
if (x === 1) {}
~~ [block is empty]
if (x === 2) {
~

~nil

~nil
}
~ [block is empty]

function testFunction1() {



}

const testFunction2 = () => { };

const testFunction3 = function() {}

for (var x = 0; x < 1; ++x) { }
~~~ [block is empty]

// empty blocks with comments should be legal
for (var y = 0; y < 1; ++y) {
// empty here
}
{ // empty block allowed
}
{
/* this block is also empty, but allowed to be */
}

class testClass {
constructor(private allowed: any, private alsoAllowed: any) {
}
}

class testClass2 {
constructor(protected allowed: any) {
}
}

class testClass3 {
constructor(notAllowed: any) {
~
}
~~~~~ [block is empty]
}

class testClass4 {
constructor(readonly allowed: any) {
}
}

class PrivateClassConstructor {
private constructor() {}
}

class ProtectedClassConstructor {
protected constructor() {}
}

class PublicClassConstructor {
public constructor() {}
~~ [block is empty]
}

try {
throw new Error();
} catch (error) {}
~~ [block is empty]
5 changes: 5 additions & 0 deletions test/rules/no-empty/allow-empty-functions/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-empty": [true, "allow-empty-functions"]
}
}
10 changes: 8 additions & 2 deletions test/rules/no-empty/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ if (x === 2) {
}
~ [block is empty]

function testFunction() {
~
function testFunction1() {
~

~nil
}
~ [block is empty]

const testFunction2 = () => { };
~~~ [block is empty]

const testFunction3 = function() {}
~~ [block is empty]

for (var x = 0; x < 1; ++x) { }
~~~ [block is empty]

Expand Down