Ensure directives get printed in block statements (#4873)
This commit is contained in:
@@ -14,11 +14,14 @@ export function Program(node: Object) {
|
||||
export function BlockStatement(node: Object) {
|
||||
this.token("{");
|
||||
this.printInnerComments(node);
|
||||
if (node.body.length) {
|
||||
|
||||
let hasDirectives = node.directives && node.directives.length;
|
||||
|
||||
if (node.body.length || hasDirectives) {
|
||||
this.newline();
|
||||
|
||||
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.removeTrailingNewline();
|
||||
|
||||
@@ -230,6 +230,20 @@ describe("programmatic generation", function() {
|
||||
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() {
|
||||
let objectStatement = t.objectTypeAnnotation(
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user