Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line breaks for CSS in JS #9136

Merged
merged 2 commits into from Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')}
`;