-
Notifications
You must be signed in to change notification settings - Fork 885
Add optional type information to rules (#1323) #1363
Conversation
|
||
export abstract class TypedRule extends AbstractRule { | ||
public apply(sourceFile: ts.SourceFile): RuleFailure[] { | ||
return []; |
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.
Add a comment about the behavior here, like "Silently ignore this rule if tslint is executed without a program, as the rule requires type-check information"
8d2ece4
to
ab31fc5
Compare
@@ -20,8 +20,8 @@ import {IOptions} from "../../lint"; | |||
import {RuleWalker} from "./ruleWalker"; | |||
|
|||
export class ProgramAwareRuleWalker extends RuleWalker { | |||
protected program: ts.Program; | |||
protected typeChecker: ts.TypeChecker; | |||
private program: ts.Program; |
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.
@jkillian is there a reason your style preference is for field declaration rather than a private
modifier on the constructor params?
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.
historically we've tended towards that code style so it's easy to locate all the fields in one place. we've loosened up the guidelines a bit; I don't really mind using constructor param properties.
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.
Let's go ahead and use parameter properties where possible 👍
In fact, just merged #1373 which does this in a few locations
* --project specifies the tsconfig.json file * --type-check enables the type checker and TypedRules * If files are specified as arguments with a tsconfig, only files will be linted * Added exit code checks for new cli options
* Calling apply on a TypedRule throws an Error * restrictPlusOperands * Added additional tests * Removed extraneous constructor * Updated failure messages * Fixed nits, descriptions, newlines, template strings * Added jsdoc comments to Linter methods * Changed cli tests to accommodate changes
8ee37c9
to
5c17efc
Compare
Hi, sorry for the large PR. Could I get another review of the changes, and should I begin squashing commits? |
Sweet, this looks pretty good @ScottSWu! I think I'll just squash + merge at the end into one commit and take your PR description as the commit message body. One last thing -- can you add documentation to the README about how to use these new APIs? This includes the new CLI options and static methods on the |
@@ -115,6 +115,8 @@ Options: | |||
-e, --exclude exclude globs from path expansion | |||
-t, --format output format (prose, json, verbose, pmd, msbuild, checkstyle) [default: "prose"] | |||
--test test that tslint produces the correct output for the specified directory | |||
--project tsconfig.json file |
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.
path to tsconfig.json file
@@ -214,6 +224,21 @@ const linter = new Linter(fileName, fileContents, options); | |||
const result = linter.lint(); | |||
``` | |||
|
|||
#### Type Checking | |||
|
|||
To enable rules that work with the type checker, a TypeScript program object may be passed to the linter. Helper functions are provided to create a program from a `tsconfig.json` file. A project directory can be specified if project files do not lie in the same directory as the `tsconfig.json` file. |
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.
This should probably be edited to "... a TypesScript program [must] be passed to the linter [when using the programmatic API]." (my additions in brackets)
Also it would be good to clarify that a program is automatically created for you when using the appropriate CLI flags.
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.
Fixed, and added a line below the code sample regarding the CLI.
🎉 |
Will the grunt-tslint plugin need to be updated before grunt users can use this? Thank you @ScottSWu . There are many users of microsoft-tslint-contrib that really have been wanting this. |
@ScottSWu @alexeagle et al - this is great work, thank you! Look forward to trying it out in the |
@adidahiya Possible to release a new version, which contains these change, soon? |
Released v3.14.0-dev.0 |
Should it not silently fail if you don't enable type checking? This would allow you to see all the other errors/warnings in an editor, even when you haven't enabled type checking (which won't always be possible or easy to do). |
Linter
to accept an optional program object in the constructorTypedRule
class which receives the program objectapplyWithProgram
Linter
passes program toTypedRule
s if availableapply
on aTypedRule
throws an errorrequiresTypeInfo
boolean to metadataProgramAwareRuleWalker
which walks with the program / typechecker--project
and--type-check
--project
takes atsconfig.json
file--type-check
enables type checking andTypedRule
s, requires--project
restrictPlusOperands
rule and tests that uses types