handle comments and use strict directives better - fixes #1030
This commit is contained in:
parent
9f037fa75e
commit
bc9c1ab61e
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
// opt
|
||||
"use strict";
|
||||
|
||||
// opt
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var a = 1;
|
||||
|
||||
@ -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;
|
||||
|
||||
4
test/fixtures/transformation/strict/leading-comments-with-existing/actual.js
vendored
Normal file
4
test/fixtures/transformation/strict/leading-comments-with-existing/actual.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
// comments
|
||||
"use strict";
|
||||
|
||||
module.exports = {};
|
||||
4
test/fixtures/transformation/strict/leading-comments-with-existing/expected.js
vendored
Normal file
4
test/fixtures/transformation/strict/leading-comments-with-existing/expected.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
// comments
|
||||
"use strict";
|
||||
|
||||
module.exports = {};
|
||||
3
test/fixtures/transformation/strict/leading-comments/actual.js
vendored
Normal file
3
test/fixtures/transformation/strict/leading-comments/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
// comments
|
||||
|
||||
module.exports = {};
|
||||
5
test/fixtures/transformation/strict/leading-comments/expected.js
vendored
Normal file
5
test/fixtures/transformation/strict/leading-comments/expected.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
// comments
|
||||
|
||||
"use strict";
|
||||
|
||||
module.exports = {};
|
||||
Loading…
x
Reference in New Issue
Block a user