better identification of strict directives - cc @jmm

This commit is contained in:
Sebastian McKenzie 2015-07-21 18:43:10 +01:00
parent 0fbc714315
commit beb8061d3f
2 changed files with 13 additions and 3 deletions

View File

@ -103,8 +103,8 @@ exports.nodes = {
* Test if Literal needs whitespace. * Test if Literal needs whitespace.
*/ */
Literal(node) { Literal(node, parent) {
if (node.value === "use strict") { if (t.isExpressionStatement(parent)) {
return { return {
after: true after: true
}; };

View File

@ -6,6 +6,16 @@ export var metadata = {
const THIS_BREAK_KEYS = ["FunctionExpression", "FunctionDeclaration", "ClassExpression", "ClassDeclaration"]; const THIS_BREAK_KEYS = ["FunctionExpression", "FunctionDeclaration", "ClassExpression", "ClassDeclaration"];
function isUseStrict(node) {
if (!t.isLiteral(node)) return false;
if (node.raw && node.rawValue === node.value) {
return node.rawValue === "use strict";
} else {
return node.value === "use strict";
}
}
/** /**
* [Please add a description.] * [Please add a description.]
*/ */
@ -21,7 +31,7 @@ export var visitor = {
var first = program.body[0]; var first = program.body[0];
var directive; var directive;
if (t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" })) { if (t.isExpressionStatement(first) && isUseStrict(first.expression)) {
directive = first; directive = first;
} else { } else {
directive = t.expressionStatement(t.literal("use strict")); directive = t.expressionStatement(t.literal("use strict"));