| 
									
										
										
										
											2015-10-27 20:51:03 -07:00
										 |  |  | import {RuleFailure} from 'tslint/lib/lint'; | 
					
						
							|  |  |  | import {AbstractRule} from 'tslint/lib/rules'; | 
					
						
							|  |  |  | import {RuleWalker} from 'tslint/lib/language/walker'; | 
					
						
							| 
									
										
										
										
											2015-12-21 14:50:56 -08:00
										 |  |  | import * as ts from 'typescript'; | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 20:51:03 -07:00
										 |  |  | export class Rule extends AbstractRule { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |   public static FAILURE_STRING = "missing type declaration"; | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 20:51:03 -07:00
										 |  |  |   public apply(sourceFile: ts.SourceFile): RuleFailure[] { | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  |     const typedefWalker = new TypedefWalker(sourceFile, this.getOptions()); | 
					
						
							|  |  |  |     return this.applyWithWalker(typedefWalker); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 20:51:03 -07:00
										 |  |  | class TypedefWalker extends RuleWalker { | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  |   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) { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let ns = "<name missing>"; | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  |       if (name != null && name.kind === ts.SyntaxKind.Identifier) { | 
					
						
							|  |  |  |         ns = (<ts.Identifier>name).text; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (ns.charAt(0) === '_') return; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let failure = this.createFailure(location, 1, "expected parameter " + ns + " to have a type"); | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  |       this.addFailure(failure); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |