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

Add compiler option to parse in strict mode #10758

Closed
mhegazy opened this issue Sep 7, 2016 · 6 comments
Closed

Add compiler option to parse in strict mode #10758

mhegazy opened this issue Sep 7, 2016 · 6 comments
Labels
Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Sep 7, 2016

See #9449 and #10755 for reference.

The ts code base is not modules, so it is not implicitly parsed in strict mode. we would like it to be. options either 1. put "use strict" at the top of every file, and make the emitter remove the duplicate ones when it merges the output. or 2. add a compiler flag that would force the compiler to parse the files in strict mode.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Sep 7, 2016
@T18970237136
Copy link
Contributor

T18970237136 commented Sep 7, 2016

Hi,
for a software product at work, we currently use a fork of TypeScript that has a --forceStrictMode compiler option which forces the compiler to parse in strict mode, without affecting the emit. The use case is a similar one:

The use case for this is that some JS Runtimes like Jurassic (which allows to run JavaScripts in a .NET environment) have an option to enable strict mode even if the JS did not declare a strict mode directive, and enabling strict mode slightly improves performance (and changes some semantics). Then, if you present an editor like Monaco to the user where he can edit TypeScripts that are to be run on such a JS Runtime later with strict mode, the editor's TypeScript Language Service can correctly identify syntax errors in strict mode even if the script does not contain the strict mode directive.

Note that enabling this option will not have any impact on the output, so if the TS file did not contain "use strict";, neither will the JS output file. It will only have an impact on the compiler during syntax check (to emit errors e.g. like using reserved words as identifiers, using the delete operator on identifiers, using old-style octal literals etc).

If this is integrated directly into the TS repo, we could abandon the fork and use the original TS Services lib instead.

Thank you!

@aluanhaddad
Copy link
Contributor

👍 I would really like to see "use strict"; emitted

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Sep 28, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Sep 28, 2016
@RyanCavanaugh
Copy link
Member

Accepting PRs for a new flag/setting --alwaysStrict (perhaps open to bikeshedding on this point) which:

  • Always parses all files in the "strict" context (as if you had written "use strict;" at the top of the file)
  • Always emits "use strict;" at the top of output files which don't already include it at the top

@T18970237136 perhaps you could turn your fork into a PR?

@T18970237136
Copy link
Contributor

T18970237136 commented Sep 29, 2016

Note, that a feature request for the same behavior (compiler option that parses in strict mode and also emits "use strict";) was already declined some time ago: #7209 (Suggestion: Add a "forceStrictMode" compiler option) 😉

👎 write "use strict"; yourself + TSLint if appropriate

@RyanCavanaugh Note that my fork currently only implements the first part: parse source files in strict mode. It does not affect the emit, so it will not emit "use strict"; on file which did not have this directive. (This is because in our use case the "use strict"; was not needed, and unfortunately I have no knowledge about the new transformation-based emitter, and how to ensure "use strict" is always emitted.)
Thanks!

slawomir added a commit to slawomir/TypeScript that referenced this issue Oct 9, 2016
 * add compiler option alwaysStrict
 * compile in strict mode when option is set
 * emit "use strict"
slawomir added a commit to slawomir/TypeScript that referenced this issue Oct 10, 2016
 * add unit test to ensure "use strict" is not added twice
 * fix code
slawomir added a commit to slawomir/TypeScript that referenced this issue Oct 10, 2016
@mhegazy
Copy link
Contributor Author

mhegazy commented Oct 14, 2016

thanks @slawomir!

@mhegazy
Copy link
Contributor Author

mhegazy commented Oct 14, 2016

The new option is --alwaysStrict should be available in typescript@next later tonight.

@mhegazy mhegazy removed this from the Community milestone Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
@RyanCavanaugh RyanCavanaugh moved this from Accepting PRs to Needs More Info in Suggestion Backlog Triage Jan 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
4 participants