Ensure directives get printed in block statements (#4873)

This commit is contained in:
Brian Ng 2016-12-16 14:30:14 -06:00 committed by Henry Zhu
parent 2bbc36d25e
commit 01bc43464e
8 changed files with 37 additions and 8 deletions

View File

@ -14,11 +14,14 @@ export function Program(node: Object) {
export function BlockStatement(node: Object) { export function BlockStatement(node: Object) {
this.token("{"); this.token("{");
this.printInnerComments(node); this.printInnerComments(node);
if (node.body.length) {
let hasDirectives = node.directives && node.directives.length;
if (node.body.length || hasDirectives) {
this.newline(); this.newline();
this.printSequence(node.directives, node, { indent: true }); this.printSequence(node.directives, node, { indent: true });
if (node.directives && node.directives.length) this.newline(); if (hasDirectives) this.newline();
this.printSequence(node.body, node, { indent: true }); this.printSequence(node.body, node, { indent: true });
this.removeTrailingNewline(); this.removeTrailingNewline();

View File

@ -230,6 +230,20 @@ describe("programmatic generation", function() {
assert.equal(ast.program.body[0].consequent.type, "BlockStatement"); assert.equal(ast.program.body[0].consequent.type, "BlockStatement");
}); });
it("prints directives in block with empty body", function() {
let blockStatement = t.blockStatement(
[],
[t.directive(t.directiveLiteral("use strict"))]
);
let output = generate.default(blockStatement).code;
assert.equal(output, [
"{",
" \"use strict\";",
"}"
].join("\n"));
});
it("flow object indentation", function() { it("flow object indentation", function() {
let objectStatement = t.objectTypeAnnotation( let objectStatement = t.objectTypeAnnotation(
[ [

View File

@ -1 +1,3 @@
define("my custom module name", [], function () {}); define("my custom module name", [], function () {
"use strict";
});

View File

@ -1 +1,3 @@
define(["foo", "foo-bar", "./directory/foo-bar"], function () {}); define(["foo", "foo-bar", "./directory/foo-bar"], function () {
"use strict";
});

View File

@ -10,4 +10,6 @@
factory(); factory();
global.myCustomModuleName = mod.exports; global.myCustomModuleName = mod.exports;
} }
})(this, function () {}); })(this, function () {
"use strict";
});

View File

@ -10,4 +10,6 @@
factory(global.foo, global.fooBar, global.fooBar); factory(global.foo, global.fooBar, global.fooBar);
global.actual = mod.exports; global.actual = mod.exports;
} }
})(this, function () {}); })(this, function () {
"use strict";
});

View File

@ -10,4 +10,6 @@
factory(global.render); factory(global.render);
global.actual = mod.exports; global.actual = mod.exports;
} }
})(this, function (_render) {}); })(this, function (_render) {
"use strict";
});

View File

@ -10,4 +10,6 @@
factory(global.baz); factory(global.baz);
global.actual = mod.exports; global.actual = mod.exports;
} }
})(this, function () {}); })(this, function () {
"use strict";
});