镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Add compiler option to parse in strict mode #10758

Description

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.

Activity

  1. T18970237136 commented on Sep 7, 2016

    @T18970237136
    Contributor

    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!

  2. aluanhaddad commented on Sep 11, 2016

    @aluanhaddad
    Contributor

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

  3. added this to the milestone on Sep 28, 2016
  4. RyanCavanaugh commented on Sep 28, 2016

    @RyanCavanaugh
    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?

  5. T18970237136 commented on Sep 29, 2016

    @T18970237136
    Contributor

    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

    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!

  6. added 3 commits that reference this issue on Oct 9, 2016
    29a85e0
    ea808f5
    8210634
  7. mhegazy commented on Oct 14, 2016

    @mhegazy
    ContributorAuthor
  8. mhegazy commented on Oct 14, 2016

    @mhegazy
    ContributorAuthor

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

  9. removed this from the milestone on Apr 26, 2018
  10. locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions