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

Babel puts 'use strict'; in front of everything #1844

Closed
marrs opened this issue Jun 26, 2015 · 14 comments
Closed

Babel puts 'use strict'; in front of everything #1844

marrs opened this issue Jun 26, 2015 · 14 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@marrs
Copy link

marrs commented Jun 26, 2015

I've created the simplest babel programme I can think of, that I would expect to do nothing.

var babel = require('babel-core');
var code = '"use strict";';    // Edited typo
var result = babel.transform(code);

// Expected result: "use strict";
// Actual result: "use strict"; "use strict";

I had presumed that a call to transform with no transformers specified would do nothing. At the very least it's forcing strict mode on me. Is this a bug or a feature? Are there other things babel forces on me by default?

@sebmck
Copy link
Contributor

sebmck commented Jun 26, 2015

ES6 modules have an implicit strict mode so at the least you'll get "use strict"; prepended to your program. Regarding:

I had presumed that a call to transform with no transformers specified would do nothing.

Not sure what you mean by "no transformers specified", by default all the ES6/stable transformers are enabled.

Also FWIW, the strict directive is being duplicated because you've got the semicolon inside the string, moving it out results in the strict directive only being output once.

@sebmck sebmck closed this as completed Jun 26, 2015
@marrs
Copy link
Author

marrs commented Jun 26, 2015

Not sure what you mean by "no transformers specified", by default all the ES6/stable transformers are enabled.

Ah, can I disable them? I really only want Babel for its transpiling abilities.

Also FWIW, the strict directive is being duplicated because you've got the semicolon inside the string, moving it out results in the strict directive only being output once.

I made a typo in the report above. The original code implemented strict mode correctly, and still generated the duplicate declaration.

@sebmck
Copy link
Contributor

sebmck commented Jun 26, 2015

Ah, can I disable them? I really only want Babel for its transpiling abilities.

Strict mode is a part of that though?

The original code implemented strict mode correctly, and still generated the duplicate declaration.

Nope?

> var babel = require('babel-core');
undefined
> var code = '"use strict";';
undefined
> var result = babel.transform(code);
undefined
> result.code;
'"use strict";'

@jmm
Copy link
Member

jmm commented Jun 26, 2015

@sebmck FWIW, there is a (different) use strict related bug that I noticed (that you very well may already know about). I decided not to mention it before because I didn't think you'd be terribly interested, but since there's already this discussion going on I figured I'd mention it (don't want to derail this though).

> var babel = require('babel-core');
undefined
> var code = '"use\\x20strict";';
undefined
> var result = babel.transform(code);
undefined
> result.code;
'"use strict";'

@sebmck
Copy link
Contributor

sebmck commented Jun 26, 2015

@jmm Can you open a new issue? Should be easy enough to fix.

@jmm
Copy link
Member

jmm commented Jun 26, 2015

@sebmck Sure, see #1852.

@marrs
Copy link
Author

marrs commented Jun 26, 2015

@sebmck you're right, there was a bug in my test case. I was echoing the output twice!

Ah, can I disable them? I really only want Babel for its transpiling abilities.

Strict mode is a part of that though?

No. Why, should it be? I'm experimenting with a superset of Javascript. For my own sanity, I'd like to be confident that the changes I get are just the changes I ask for. I think I just made the case for that requirement at the top of this post :)

@sebmck
Copy link
Contributor

sebmck commented Jun 26, 2015

@marrs

No. Why, should it be?

As I said before, ES6 modules have an implicit strict mode. Spec reference here if you don't believe me.

@marrs
Copy link
Author

marrs commented Jun 26, 2015

I see. It wasn't clear to me that Babel exclusively targets ES6. I deduced that it was written to enable ES6 compatibility in legacy browsers but the homepage gave me the impression it was also a general purpose transpiler.

@sebmck
Copy link
Contributor

sebmck commented Jun 26, 2015

It wasn't clear to me that Babel exclusively targets ES6.

It doesn't? Babel just has an comprehensive suite of ES6 transformers that are enabled by default.

the homepage gave me the impression it was also a general purpose transpiler.

Yeah it is. There's a whole host of plugins. And syntax extensions, linting/static analysis and minification is in the pipeline.

@marrs
Copy link
Author

marrs commented Jun 26, 2015

So can I disable them? It doesn't seem to be possible without modifying the code. I have stripped them out of transformation/index.js in my fork to get the behaviour I want. This seems to be the best I can do.

@sebmck
Copy link
Contributor

sebmck commented Jun 26, 2015

You can disable any of the transformers. See the docs here for a list of the built-in ones. And see the strict transformer docs on how to disable that particular behaviour.

@Wandalen
Copy link

Wandalen commented Mar 2, 2017

Annoying issue, what a point to force "strict" mode?
@kittens links are dead!

@hzoo
Copy link
Member

hzoo commented Mar 2, 2017

ES6 module code, which Babel expects by default, is automatically Strict Mode according to the ES6 spec

@babel babel locked and limited conversation to collaborators Mar 2, 2017
@hzoo hzoo added the old label Mar 2, 2017
@hzoo hzoo added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

5 participants