Repository navigation
Add compiler option to parse in strict mode #10758
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Sep 7, 2016 Hi,
for a software product at work, we currently use a fork of TypeScript that has a--forceStrictModecompiler 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 thedeleteoperator 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 commented
on Sep 11, 2016 ContributorMore actions👍 I would really like to see
"use strict";emittedReacted by Basarat Ali SyedReacted by Basarat Ali Syed- addedHelp WantedYou can do thisYou can do thisand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Sep 28, 2016 - added this to the This milestone has been deleted milestone
on Sep 28, 2016 RyanCavanaugh commented
on Sep 28, 2016 MemberMore actionsAccepting 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?
Reacted by Adam C. Abernathy and garkinNote, 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
Ryan Cavanaugh (@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!- added 3 commits that reference this issue
on Oct 9, 2016 mhegazy commented
on Oct 14, 2016 ContributorAuthorMore actionsthanks Sławomir Sadziak (@slawomir)!
mhegazy commented
on Oct 14, 2016 ContributorAuthorMore actionsThe new option is
--alwaysStrictshould be available intypescript@nextlater tonight.Reacted by Sławomir Sadziak, Whymarrh Whitby, Leon Adler, Fabio Spampinato and Brian Caruso- removed this from the This milestone has been deleted milestone
on Apr 26, 2018 - locked and limited conversation to collaborators
on Jul 31, 2018
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.