feat(build): require parameter types

Fixes #2833
This commit is contained in:
Alex Eagle
2015-07-07 20:03:00 -07:00
parent 6d760666a9
commit de18da2a0d
81 changed files with 379 additions and 290 deletions
+38
View File
@@ -0,0 +1,38 @@
/// <reference path="../../node_modules/typescript/bin/typescriptServices.d.ts" />
/// <reference path="../../node_modules/gulp-tslint/node_modules/tslint/lib/tslint.d.ts" />
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "missing type declaration";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const typedefWalker = new TypedefWalker(sourceFile, this.getOptions());
return this.applyWithWalker(typedefWalker);
}
}
class TypedefWalker extends Lint.RuleWalker {
public visitMethodDeclaration(node: ts.MethodDeclaration) {
if (node.name.getText().charAt(0) !== '_') {
node.parameters.forEach((p: ts.ParameterDeclaration) => {
// a parameter's "type" could be a specific string value, for example `fn(option:
// "someOption", anotherOption: number)`
if (p.type == null || p.type.kind !== ts.SyntaxKind.StringLiteral) {
this.checkTypeAnnotation(p.getEnd(), <ts.TypeNode>p.type, p.name);
}
});
}
super.visitMethodDeclaration(node);
}
private checkTypeAnnotation(location: number, typeAnnotation: ts.TypeNode, name?: ts.Node) {
if (typeAnnotation == null) {
let ns = "<name missing>";
if (name != null && name.kind === ts.SyntaxKind.Identifier) {
ns = (<ts.Identifier>name).text;
}
if (ns.charAt(0) === '_') return;
let failure = this.createFailure(location, 1, "expected parameter " + ns + " to have a type");
this.addFailure(failure);
}
}
}
+3 -4
View File
@@ -40,21 +40,20 @@ class TypedefWalker extends Lint.RuleWalker {
}
private handleCallSignature(node: ts.SignatureDeclaration) {
const location = (node.parameters != null) ? node.parameters.end : null;
// set accessors can't have a return type.
if (node.kind !== ts.SyntaxKind.SetAccessor) {
this.checkTypeAnnotation(location, node.type, node.name);
this.checkTypeAnnotation(node.type, node.name);
}
}
private checkTypeAnnotation(location: number, typeAnnotation: ts.TypeNode, name?: ts.Node) {
private checkTypeAnnotation(typeAnnotation: ts.TypeNode, name?: ts.Node) {
if (typeAnnotation == null) {
let ns = "<name missing>";
if (name != null && name.kind === ts.SyntaxKind.Identifier) {
ns = (<ts.Identifier>name).text;
}
if (ns.charAt(0) === '_') return;
let failure = this.createFailure(location, 1, "expected " + ns + " to have a return type");
let failure = this.createFailure(null, 1, "expected " + ns + " to have a return type");
this.addFailure(failure);
}
}