Skip to content

'--alwaysStrict' option should not 'use strict' declaration if setting with module=es2015 #11806

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

Closed
tetsuharuohzeki opened this issue Oct 24, 2016 · 6 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@tetsuharuohzeki
Copy link
Contributor

Env

  • TypeScript Version 2.1.0-dev.20161023
  • with enable --alwaysStrict & --module=es2015 option

Code

Compile the below code with the above options.

export const a = 1;

Actual behavior

"use strict";
export var a = 1;

Expected behavior

I think "use strict" declareation is needless for ES2015 module because "Module code is always strict mode code" by the spec.

@tetsuharuohzeki
Copy link
Contributor Author

relate: #10758

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 24, 2016
@mhegazy mhegazy self-assigned this Oct 24, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Oct 24, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Oct 24, 2016

I agree it is invalid with --target ES2015; but the above sample is for --module ES2015, which i do not see why it is invalid. the module code has been parsed and transformed, and the "use strict" is part of this transformation.

@tetsuharuohzeki
Copy link
Contributor Author

import/export declarations introduced by es2015 is only be parsed in parsing them as Module. Paradoxically, we can regards as Module as the source file which includes import/export declarations and is parsed as valid ecma262 code.

@kitsonk
Copy link
Contributor

kitsonk commented Oct 25, 2016

Modules in the ES2015 module format do not have to be loaded by the native module loader (since there is yet to be a production ready one). Having a valid module format does not always imply the code will be interpreted in strict mode. While SystemJS will load modules in strict mode when their source module format is ES2015, other bundlers may or may not do the same.

Since there is no issue extra emits of the prolog, I would say it is still best to emit code targeted at ES5 to provide the "use strict"; prolog irrespective of the module format. It minimises any surprises.

@mhegazy mhegazy assigned yuit and unassigned mhegazy Oct 27, 2016
@mhegazy mhegazy modified the milestones: TypeScript 2.1, TypeScript 2.1.2 Oct 27, 2016
@aluanhaddad
Copy link
Contributor

I am definitely in favor of leaving this as is.

@slawomir
Copy link
Contributor

Specification mentioned by @saneyuki stays that modules are interpreted in strict mode. Specification means ES6 modules, so following conditions must be met to make TypeScript generate ES6 module:

  • --target es2015
  • --module es2015

--module cannot be set to for example commonjs, because then result Javascript will not be ES6 module, and strict mode will not be assured by default in runtime.

On the other hand adding "use strict" in generated ES6 module should not cause any problems. In that case skipping use strict for ES6 modules would be an optimisation to result code.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 10, 2016
@mhegazy mhegazy closed this as completed Nov 10, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

7 participants