rejigger strict directives transformer logic so they're included in the body before module formatters are ran - @jayphelps
This commit is contained in:
21
lib/babel/transformation/helpers/use-strict.js
Normal file
21
lib/babel/transformation/helpers/use-strict.js
Normal file
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../types");
|
||||
|
||||
exports.has = function (node) {
|
||||
var first = node.body[0];
|
||||
return t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" });
|
||||
};
|
||||
|
||||
exports.wrap = function (node, callback) {
|
||||
var useStrictNode;
|
||||
if (exports.has(node)) {
|
||||
useStrictNode = node.body.shift();
|
||||
}
|
||||
|
||||
callback();
|
||||
|
||||
if (useStrictNode) {
|
||||
node.body.unshift(useStrictNode);
|
||||
}
|
||||
};
|
||||
@@ -95,6 +95,7 @@ module.exports = {
|
||||
"spec.typeofSymbol": require("./spec/typeof-symbol"),
|
||||
"spec.undefinedToVoid": require("./spec/undefined-to-void"),
|
||||
|
||||
_useStrict: require("./internal/use-strict"),
|
||||
_moduleFormatter: require("./internal/module-formatter"),
|
||||
|
||||
"es3.propertyLiterals": require("./es3/property-literals"),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../../types");
|
||||
var useStrict = require("../../helpers/use-strict");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.secondPass = true;
|
||||
|
||||
@@ -8,26 +9,28 @@ exports.BlockStatement =
|
||||
exports.Program = function (node, parent, scope, file) {
|
||||
if (!node._declarations) return;
|
||||
|
||||
var kinds = {};
|
||||
var kind;
|
||||
useStrict.wrap(node, function () {
|
||||
var kinds = {};
|
||||
var kind;
|
||||
|
||||
for (var i in node._declarations) {
|
||||
var declar = node._declarations[i];
|
||||
for (var i in node._declarations) {
|
||||
var declar = node._declarations[i];
|
||||
|
||||
kind = declar.kind || "var";
|
||||
var declarNode = t.variableDeclarator(declar.id, declar.init);
|
||||
kind = declar.kind || "var";
|
||||
var declarNode = t.variableDeclarator(declar.id, declar.init);
|
||||
|
||||
if (declar.init) {
|
||||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, [declarNode])));
|
||||
} else {
|
||||
kinds[kind] = kinds[kind] || [];
|
||||
kinds[kind].push(declarNode);
|
||||
if (declar.init) {
|
||||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, [declarNode])));
|
||||
} else {
|
||||
kinds[kind] = kinds[kind] || [];
|
||||
kinds[kind].push(declarNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (kind in kinds) {
|
||||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, kinds[kind])));
|
||||
}
|
||||
for (kind in kinds) {
|
||||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, kinds[kind])));
|
||||
}
|
||||
|
||||
node._declarations = null;
|
||||
node._declarations = null;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var useStrict = require("../../helpers/use-strict");
|
||||
|
||||
exports.Program = function (program, parent, scope, file) {
|
||||
if (!file.transformers["es6.modules"].canRun()) return;
|
||||
|
||||
program.body = file.dynamicImports.concat(program.body);
|
||||
useStrict.wrap(program, function () {
|
||||
program.body = file.dynamicImports.concat(program.body);
|
||||
});
|
||||
|
||||
if (file.moduleFormatter.transform) {
|
||||
file.moduleFormatter.transform(program);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.Program = function (program, parent, scope, file) {
|
||||
if (file.transformers.useStrict.canRun()) {
|
||||
program.body.unshift(t.expressionStatement(t.literal("use strict")));
|
||||
}
|
||||
};
|
||||
@@ -10,10 +10,6 @@ exports.Program = function (program) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.post = function (file) {
|
||||
file.ast.program.body.unshift(t.expressionStatement(t.literal("use strict")));
|
||||
};
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function () {
|
||||
this.skip();
|
||||
|
||||
Reference in New Issue
Block a user