(sourceFile: ts.SourceFile, program: ts.Program)
| 23 | */ |
| 24 | export class Rule extends TypedRule { |
| 25 | override applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] { |
| 26 | const checker = program.getTypeChecker(); |
| 27 | |
| 28 | return this.applyWithFunction(sourceFile, (ctx) => { |
| 29 | for (const st of sourceFile.statements) { |
| 30 | if ( |
| 31 | !ts.isVariableStatement(st) || |
| 32 | !(st.modifiers ?? []).some((s) => s.kind === ts.SyntaxKind.ExportKeyword) |
| 33 | ) { |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | for (const decl of st.declarationList.declarations) { |
| 38 | if ( |
| 39 | decl.initializer !== undefined && |
| 40 | ts.isCallExpression(decl.initializer) && |
| 41 | decl.type === undefined |
| 42 | ) { |
| 43 | const inferredType = checker.getTypeAtLocation(decl.name); |
| 44 | const typeStr = checker.typeToString(inferredType, decl); |
| 45 | |
| 46 | ctx.addFailureAtNode( |
| 47 | decl, |
| 48 | 'No explicit type. Inferred types can cause unexpected issues. ' + |
| 49 | 'Please add an explicit type.', |
| 50 | Replacement.appendText(decl.name.end, `: ${typeStr}`), |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | } |
nothing calls this directly
no test coverage detected