diff --git a/src/babel/transformation/transformers/internal/strict.js b/src/babel/transformation/transformers/internal/strict.js index 84a1ee0499..861988daae 100644 --- a/src/babel/transformation/transformers/internal/strict.js +++ b/src/babel/transformation/transformers/internal/strict.js @@ -2,6 +2,17 @@ import * as t from "../../../types"; export function Program(program, parent, scope, file) { if (file.transformers.strict.canRun()) { - program.body.unshift(t.expressionStatement(t.literal("use strict"))); + var directive = file.get("existingStrictDirective"); + + if (!directive) { + directive = t.expressionStatement(t.literal("use strict")); + var first = program.body[0]; + if (first) { + directive.leadingComments = first.leadingComments; + first.leadingComments = []; + } + } + + program.body.unshift(directive); } } diff --git a/src/babel/transformation/transformers/other/strict.js b/src/babel/transformation/transformers/other/strict.js index db7849059a..1ebde501e6 100644 --- a/src/babel/transformation/transformers/other/strict.js +++ b/src/babel/transformation/transformers/other/strict.js @@ -1,10 +1,10 @@ import * as messages from "../../../messages"; import * as t from "../../../types"; -export function Program(program) { +export function Program(program, parent, scope, file) { var first = program.body[0]; if (t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" })) { - program.body.shift(); + file.set("existingStrictDirective", program.body.shift()); } } diff --git a/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js b/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js index ad9f44331c..d1aaf9d67a 100644 --- a/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js +++ b/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js @@ -1,6 +1,6 @@ +// opt "use strict"; -// opt var a = 1; var b = 2; var a = 1; diff --git a/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js b/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js index f658848e82..66d333d054 100644 --- a/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js +++ b/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js @@ -1,8 +1,8 @@ -"use strict"; - // we need to deopt `test` if it's reassigned as we can't be certain of it's // state, ie. it could have been rebound or dereferenced +"use strict"; + function test(exit) { if (exit) { return this.x; diff --git a/test/fixtures/transformation/strict/leading-comments-with-existing/actual.js b/test/fixtures/transformation/strict/leading-comments-with-existing/actual.js new file mode 100644 index 0000000000..078ae074be --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments-with-existing/actual.js @@ -0,0 +1,4 @@ +// comments +"use strict"; + +module.exports = {}; diff --git a/test/fixtures/transformation/strict/leading-comments-with-existing/expected.js b/test/fixtures/transformation/strict/leading-comments-with-existing/expected.js new file mode 100644 index 0000000000..078ae074be --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments-with-existing/expected.js @@ -0,0 +1,4 @@ +// comments +"use strict"; + +module.exports = {}; diff --git a/test/fixtures/transformation/strict/leading-comments/actual.js b/test/fixtures/transformation/strict/leading-comments/actual.js new file mode 100644 index 0000000000..51a1ff892d --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments/actual.js @@ -0,0 +1,3 @@ +// comments + +module.exports = {}; diff --git a/test/fixtures/transformation/strict/leading-comments/expected.js b/test/fixtures/transformation/strict/leading-comments/expected.js new file mode 100644 index 0000000000..e6091249bb --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments/expected.js @@ -0,0 +1,5 @@ +// comments + +"use strict"; + +module.exports = {};