-
Notifications
You must be signed in to change notification settings - Fork 199
/
preferDeclareFunctionRule.ts
36 lines (31 loc) · 1.08 KB
/
preferDeclareFunctionRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as Lint from "tslint";
import * as ts from "typescript";
import { eachModuleStatement, failure } from "../util";
export class Rule extends Lint.Rules.AbstractRule {
static metadata: Lint.IRuleMetadata = {
ruleName: "prefer-declare-function",
description: "Forbids `export const x = () => void`.",
optionsDescription: "Not configurable.",
options: null,
type: "style",
typescriptOnly: true,
};
static FAILURE_STRING = failure(
Rule.metadata.ruleName,
"Use a function declaration instead of a variable of function type."
);
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>): void {
eachModuleStatement(ctx.sourceFile, (statement) => {
if (ts.isVariableStatement(statement)) {
for (const varDecl of statement.declarationList.declarations) {
if (varDecl.type !== undefined && varDecl.type.kind === ts.SyntaxKind.FunctionType) {
ctx.addFailureAtNode(varDecl, Rule.FAILURE_STRING);
}
}
}
});
}