Skip to content

Commit

Permalink
Fix line breaks for CSS in JS (#9136)
Browse files Browse the repository at this point in the history
* Fix 2.1 regression for css-in-js

* Add changelog
  • Loading branch information
sosukesuzuki committed Sep 4, 2020
1 parent 9a5dd4b commit 2c0e983
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 16 deletions.
46 changes: 46 additions & 0 deletions changelog_unreleased/javascript/pr-9136.md
@@ -0,0 +1,46 @@
#### Fix line breaks for CSS in JS (#9136 by @sosukesuzuki)

<!-- prettier-ignore -->
```js
// Input
styled.div`
// prettier-ignore
@media (aaaaaaaaaaaaa) {
z-index: ${(props) => (props.isComplete ? '1' : '0')};
}
`;
styled.div`
${props => getSize(props.$size.xs)}
${props => getSize(props.$size.sm, 'sm')}
${props => getSize(props.$size.md, 'md')}
`;

// Prettier stable
styled.div`
// prettier-ignore
@media (aaaaaaaaaaaaa) {
z-index: ${(props) =>
props.isComplete ? "1" : "0"};
}
`;
styled.div`
${(props) => getSize(props.$size.xs)}
${(props) => getSize(props.$size.sm, "sm")}
${(props) =>
getSize(props.$size.md, "md")}
`;

// Prettier master
styled.div`
// prettier-ignore
@media (aaaaaaaaaaaaa) {
z-index: ${(props) => (props.isComplete ? "1" : "0")};
}
`;
styled.div`
${(props) => getSize(props.$size.xs)}
${(props) => getSize(props.$size.sm, "sm")}
${(props) => getSize(props.$size.md, "md")}
`;

```
15 changes: 15 additions & 0 deletions src/document/doc-utils.js
@@ -1,5 +1,7 @@
"use strict";

const { literalline, concat } = require("./doc-builders");

// Using a unique object to compare by reference.
const traverseDocOnExitStackMarker = {};

Expand Down Expand Up @@ -258,6 +260,18 @@ function normalizeDoc(doc) {
});
}

function replaceNewlinesWithLiterallines(doc) {
return mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string" && currentDoc.includes("\n")
? concat(
currentDoc
.split(/(\n)/g)
.map((v, i) => (i % 2 === 0 ? v : literalline))
)
: currentDoc
);
}

module.exports = {
isEmpty,
willBreak,
Expand All @@ -270,4 +284,5 @@ module.exports = {
stripTrailingHardline,
normalizeParts,
normalizeDoc,
replaceNewlinesWithLiterallines,
};
4 changes: 2 additions & 2 deletions src/language-js/embed.js
Expand Up @@ -12,7 +12,7 @@ const {
group,
dedentToRoot,
},
utils: { mapDoc },
utils: { mapDoc, replaceNewlinesWithLiterallines },
} = require("../document");
const { isBlockComment, hasLeadingComment } = require("./comments");

Expand Down Expand Up @@ -301,7 +301,7 @@ function replacePlaceholders(quasisDoc, expressionDocs) {
part.split(/@prettier-placeholder-(\d+)-id/).forEach((component, idx) => {
// The placeholder is always at odd indices
if (idx % 2 === 0) {
replacedParts.push(component);
replacedParts.push(replaceNewlinesWithLiterallines(component));
return;
}

Expand Down
16 changes: 2 additions & 14 deletions src/language-markdown/embed.js
Expand Up @@ -2,8 +2,8 @@

const { getParserName, getMaxContinuousCount } = require("../common/util");
const {
builders: { hardline, literalline, concat, markAsRoot },
utils: { mapDoc },
builders: { hardline, concat, markAsRoot },
utils: { replaceNewlinesWithLiterallines },
} = require("../document");
const { print: printFrontMatter } = require("../utils/front-matter");
const { getFencedCodeBlockValue } = require("./utils");
Expand Down Expand Up @@ -66,18 +66,6 @@ function embed(path, print, textToDoc, options) {
}

return null;

function replaceNewlinesWithLiterallines(doc) {
return mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string" && currentDoc.includes("\n")
? concat(
currentDoc
.split(/(\n)/g)
.map((v, i) => (i % 2 === 0 ? v : literalline))
)
: currentDoc
);
}
}

module.exports = embed;
26 changes: 26 additions & 0 deletions tests/js/multiparser-css/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -573,6 +573,19 @@ const Foo = styled.p\`
}
\`;
styled(A)\`
// prettier-ignore
@media (aaaaaaaaaaaaa) {
z-index: \${(props) => (props.isComplete ? '1' : '0')};
}
\`;
const StyledDiv = styled.div\`
\${props => getSize(props.$size.xs)}
\${props => getSize(props.$size.sm, 'sm')}
\${props => getSize(props.$size.md, 'md')}
\`;
=====================================output=====================================
const ListItem1 = styled.li\`\`;
Expand Down Expand Up @@ -826,6 +839,19 @@ const Foo = styled.p\`
}
\`;
styled(A)\`
// prettier-ignore
@media (aaaaaaaaaaaaa) {
z-index: \${(props) => (props.isComplete ? "1" : "0")};
}
\`;
const StyledDiv = styled.div\`
\${(props) => getSize(props.$size.xs)}
\${(props) => getSize(props.$size.sm, "sm")}
\${(props) => getSize(props.$size.md, "md")}
\`;
================================================================================
`;
Expand Down
13 changes: 13 additions & 0 deletions tests/js/multiparser-css/styled-components.js
Expand Up @@ -246,3 +246,16 @@ const Foo = styled.p`
margin-top: 3rem;
}
`;

styled(A)`
// prettier-ignore
@media (aaaaaaaaaaaaa) {
z-index: ${(props) => (props.isComplete ? '1' : '0')};
}
`;

const StyledDiv = styled.div`
${props => getSize(props.$size.xs)}
${props => getSize(props.$size.sm, 'sm')}
${props => getSize(props.$size.md, 'md')}
`;

0 comments on commit 2c0e983

Please sign in to comment.