Skip to content

Commit

Permalink
fix: namespace followed by inline node
Browse files Browse the repository at this point in the history
Fixes #1052
  • Loading branch information
czosel committed Jan 2, 2020
1 parent 9f02d4e commit 7410025
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,12 +1037,12 @@ function printLines(path, options, print, childrenAttribute = "children") {
const beforeInline =
childNode.leadingComments && childNode.leadingComments.length
? concat([
isFirstNode ? openTag : "",
isFirstNode && node.kind !== "namespace" ? openTag : "",
hardline,
comments.printComments(childNode.leadingComments, options),
"?>"
])
: isProgramLikeNode(node) && isFirstNode
: isProgramLikeNode(node) && isFirstNode && node.kind !== "namespace"
? ""
: concat([beforeCloseTagInlineNode, "?>"]);
const afterInline =
Expand Down Expand Up @@ -1623,14 +1623,18 @@ function printNode(path, options, print) {
? node.withBrackets
? indent(concat([hardline, printLines(path, options, print)]))
: concat([
hardline,
isNextLineEmptyAfterNamespace(
options.originalText,
node,
options.locStart
)
? hardline
: "",
node.children[0].kind === "inline"
? ""
: concat([
hardline,
isNextLineEmptyAfterNamespace(
options.originalText,
node,
options.locStart
)
? hardline
: ""
]),
printLines(path, options, print)
])
: "",
Expand Down
58 changes: 58 additions & 0 deletions tests/inline/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,64 @@ $var = 2;
================================================================================
`;

exports[`namespace-1.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php namespace foo; ?>
<html></html>

=====================================output=====================================
<?php namespace foo; ?>
<html></html>

================================================================================
`;

exports[`namespace-2.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php namespace foo; ?>

<html></html>

=====================================output=====================================
<?php namespace foo; ?>

<html></html>

================================================================================
`;

exports[`namespace-3.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php
namespace foo;
// comment
?>

<html></html>

=====================================output=====================================
<?php
namespace foo;
// comment
?>

<html></html>

================================================================================
`;

exports[`only-html.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
2 changes: 2 additions & 0 deletions tests/inline/namespace-1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php namespace foo; ?>
<html></html>
3 changes: 3 additions & 0 deletions tests/inline/namespace-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace foo; ?>

<html></html>
6 changes: 6 additions & 0 deletions tests/inline/namespace-3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace foo;
// comment
?>

<html></html>

0 comments on commit 7410025

Please sign in to comment.