add "use strict" declaration - closes #78

This commit is contained in:
Sebastian McKenzie
2014-10-18 15:44:44 +11:00
parent dcaea68240
commit 234d9997c8

View File

@@ -0,0 +1,15 @@
var b = require("recast").types.builders;
module.exports = function (ast, file) {
var body = ast.program.body;
var first = body[0];
var noStrict = !first || first.type !== "ExpressionStatement" || first.expression.type !== "Literal" || first.expression.value !== "use strict";
if (noStrict) {
if (file.opts._noStrict) return;
body.unshift(b.expressionStatement(b.literal("use strict")));
} else {
if (file.opts._noStrict) body.shift();
}
};