Skip to content

Commit

Permalink
feat: add html-top-level-indent option, resolve prettier#3888
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo committed Apr 23, 2019
1 parent ed15b6d commit 9d5c682
Show file tree
Hide file tree
Showing 23 changed files with 8,363 additions and 986 deletions.
23 changes: 23 additions & 0 deletions src/language-html/options.js
Expand Up @@ -24,5 +24,28 @@ module.exports = {
description: "Whitespaces are considered insensitive."
}
]
},
htmlTopLevelIndent: {
since: "1.18.0",
category: CATEGORY_HTML,
type: "choice",
default: "auto",
description: "How to handle top-level indent in HTML.",
choices: [
{
value: "always",
description:
"Always apply top-level indent for templates, scripts and styles."
},
{
value: "never",
description: "Avoid top-level indent for templates, scripts and styles."
},
{
value: "auto",
description:
"Avoid top-level indent for scripts and styles inside Vue files."
}
]
}
};
8 changes: 5 additions & 3 deletions src/language-html/printer-html.js
Expand Up @@ -227,9 +227,11 @@ function genericPrint(path, options, print) {
? ifBreak(indent(childrenDoc), childrenDoc, {
groupId: attrGroupId
})
: isScriptLikeTag(node) &&
node.parent.type === "root" &&
options.parser === "vue"
: node.parent.type === "root" &&
((options.htmlTopLevelIndent === "auto" &&
isScriptLikeTag(node) &&
options.parser === "vue") ||
options.htmlTopLevelIndent === "never")
? childrenDoc
: indent(childrenDoc))(
concat([
Expand Down
57 changes: 57 additions & 0 deletions tests/html_aurelia/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -17,3 +17,60 @@ printWidth: 80
================================================================================
`;
exports[`basic.html 2`] = `
====================================options=====================================
htmlTopLevelIndent: "auto"
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<template>
<i class.bind="icon"></i>
</template>
=====================================output=====================================
<template>
<i class.bind="icon"></i>
</template>
================================================================================
`;
exports[`basic.html 3`] = `
====================================options=====================================
htmlTopLevelIndent: "always"
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<template>
<i class.bind="icon"></i>
</template>
=====================================output=====================================
<template>
<i class.bind="icon"></i>
</template>
================================================================================
`;
exports[`basic.html 4`] = `
====================================options=====================================
htmlTopLevelIndent: "never"
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<template>
<i class.bind="icon"></i>
</template>
=====================================output=====================================
<template>
<i class.bind="icon"></i>
</template>
================================================================================
`;
3 changes: 3 additions & 0 deletions tests/html_aurelia/jsfmt.spec.js
@@ -1 +1,4 @@
run_spec(__dirname, ["html"]);
run_spec(__dirname, ["html"], { htmlTopLevelIndent: "auto" });
run_spec(__dirname, ["html"], { htmlTopLevelIndent: "always" });
run_spec(__dirname, ["html"], { htmlTopLevelIndent: "never" });

0 comments on commit 9d5c682

Please sign in to comment.