diff --git a/src/language-html/options.js b/src/language-html/options.js index ee8d6c0592dc..44179df1ca1b 100644 --- a/src/language-html/options.js +++ b/src/language-html/options.js @@ -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." + } + ] } }; diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index ce5a8fab8f34..d586ebe81460 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -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([ diff --git a/tests/html_aurelia/__snapshots__/jsfmt.spec.js.snap b/tests/html_aurelia/__snapshots__/jsfmt.spec.js.snap index d08d4efdaa63..8b4f4cd8a86e 100644 --- a/tests/html_aurelia/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_aurelia/__snapshots__/jsfmt.spec.js.snap @@ -17,3 +17,60 @@ printWidth: 80 ================================================================================ `; + +exports[`basic.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`basic.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`basic.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; diff --git a/tests/html_aurelia/jsfmt.spec.js b/tests/html_aurelia/jsfmt.spec.js index 53763df9b20b..53078dba5fde 100644 --- a/tests/html_aurelia/jsfmt.spec.js +++ b/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" }); diff --git a/tests/html_css/__snapshots__/jsfmt.spec.js.snap b/tests/html_css/__snapshots__/jsfmt.spec.js.snap index 1effb3f87af8..6c2b1f48879a 100644 --- a/tests/html_css/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_css/__snapshots__/jsfmt.spec.js.snap @@ -14,6 +14,51 @@ printWidth: 80 ================================================================================ `; +exports[`empty.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`empty.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`empty.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + exports[`less.html 1`] = ` ====================================options===================================== parsers: ["html"] @@ -60,6 +105,147 @@ printWidth: 80 ================================================================================ `; +exports[`less.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`less.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`less.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + exports[`postcss.html 1`] = ` ====================================options===================================== parsers: ["html"] @@ -92,6 +278,105 @@ printWidth: 80 ================================================================================ `; +exports[`postcss.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`postcss.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`postcss.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + exports[`scss.html 1`] = ` ====================================options===================================== parsers: ["html"] @@ -168,14 +453,341 @@ printWidth: 80 ================================================================================ `; -exports[`simple.html 1`] = ` +exports[`scss.html 2`] = ` ====================================options===================================== +htmlTopLevelIndent: "auto" parsers: ["html"] printWidth: 80 | printWidth =====================================input====================================== - - + + + + + + +=====================================output===================================== + + + + + + +================================================================================ +`; + +exports[`scss.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + +=====================================output===================================== + + + + + + +================================================================================ +`; + +exports[`scss.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + +=====================================output===================================== + + + + + + +================================================================================ +`; + +exports[`simple.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +
+This page is just a demo.
+ + + +=====================================output===================================== + + + +This page is just a demo.
+ + + +================================================================================ +`; + +exports[`simple.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +This page is just a demo.
+ + + +=====================================output===================================== + + + +This page is just a demo.
+ + + +================================================================================ +`; + +exports[`simple.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + +This page is just a demo.
+ + + +=====================================output===================================== + + + +This page is just a demo.
+ + + +================================================================================ +`; + exports[`single-style.html 1`] = ` ====================================options===================================== parsers: ["html"] @@ -246,3 +906,102 @@ printWidth: 80 ================================================================================ `; + +exports[`single-style.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; + +exports[`single-style.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; + +exports[`single-style.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; diff --git a/tests/html_css/jsfmt.spec.js b/tests/html_css/jsfmt.spec.js index 53763df9b20b..53078dba5fde 100644 --- a/tests/html_css/jsfmt.spec.js +++ b/tests/html_css/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" }); diff --git a/tests/html_js/__snapshots__/jsfmt.spec.js.snap b/tests/html_js/__snapshots__/jsfmt.spec.js.snap index 66ca2ccdf9cc..004c7e677679 100644 --- a/tests/html_js/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_js/__snapshots__/jsfmt.spec.js.snap @@ -14,6 +14,51 @@ printWidth: 80 ================================================================================ `; +exports[`empty.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`empty.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`empty.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + exports[`js.html 1`] = ` ====================================options===================================== parsers: ["html"] @@ -74,6 +119,189 @@ printWidth: 80 ================================================================================ `; +exports[`js.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + +=====================================output===================================== + + + + + + +================================================================================ +`; + +exports[`js.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + +=====================================output===================================== + + + + + + +================================================================================ +`; + +exports[`js.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + +=====================================output===================================== + + + + + + +================================================================================ +`; + exports[`simple.html 1`] = ` ====================================options===================================== parsers: ["html"] @@ -120,93 +348,858 @@ printWidth: 80 ================================================================================ `; -exports[`single-script.html 1`] = ` +exports[`simple.html 2`] = ` ====================================options===================================== +htmlTopLevelIndent: "auto" parsers: ["html"] printWidth: 80 | printWidth =====================================input====================================== - - + + + +This page is just a demo.
+ + =====================================output===================================== - - + + + +This page is just a demo.
+ + + +================================================================================ +`; + +exports[`simple.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +This page is just a demo.
+ + + +=====================================output===================================== + + + +This page is just a demo.
+ + + +================================================================================ +`; + +exports[`simple.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +This page is just a demo.
+ + + +=====================================output===================================== + + + +This page is just a demo.
+ + + +================================================================================ +`; + +exports[`single-script.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; + +exports[`single-script.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; + +exports[`single-script.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; + +exports[`single-script.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + +================================================================================ +`; + +exports[`something-else.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`something-else.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`something-else.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`something-else.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + +exports[`template-literal.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + + + + +=====================================output===================================== + + + + + + + + +================================================================================ +`; + +exports[`template-literal.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + + + + +=====================================output===================================== + + + + + + + + +================================================================================ +`; + +exports[`template-literal.html 3`] = ` +====================================options===================================== +htmlTopLevelIndent: "always" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + + + + +=====================================output===================================== + + + + + + + + +================================================================================ +`; + +exports[`template-literal.html 4`] = ` +====================================options===================================== +htmlTopLevelIndent: "never" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + + + + + + +=====================================output===================================== + + + + + + + + +================================================================================ +`; + +exports[`typescript.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`typescript.html 2`] = ` +====================================options===================================== +htmlTopLevelIndent: "auto" +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + -================================================================================ -`; +=====================================output===================================== + + interface Person { + firstName: string; + lastName: string; + } -=====================================output===================================== - + - -