Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tslint rules for #3994 #4458

Merged
merged 6 commits into from
Sep 18, 2015
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Give up on real typechecking, just check literals
  • Loading branch information
weswigham committed Aug 26, 2015
commit dc9dd3e667d9d99a487741f4633c7caad8df9515
46 changes: 34 additions & 12 deletions scripts/tslint/noInferrableTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,52 @@


export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "LHS type inferred by RHS expression, remove type annotation";
public static FAILURE_STRING_FACTORY = (type: string) => `LHS type (${type}) inferred by RHS expression, remove type annotation`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const program = ts.createProgram([sourceFile.fileName], Lint.createCompilerOptions());
return this.applyWithWalker(new InferrableTypeWalker(sourceFile, this.getOptions(), program));
return this.applyWithWalker(new InferrableTypeWalker(sourceFile, this.getOptions()));
}
}

class InferrableTypeWalker extends Lint.RuleWalker {
constructor(file: ts.SourceFile, opts: Lint.IOptions, private program: ts.Program) {
super(program.getSourceFile(file.fileName), opts);
private originalService: ts.LanguageService;
private fileName: string;
private originalContent: string;

constructor(file: ts.SourceFile, opts: Lint.IOptions) {
this.fileName = file.fileName;
this.originalContent = file.getFullText();
this.originalService = ts.createLanguageService(Lint.createLanguageServiceHost(this.fileName, this.originalContent));
super(this.originalService.getSourceFile(this.fileName), opts);
}

visitVariableStatement(node: ts.VariableStatement) {
node.declarationList.declarations.forEach(e => {
if (
(!!e.type) &&
(!!e.initializer) &&
(this.program.getTypeChecker().getTypeAtLocation(e.type) === this.program.getTypeChecker().getContextualType(e.initializer))
) {
this.addFailure(this.createFailure(e.type.getStart(), e.type.getWidth(), Rule.FAILURE_STRING));
if ((!!e.type) && (!!e.initializer)) {
let failure: string;
switch (e.type.kind) {
case ts.SyntaxKind.BooleanKeyword:
if (e.initializer.kind === ts.SyntaxKind.TrueKeyword || e.initializer.kind === ts.SyntaxKind.FalseKeyword) {
failure = 'boolean';
}
break;
case ts.SyntaxKind.NumberKeyword:
if (e.initializer.kind === ts.SyntaxKind.NumericLiteral) {
failure = 'number';
}
break;
case ts.SyntaxKind.StringKeyword:
if (e.initializer.kind === ts.SyntaxKind.StringLiteral || e.initializer.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider TemplateExpression as well

failure = 'string';
}
break;
}
if (failure) {
this.addFailure(this.createFailure(e.type.getStart(), e.type.getWidth(), Rule.FAILURE_STRING_FACTORY(failure)));
}
}
});

super.visitVariableStatement(node);
}
}