Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add optional type information to rules (#1323) #1363

Merged
merged 10 commits into from
Jul 11, 2016

Conversation

ScottSWu
Copy link
Contributor

@ScottSWu ScottSWu commented Jun 30, 2016

  • Changed Linter to accept an optional program object in the constructor
    • Added helper functions for creating a program using a tsconfig and getting relevant files
  • Created TypedRule class which receives the program object
    • Rules extending this class must implement applyWithProgram
    • Linter passes program to TypedRules if available
    • Calling apply on a TypedRule throws an error
  • Added requiresTypeInfo boolean to metadata
  • Created ProgramAwareRuleWalker which walks with the program / typechecker
  • Added cli options --project and --type-check
    • --project takes a tsconfig.json file
    • --type-check enables type checking and TypedRules, requires --project
  • Added an example restrictPlusOperands rule and tests that uses types


export abstract class TypedRule extends AbstractRule {
public apply(sourceFile: ts.SourceFile): RuleFailure[] {
return [];
Copy link
Contributor

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"

@ScottSWu ScottSWu force-pushed the typechecking-main branch 2 times, most recently from 8d2ece4 to ab31fc5 Compare July 1, 2016 18:53
@@ -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;
Copy link
Contributor

@alexeagle alexeagle Jul 6, 2016

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?

Copy link
Contributor

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.

Copy link
Contributor

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
@ScottSWu
Copy link
Contributor Author

Hi, sorry for the large PR. Could I get another review of the changes, and should I begin squashing commits?

@adidahiya
Copy link
Contributor

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 Linter class.

@@ -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
Copy link
Contributor

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@adidahiya
Copy link
Contributor

🎉

@adidahiya adidahiya merged commit 9248e05 into palantir:master Jul 11, 2016
@HamletDRC
Copy link
Contributor

HamletDRC commented Jul 12, 2016

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.

@myitcv
Copy link
Contributor

myitcv commented Jul 12, 2016

@ScottSWu @alexeagle et al - this is great work, thank you! Look forward to trying it out in the @next release

@Fank
Copy link

Fank commented Jul 12, 2016

@adidahiya Possible to release a new version, which contains these change, soon?

@adidahiya
Copy link
Contributor

@Fank @ScottSWu I'm attempting to cut a release here (#1395) but running into some compilation issues. I don't have time to dig into these right now but I'll try to get a release out for you guys ASAP (hopefully tonight or tomorrow)!

@adidahiya
Copy link
Contributor

Released v3.14.0-dev.0

@glen-84
Copy link
Contributor

glen-84 commented Aug 2, 2016

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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants