Skip to content

Warn when a subset of input files have "use strict" with --outFile #11464

Closed
@mattmccutchen

Description

@mattmccutchen

As per #4270, the supported way to control strict mode when not using ES6 modules is to put "use strict"; at the top of the file. However, when the --outFile option is used to concatenate the emitted code for the input files into a single output file, the strict mode of the output file ends up being controlled by the first input file; attempting to enable strict mode on some input files and not others does not work. The compiler could warn if a user tries this by mistake.

TypeScript Version: 2.0.3

Code

// a.ts
"use strict";
var a = 1;
// b.ts
var b = 2;

tsc a.ts b.ts --outFile ab.js

Expected behavior: Semantic error: "Cannot enable strict mode on a subset of input files when using --outFile" or similar.

Actual behavior: No warning and the following output:

"use strict";
var a = 1;
var b = 2;

So strict mode is enabled on both input files, or neither if the order is reversed.

Activity

RyanCavanaugh

RyanCavanaugh commented on Oct 10, 2016

@RyanCavanaugh
Member

I think #10758 would handle your use case? It's likely some people want to only have to write "use strict" at the top file in concatenated output, so I wouldn't want to break them.

mattmccutchen

mattmccutchen commented on Oct 10, 2016

@mattmccutchen
ContributorAuthor

I think #10758 would handle your use case?

Indeed. Thanks for the reference; I'm not having much success finding these existing issues myself. :(

It's likely some people want to only have to write "use strict" at the top file in concatenated output, so I wouldn't want to break them.

If a user does that, then all of the other files will use strict mode for the eventual parsing and execution by the JavaScript engine, but not for the parsing by the TypeScript compiler. Do you think that's a reasonable thing for a user to want? I don't think the inconsistency is reasonable.

added
SuggestionAn idea for TypeScript
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
and removed
Needs More InfoThe issue still hasn't been fully clarified
on Oct 10, 2016
mattmccutchen

mattmccutchen commented on Dec 5, 2016

@mattmccutchen
ContributorAuthor

I believe that while this is still a valid suggestion in principle, it almost certainly won't be worth implementing now that --alwaysStrict is implemented (note, not yet released), so closing. Comment if you disagree.

locked and limited conversation to collaborators on Jun 19, 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

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @RyanCavanaugh@mattmccutchen

        Issue actions

          Warn when a subset of input files have "use strict" with --outFile · Issue #11464 · microsoft/TypeScript