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

Allow some per file compiler options #49886

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Jul 12, 2022

In this PR, we allow certain compiler options to be configured via comments within a file for the scope of that file. This means you can write, for example:
compiler-option-pragmas

Fixes #31035

I've wanted to look into this since I originally added the generic pragma-parsing infrastructure for @ts-check and related comments.

To start, I'm aiming to allow every compiler option in the "Strictness" category as a per-file configurable option. That way you can toss a // @ts-strict into a (potentially javascript) file and get strict mode checking in it. Currently in this PR, every option in that category is supported except for strictNullChecks and exactOptionalPropertyTypes - they do some global type-identity configuration that's taking me a bit longer to abstract away into a per-file configuration. (Also strictNullChecks just straight up is referenced more than any other compiler option.)

To start off the discussion, in the linked issue, @RyanCavanaugh asked:

It's not really even clear what this means - if you have a type declared in file A, another type in file B, and you try to relate them in file C, whose value of strictFunctionTypes is in play?

Currently in this PR, whichever is stricter. If either file the types originated from has it set, it's related strictly. Could go the other way and relate loosely if either is explicitly non-strict if looser checking is more preferable. Honestly, most of these "which context controls the flag" already have fairly canonical answers, either because we already look up a context to check JS-y-ness (for JS-file-specific-behaviors or error ignoring), or to issue an error in (in which case the error node location is a pretty logical context).

Anyways, I'm hoping this implementation can jumpstart discussion on the value of in-file pragmas for compiler options, specifically with the goal of bikeshedding the syntax and what options we should ultimately expose on a per-file basis. (Technically, I've tried to make the internal APIs for adding new per-file pragmas very type-safe and very easy to use.)

TODO list:

  • File-local support for strictNullChecks (Per file option: strictNullChecks #50347 for separate review comments, since it's big)
  • File-local support for exactOptionalPropertyTypes
  • Errors on conflicting file-local options/redeclarations (silently uses the last declaration and silently discards invalid arguments right now)
  • Copious testing (thus far I've just been prototyping in a sample project)

@weswigham
Copy link
Member Author

weswigham commented Jul 12, 2022

@typescript-bot pack this

@typescript-bot typescript-bot added the For Uncommitted Bug label Jul 12, 2022
@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 12, 2022

Heya @weswigham, I've started to run the tarball bundle task on this PR at 459196b. You can monitor the build here.

@weswigham
Copy link
Member Author

weswigham commented Jul 13, 2022

@typescript-bot pack this now that baselines should be green again :)

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 13, 2022

Heya @weswigham, I've started to run the tarball bundle task on this PR at 97bba40. You can monitor the build here.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 13, 2022

Is this opt-in only, or will it be possible to opt-out as well?

@weswigham
Copy link
Member Author

weswigham commented Jul 13, 2022

You should be able to write things like // @ts-strict false and the like. Parameters get parsed the same way they do on the command line.

@weswigham
Copy link
Member Author

weswigham commented Jul 13, 2022

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 13, 2022

Heya @weswigham, I've started to run the tarball bundle task on this PR at ef3813c. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 13, 2022

Hey @weswigham, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/129711/artifacts?artifactName=tgz&fileId=07F2813C225AA6CC371299076DC3BF08FFE79A5C45BC00165AB9D270F70FC3FB02&fileName=/typescript-4.8.0-insiders.20220713.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@4.8.0-pr-49886-8".;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants