-
Notifications
You must be signed in to change notification settings - Fork 885
Add optional type information to rules #1323
Comments
Sure, adding a CLI option to point to a A lot of people are very interested in #680. Would you mind writing up a slightly longer proposal of how you plan to approach this? How will you construct a Re: backcompat -- a new Rule method makes sense, but its API probably needs to be more clear than simply "applyWithProgram" (because a walker is also involved). One alternative might be to make a new base class that extends |
Sure, creating the
Some other changes tried in this fork:
|
cc @myitcv and @alexeagle who have been involved in this discussion previously |
thanks @jkillian - actually @ScottSWu and I are working together on this proposal. |
Did @ScottSWu 's update address the design questions about this? We'd like to get a PR going soon to move our work upstream. Would you like a longer design/proposal? |
Sorry for the lack of response @alexeagle and @ScottSWu. I had thought that you were putting this on hold until we saw what became of microsoft/TypeScript#9038. I'll take a more detailed look at your proposal and work so far @ScottSWu either this weekend or early next week |
I like your proposal @ScottSWu, it seems like a smooth way to add the feature that doesn't cause breaking changes. A couple quick thoughts:
|
const {config, error} = ts.readConfigFile("tsconfig.json", ts.sys.readFile);
const parsed = ts.parseJsonConfigFileContent(config, {readDirectory: ts.sys.readDirectory}, path);
const host = ts.createCompilerHost(parsed.options, true);
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
const fileNames = program.getSourceFiles().map(s => s.fileName).filter(l => l.substr(5) !== ".d.ts");
|
It seems a little bit complex, although an example in the docs would obviously help with that a lot. Would you anticipate people using the exact same code every time essentially? If so, it seems that we could at least optionally abstract things for API users. |
It would probably be the same for projects that do not already compile through the typescript API. Should the Linter have some sort of |
Our thinking is that most developer workflows that call tslint also do type-checking. Type-checking takes something like 3x the time of linting, so it would be better not to type-check the program twice. Ideally I think that programs that use the proposed Linter API that accepts a For example, API users like gulp/grunt plugins would get the When/if the https://github.com/Microsoft/TypeScript/tree/extensibility-model branch is merged, we'll have an easy time explaining how those lint checks can be added into the
|
That makes sense, seems like a sound proposal. @ScottSWu I think it's a good idea to have a convenience function included, but the |
Going back to the command line options, other discussions seem to point towards a |
Let's enable it by default but have an opt-out flag? It seems that it's a worthwhile feature that most people will end up wanting to use, but we don't want to force users to take on negative performance problems. Just an idea though, I could be persuaded either way |
I would prefer type checking to be opt-in at first, at least until the next major version. We should let it bake for a little while before flipping the switch to make it a default. Also there might be some users who want TSLint to only do syntactic checking (maybe as a philosophical thing). |
Addresses #1323. * 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 `TypedRule`s 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 `TypedRule`s, requires `--project` * Added an example `restrictPlusOperands` rule and tests that uses types
Should #680 be closed as well? |
We would like to add type information to nodes and give rules access to the type checker, thus resolving #680. To maintain compatibility with current rules, a tentative design is to accept a ts.Program object in the Linter, and pass that to rules that implement an "applyWithProgram" function. A PR is in the works for this feature.
From the cli, constructing the program would require tsconfig for compiler options. Would it be possible to add an argument in the TSLint cli for the config location and/or whether or not to enable type checking?
The text was updated successfully, but these errors were encountered: