fix(javascript): correct indentation for expression in root template (#5607)

master
Ika 2018-12-12 09:52:23 +08:00 committed by GitHub
parent cd141c5347
commit 419559e964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 5 deletions

View File

@ -2491,10 +2491,8 @@ function printPathNoParens(path, options, print, args) {
// expression inside at the beginning of ${ instead of the beginning
// of the `.
const tabWidth = options.tabWidth;
const indentSize = getIndentSize(
childPath.getValue().value.raw,
tabWidth
);
const quasi = childPath.getValue();
const indentSize = getIndentSize(quasi.value.raw, tabWidth);
let printed = expressions[i];
@ -2507,7 +2505,10 @@ function printPathNoParens(path, options, print, args) {
printed = concat([indent(concat([softline, printed])), softline]);
}
const aligned = addAlignmentToDoc(printed, indentSize, tabWidth);
const aligned =
indentSize === 0 && quasi.value.raw.endsWith("\n")
? align(-Infinity, printed)
: addAlignmentToDoc(printed, indentSize, tabWidth);
parts.push(group(concat(["${", aligned, lineSuffixBoundary, "}"])));
}

View File

@ -187,6 +187,59 @@ module.exports = Relay.createContainer(
================================================================================
`;
exports[`indent.js 1`] = `
====================================options=====================================
parsers: ["flow", "babylon"]
printWidth: 80
| printWidth
=====================================input======================================
const foo = () => {
{
{
{
return \`
line 1
line 2
...
line n
\${foo({
many: keys,
many: keys
})}
line n + 1
line n + 2
line n + n
\`;
}
}
}
};
=====================================output=====================================
const foo = () => {
{
{
{
return \`
line 1
line 2
...
line n
\${foo({
many: keys,
many: keys
})}
line n + 1
line n + 2
line n + n
\`;
}
}
}
};
================================================================================
`;
exports[`inline.js 1`] = `
====================================options=====================================
parsers: ["flow", "babylon"]

21
tests/template/indent.js Normal file
View File

@ -0,0 +1,21 @@
const foo = () => {
{
{
{
return `
line 1
line 2
...
line n
${foo({
many: keys,
many: keys
})}
line n + 1
line n + 2
line n + n
`;
}
}
}
};