-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c31ad6f
Add tslint rules for #3994
weswigham 7813121
compile vs tslints services dts, null check lint
weswigham 1cd016b
Boolean trivia rule
weswigham dc9dd3e
Give up on real typechecking, just check literals
weswigham 0d88d8d
Simplify it a bit
weswigham 2793bc2
Feedback from PR, remove unused identifiers
weswigham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add tslint rules for #3994
- Loading branch information
commit c31ad6fb28e9daed09d585558edfc2f39ad4a3a7
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/// <reference path="../../lib/typescriptServices.d.ts" /> | ||
/// <reference path="../../node_modules/tslint/lib/tslint.d.ts" /> | ||
|
||
const OPTION_CATCH = "check-catch"; | ||
const OPTION_ELSE = "check-else"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static CATCH_FAILURE_STRING = "'catch' should be on the line following the previous block's ending curly brace"; | ||
public static ELSE_FAILURE_STRING = "'else' should be on the line following the previous block's ending curly brace"; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new NextLineWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class NextLineWalker extends Lint.RuleWalker { | ||
public visitIfStatement(node: ts.IfStatement) { | ||
const sourceFile = node.getSourceFile(); | ||
const thenStatement = node.thenStatement; | ||
|
||
const elseStatement = node.elseStatement; | ||
if (!!elseStatement) { | ||
// find the else keyword | ||
const elseKeyword = getFirstChildOfKind(node, ts.SyntaxKind.ElseKeyword); | ||
if (this.hasOption(OPTION_ELSE) && !!elseKeyword) { | ||
const thenStatementEndLoc = sourceFile.getLineAndCharacterOfPosition(thenStatement.getEnd()); | ||
const elseKeywordLoc = sourceFile.getLineAndCharacterOfPosition(elseKeyword.getStart()); | ||
if (thenStatementEndLoc.line !== (elseKeywordLoc.line - 1)) { | ||
const failure = this.createFailure(elseKeyword.getStart(), elseKeyword.getWidth(), Rule.ELSE_FAILURE_STRING); | ||
this.addFailure(failure); | ||
} | ||
} | ||
} | ||
|
||
super.visitIfStatement(node); | ||
} | ||
|
||
public visitTryStatement(node: ts.TryStatement) { | ||
const sourceFile = node.getSourceFile(); | ||
const catchClause = node.catchClause; | ||
|
||
// "visit" try block | ||
const tryKeyword = node.getChildAt(0); | ||
const tryBlock = node.tryBlock; | ||
const tryOpeningBrace = tryBlock.getChildAt(0); | ||
|
||
if (this.hasOption(OPTION_CATCH) && !!catchClause) { | ||
const tryClosingBrace = node.tryBlock.getChildAt(node.tryBlock.getChildCount() - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const catchKeyword = catchClause.getChildAt(0); | ||
const tryClosingBraceLoc = sourceFile.getLineAndCharacterOfPosition(tryClosingBrace.getEnd()); | ||
const catchKeywordLoc = sourceFile.getLineAndCharacterOfPosition(catchKeyword.getStart()); | ||
if (tryClosingBraceLoc.line !== (catchKeywordLoc.line - 1)) { | ||
const failure = this.createFailure(catchKeyword.getStart(), catchKeyword.getWidth(), Rule.CATCH_FAILURE_STRING); | ||
this.addFailure(failure); | ||
} | ||
} | ||
super.visitTryStatement(node); | ||
} | ||
} | ||
|
||
function getFirstChildOfKind(node: ts.Node, kind: ts.SyntaxKind) { | ||
return node.getChildren().filter((child) => child.kind === kind)[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/// <reference path="../../lib/typescriptServices.d.ts" /> | ||
/// <reference path="../../node_modules/tslint/lib/tslint.d.ts" /> | ||
|
||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static FAILURE_STRING = "LHS 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)); | ||
} | ||
} | ||
|
||
class InferrableTypeWalker extends Lint.RuleWalker { | ||
constructor(file: ts.SourceFile, opts: Lint.IOptions, private program: ts.Program) { | ||
super(program.getSourceFile(file.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)); | ||
} | ||
}); | ||
|
||
super.visitVariableStatement(node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"noImplicitAny": true, | ||
"module": "commonjs", | ||
"outDir": "../../built/local/tslint" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a
getFirstToken()