diff --git a/lib/rules/indentation/__tests__/at-rules.js b/lib/rules/indentation/__tests__/at-rules.js index a2bc195a94..295f2f4ed5 100644 --- a/lib/rules/indentation/__tests__/at-rules.js +++ b/lib/rules/indentation/__tests__/at-rules.js @@ -13,10 +13,14 @@ testRule(rule, { accept: [ { code: "@media print {\n" + " a {\n" + " color: pink;\n" + " }\n" + "}", + }, { + code: "@media\n" + " print {\n" + " a {\n" + " color: pink;\n" + " }\n" + "}", }, { code: "@media print {\n" + " a {\n" + " color: pink;\n" + " }\n" + "}\n" + "\n" + "@media screen {\n" + " b { color: orange; }\n" + "}", }, { code: "@media print {\r\n" + " a {\r\n" + " color: pink;\r\n" + " }\r\n" + "}", + }, { + code: "@media\r\n" + " print {\r\n" + " a {\r\n" + " color: pink;\r\n" + " }\r\n" + "}", }, { code: "@media print {\r\n" + " a {\r\n" + " color: pink;\r\n" + " }\r\n" + "}\r\n" + "\r\n" + "@media screen {\r\n" + " b { color: orange; }\r\n" + "}", } ], @@ -91,6 +95,22 @@ testRule(rule, { message: messages.expected("0 spaces"), line: 5, column: 2, + }, { + code: "@media\n" + " print {\n" + " a {\n" + " color: pink;\n" + " }\n" + "}", + fixed: "@media\n" + " print {\n" + " a {\n" + " color: pink;\n" + " }\n" + "}", + description: "at-rule parameters on the next line", + + message: messages.expected("2 spaces"), + line: 2, + column: 2, + }, { + code: "@media\r\n" + " print {\r\n" + " a {\r\n" + " color: pink;\r\n" + " }\r\n" + "}", + fixed: "@media\r\n" + " print {\r\n" + " a {\r\n" + " color: pink;\r\n" + " }\r\n" + "}", + description: "at-rule parameters on the next line (CRLF)", + + message: messages.expected("2 spaces"), + line: 2, + column: 2, } ], }) @@ -119,14 +139,22 @@ testRule(rule, { message: messages.expected("1 tab"), line: 4, column: 1, - // }, { - // code: "@media print,\n" + " (-webkit-min-device-pixel-ratio: 1.25),\n" + "\t(min-resolution: 120dpi) {}", - // fixed: "@media print,\n" + " (-webkit-min-device-pixel-ratio: 1.25),\n" + "\t(min-resolution: 120dpi) {}", - - // description: "multi-line at-rule params", - // message: messages.expected("1 tab"), - // line: 2, - // column: 3, + }, { + code: "@media print,\n" + " (-webkit-min-device-pixel-ratio: 1.25),\n" + "\t(min-resolution: 120dpi) {}", + fixed: "@media print,\n" + "\t(-webkit-min-device-pixel-ratio: 1.25),\n" + "\t(min-resolution: 120dpi) {}", + + description: "multi-line at-rule params", + message: messages.expected("1 tab"), + line: 2, + column: 3, + }, { + code: "@media print,\r\n" + " (-webkit-min-device-pixel-ratio: 1.25),\r\n" + "\t(min-resolution: 120dpi) {}", + fixed: "@media print,\r\n" + "\t(-webkit-min-device-pixel-ratio: 1.25),\r\n" + "\t(min-resolution: 120dpi) {}", + + description: "multi-line at-rule params (CRLF)", + message: messages.expected("1 tab"), + line: 2, + column: 3, } ], }) @@ -139,14 +167,23 @@ testRule(rule, { code: "@media print,\n" + "(-webkit-min-device-pixel-ratio: 1.25),\n" + "(min-resolution: 120dpi) {}", }], - // reject: [{ - // code: "@media print,\n" + " (-webkit-min-device-pixel-ratio: 1.25),\n" + "(min-resolution: 120dpi) {}", - // fixed: "@media print,\n" + " (-webkit-min-device-pixel-ratio: 1.25),\n" + "(min-resolution: 120dpi) {}", + reject: [ { + code: "@media print,\n" + " (-webkit-min-device-pixel-ratio: 1.25),\n" + "(min-resolution: 120dpi) {}", + fixed: "@media print,\n" + "(-webkit-min-device-pixel-ratio: 1.25),\n" + "(min-resolution: 120dpi) {}", + + description: "multi-line at-rule params, no params indent", + message: messages.expected("0 spaces"), + line: 2, + column: 3, + }, { + code: "@media print,\r\n" + " (-webkit-min-device-pixel-ratio: 1.25),\r\n" + "(min-resolution: 120dpi) {}", + fixed: "@media print,\r\n" + "(-webkit-min-device-pixel-ratio: 1.25),\r\n" + "(min-resolution: 120dpi) {}", - // message: messages.expected("0 spaces"), - // line: 2, - // column: 3, - // }], + description: "multi-line at-rule params, no params indent (CRLF)", + message: messages.expected("0 spaces"), + line: 2, + column: 3, + } ], }) testRule(rule, { diff --git a/lib/rules/indentation/__tests__/functions.js b/lib/rules/indentation/__tests__/functions.js index bd89cb92b2..6af0ec4811 100644 --- a/lib/rules/indentation/__tests__/functions.js +++ b/lib/rules/indentation/__tests__/functions.js @@ -1,6 +1,6 @@ "use strict" -// const messages = require("..").messages +const messages = require("..").messages const ruleName = require("..").ruleName const rules = require("../../../rules") @@ -40,59 +40,66 @@ testRule(rule, { description: "nested parenthetical inside multiline value", } ], - // reject: [ { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + "0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + "0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // message: messages.expected("4 spaces"), - // line: 4, - // column: 1, - // }, { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // message: messages.expected("2 spaces"), - // line: 6, - // column: 5, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 4, - // column: 2, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // description: "sass-list", - // message: messages.expected("0 spaces"), - // line: 5, - // column: 3, - // }, { - // code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + "0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + "0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // message: messages.expected("4 spaces"), - // line: 4, - // column: 1, - // }, { - // code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // message: messages.expected("2 spaces"), - // line: 6, - // column: 5, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 4, - // column: 2, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // description: "sass-list", - // message: messages.expected("0 spaces"), - // line: 5, - // column: 3, - // } ], + reject: [ { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + "0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + message: messages.expected("4 spaces"), + line: 4, + column: 1, + }, { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + message: messages.expected("2 spaces"), + line: 6, + column: 5, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 4, + column: 2, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", + description: "sass-list", + message: messages.expected("0 spaces"), + line: 5, + column: 3, + }, { + code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + "0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + message: messages.expected("4 spaces"), + line: 4, + column: 1, + }, { + code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + message: messages.expected("2 spaces"), + line: 6, + column: 5, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 4, + column: 2, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", + description: "sass-list", + message: messages.expected("0 spaces"), + line: 5, + column: 3, + }, { + code: "background:\n" + "linear-gradient(\n" + " to bottom,\n" + " transparentize($gray-dark, 1) 0%,\n" + " transparentize($gray-dark, 0.1) 100%\n" + " );", + fixed: "background:\n" + " linear-gradient(\n" + " to bottom,\n" + " transparentize($gray-dark, 1) 0%,\n" + " transparentize($gray-dark, 0.1) 100%\n" + " );", + + message: messages.expected("2 spaces"), + line: 2, + column: 1, + } ], }) testRule(rule, { @@ -128,14 +135,14 @@ testRule(rule, { description: "tabbed sass-list with property value pairs", }], - // reject: [{ - // code: "$some-list: (\n" + "\tvar: value,\n" + "\tvar: value,\n" + "\t\tvar: value\n" + ");", - // fixed: "$some-list: (\n" + "\tvar: value,\n" + "\tvar: value,\n" + "\t\tvar: value\n" + ");", - // description: "tabbed sass-list with property value pairs", - // message: messages.expected("1 tab"), - // line: 4, - // column: 3, - // }], + reject: [{ + code: "$some-list: (\n" + "\tvar: value,\n" + "\tvar: value,\n" + "\t\tvar: value\n" + ");", + fixed: "$some-list: (\n" + "\tvar: value,\n" + "\tvar: value,\n" + "\tvar: value\n" + ");", + description: "tabbed sass-list with property value pairs", + message: messages.expected("1 tab"), + line: 4, + column: 3, + }], }) testRule(rule, { @@ -156,47 +163,47 @@ testRule(rule, { description: "sass-list", } ], - // reject: [ { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // message: messages.expected("6 spaces"), - // line: 4, - // column: 5, - // }, { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // message: messages.expected("4 spaces"), - // line: 6, - // column: 6, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // description: "sass-list", - // message: messages.expected("4 spaces"), - // line: 4, - // column: 4, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 5, - // column: 2, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // description: "sass-list", - // message: messages.expected("4 spaces"), - // line: 4, - // column: 4, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 5, - // column: 2, - // } ], + reject: [ { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + message: messages.expected("6 spaces"), + line: 4, + column: 5, + }, { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + message: messages.expected("4 spaces"), + line: 6, + column: 6, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + description: "sass-list", + message: messages.expected("4 spaces"), + line: 4, + column: 4, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 5, + column: 2, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + description: "sass-list", + message: messages.expected("4 spaces"), + line: 4, + column: 4, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 5, + column: 2, + } ], }) testRule(rule, { @@ -217,45 +224,45 @@ testRule(rule, { description: "sass-list", } ], - // reject: [ { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // message: messages.expected("6 spaces"), - // line: 4, - // column: 5, - // }, { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", - // message: messages.expected("4 spaces"), - // line: 6, - // column: 6, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // description: "sass-list", - // message: messages.expected("0 spaces"), - // line: 5, - // column: 3, - // }, { - // code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // message: messages.expected("6 spaces"), - // line: 4, - // column: 5, - // }, { - // code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", - // message: messages.expected("4 spaces"), - // line: 6, - // column: 6, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // description: "sass-list", - // message: messages.expected("0 spaces"), - // line: 5, - // column: 3, - // } ], + reject: [ { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + message: messages.expected("6 spaces"), + line: 4, + column: 5, + }, { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + "}", + message: messages.expected("4 spaces"), + line: 6, + column: 6, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", + description: "sass-list", + message: messages.expected("0 spaces"), + line: 5, + column: 3, + }, { + code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + message: messages.expected("6 spaces"), + line: 4, + column: 5, + }, { + code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + "}", + message: messages.expected("4 spaces"), + line: 6, + column: 6, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", + description: "sass-list", + message: messages.expected("0 spaces"), + line: 5, + column: 3, + } ], }) testRule(rule, { @@ -277,73 +284,73 @@ testRule(rule, { description: "sass-list", } ], - // reject: [ { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", - // message: messages.expected("6 spaces"), - // line: 4, - // column: 5, - // }, { - // code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", - // fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", - // message: messages.expected("6 spaces"), - // line: 6, - // column: 6, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 4, - // column: 2, - // }, { - // code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", - // fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 5, - // column: 1, - // }, { - // code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", - // fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", - // message: messages.expected("6 spaces"), - // line: 4, - // column: 5, - // }, { - // code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", - // fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", - // message: messages.expected("6 spaces"), - // line: 6, - // column: 6, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 4, - // column: 2, - // }, { - // code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", - // fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", - // description: "sass-list", - // message: messages.expected("2 spaces"), - // line: 5, - // column: 1, - // } ], + reject: [ { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", + message: messages.expected("6 spaces"), + line: 4, + column: 5, + }, { + code: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", + fixed: ".foo {\n" + " color: rgb(\n" + " 0,\n" + " 0,\n" + " 0\n" + " );\n" + " top: 0;\n" + " }", + message: messages.expected("6 spaces"), + line: 6, + column: 6, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 4, + column: 2, + }, { + code: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + ");", + fixed: "$some-list: (\n" + " 0,\n" + " 0,\n" + " 0\n" + " );", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 5, + column: 1, + }, { + code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", + fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", + message: messages.expected("6 spaces"), + line: 4, + column: 5, + }, { + code: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", + fixed: ".foo {\r\n" + " color: rgb(\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );\r\n" + " top: 0;\r\n" + " }", + message: messages.expected("6 spaces"), + line: 6, + column: 6, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 4, + column: 2, + }, { + code: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + ");", + fixed: "$some-list: (\r\n" + " 0,\r\n" + " 0,\r\n" + " 0\r\n" + " );", + description: "sass-list", + message: messages.expected("2 spaces"), + line: 5, + column: 1, + } ], }) -// testRule(rule, { -// ruleName, -// config: [2], -// syntax: "less", -// skipBasicChecks: true, -// fix: true, +testRule(rule, { + ruleName, + config: [2], + syntax: "less", + skipBasicChecks: true, + // fix: true, -// accept: [ { -// code: ".foo {\n" + " .mixin(\n" + " @foo,\n" + " @bar,\n" + " @baz\n" + " );\n" + "}", -// description: "Less mixin with multi-line arguments", -// }, { -// code: ".foo {\r\n" + " .mixin(\r\n" + " @foo,\r\n" + " @bar,\r\n" + " @baz\r\n" + " );\r\n" + "}", -// description: "Less mixin with multi-line arguments", -// } ], -// }) + accept: [ { + code: ".foo {\n" + " .mixin(\n" + " @foo,\n" + " @bar,\n" + " @baz\n" + " );\n" + "}", + description: "Less mixin with multi-line arguments", + }, { + code: ".foo {\r\n" + " .mixin(\r\n" + " @foo,\r\n" + " @bar,\r\n" + " @baz\r\n" + " );\r\n" + "}", + description: "Less mixin with multi-line arguments", + } ], +}) diff --git a/lib/rules/indentation/__tests__/rules.js b/lib/rules/indentation/__tests__/rules.js index 746ace36ed..cf09554ac8 100644 --- a/lib/rules/indentation/__tests__/rules.js +++ b/lib/rules/indentation/__tests__/rules.js @@ -47,6 +47,8 @@ testRule(rule, { code: "* { top: 0; }", }, { code: "@media print {\n" + " * { color: pink; }\n" + "}", + }, { + code: "a {\n" + " @media print { color: pink; }\n" + "}", }, { code: "/* anything\r\n" + " goes\r\n" + "\t\t\twithin a comment */\r\n" + "", }, { @@ -146,20 +148,20 @@ testRule(rule, { message: messages.expected("2 spaces"), line: 3, column: 2, - // }, { - // code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - // fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + }, { + code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - // message: messages.expected("4 spaces"), - // line: 3, - // column: 3, - // }, { - // code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - // fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + message: messages.expected("4 spaces"), + line: 3, + column: 3, + }, { + code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - // message: messages.expected("4 spaces"), - // line: 4, - // column: 3, + message: messages.expected("4 spaces"), + line: 4, + column: 3, }, { code: "@media print {\n" + " * { color: pink; }\n" + "}", fixed: "@media print {\n" + " * { color: pink; }\n" + "}", @@ -167,6 +169,13 @@ testRule(rule, { message: messages.expected("2 spaces"), line: 2, column: 4, + }, { + code: "a {\n" + " @media print { color: pink; }\n" + "}", + fixed: "a {\n" + " @media print { color: pink; }\n" + "}", + + message: messages.expected("2 spaces"), + line: 2, + column: 2, }, { code: "\ta {\r\n" + " color: pink;\r\n" + "}", fixed: "a {\r\n" + " color: pink;\r\n" + "}", @@ -230,20 +239,20 @@ testRule(rule, { message: messages.expected("2 spaces"), line: 3, column: 2, - // }, { - // code: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", - // fixed: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", + }, { + code: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", + fixed: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", - // message: messages.expected("4 spaces"), - // line: 3, - // column: 3, - // }, { - // code: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", - // fixed: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", + message: messages.expected("4 spaces"), + line: 3, + column: 3, + }, { + code: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", + fixed: "a {\r\n" + " background-position: top left,\r\n" + " top right,\r\n" + " bottom left;\r\n" + " color: pink;\r\n" + "}", - // message: messages.expected("4 spaces"), - // line: 4, - // column: 3, + message: messages.expected("4 spaces"), + line: 4, + column: 3, }, { code: "@media print {\r\n" + " * { color: pink; }\r\n" + "}", fixed: "@media print {\r\n" + " * { color: pink; }\r\n" + "}", @@ -341,21 +350,21 @@ testRule(rule, { code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", } ], - // reject: [ { - // code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - // fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - - // message: messages.expected("2 spaces"), - // line: 3, - // column: 5, - // }, { - // code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - // fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", - - // message: messages.expected("2 spaces"), - // line: 4, - // column: 5, - // } ], + reject: [ { + code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + + message: messages.expected("2 spaces"), + line: 3, + column: 5, + }, { + code: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + fixed: "a {\n" + " background-position: top left,\n" + " top right,\n" + " bottom left;\n" + " color: pink;\n" + "}", + + message: messages.expected("2 spaces"), + line: 4, + column: 5, + } ], }) testRule(rule, { diff --git a/lib/rules/indentation/__tests__/selectors.js b/lib/rules/indentation/__tests__/selectors.js index e99955c85c..8d650bd70b 100644 --- a/lib/rules/indentation/__tests__/selectors.js +++ b/lib/rules/indentation/__tests__/selectors.js @@ -33,68 +33,68 @@ testRule(rule, { } ], reject: [ { - // code: "a,\n" + " b { color: pink; }", - // fixed: "a,\n" + " b { color: pink; }", + code: "a,\n" + " b { color: pink; }", + fixed: "a,\n" + "b { color: pink; }", - // message: messages.expected("0 spaces"), - // line: 2, - // column: 3, - // }, { - // code: "a,\n" + "b,\n" + " c { color: pink; }", - // fixed: "a,\n" + "b,\n" + " c { color: pink; }", + message: messages.expected("0 spaces"), + line: 2, + column: 3, + }, { + code: "a,\n" + "b,\n" + " c { color: pink; }", + fixed: "a,\n" + "b,\n" + "c { color: pink; }", - // message: messages.expected("0 spaces"), - // line: 3, - // column: 2, - // }, { - // code: "@media print {\n" + " a,\n" + "b { color: pink;}\n" + "}", - // fixed: "@media print {\n" + " a,\n" + "b { color: pink;}\n" + "}", + message: messages.expected("0 spaces"), + line: 3, + column: 2, + }, { + code: "@media print {\n" + " a,\n" + "b { color: pink;}\n" + "}", + fixed: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", - // message: messages.expected("2 spaces"), - // line: 3, - // column: 1, - // }, { - // code: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", - // fixed: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", + message: messages.expected("2 spaces"), + line: 3, + column: 1, + }, { + code: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", + fixed: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", - // message: messages.expected("2 spaces"), - // line: 3, - // column: 4, - // }, { + message: messages.expected("2 spaces"), + line: 3, + column: 4, + }, { code: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", fixed: "@media print {\n" + " a,\n" + " b { color: pink;}\n" + "}", message: messages.expected("2 spaces"), line: 2, column: 4, - // }, { - // code: "a,\r\n" + " b { color: pink; }", - // fixed: "a,\r\n" + " b { color: pink; }", + }, { + code: "a,\r\n" + " b { color: pink; }", + fixed: "a,\r\n" + "b { color: pink; }", - // message: messages.expected("0 spaces"), - // line: 2, - // column: 3, - // }, { - // code: "a,\r\n" + "b,\r\n" + " c { color: pink; }", - // fixed: "a,\r\n" + "b,\r\n" + " c { color: pink; }", + message: messages.expected("0 spaces"), + line: 2, + column: 3, + }, { + code: "a,\r\n" + "b,\r\n" + " c { color: pink; }", + fixed: "a,\r\n" + "b,\r\n" + "c { color: pink; }", - // message: messages.expected("0 spaces"), - // line: 3, - // column: 2, - // }, { - // code: "@media print {\r\n" + " a,\r\n" + "b { color: pink;}\r\n" + "}", - // fixed: "@media print {\r\n" + " a,\r\n" + "b { color: pink;}\r\n" + "}", + message: messages.expected("0 spaces"), + line: 3, + column: 2, + }, { + code: "@media print {\r\n" + " a,\r\n" + "b { color: pink;}\r\n" + "}", + fixed: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", - // message: messages.expected("2 spaces"), - // line: 3, - // column: 1, - // }, { - // code: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", - // fixed: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", + message: messages.expected("2 spaces"), + line: 3, + column: 1, + }, { + code: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", + fixed: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", - // message: messages.expected("2 spaces"), - // line: 3, - // column: 4, + message: messages.expected("2 spaces"), + line: 3, + column: 4, }, { code: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", fixed: "@media print {\r\n" + " a,\r\n" + " b { color: pink;}\r\n" + "}", diff --git a/lib/rules/indentation/index.js b/lib/rules/indentation/index.js index a78dccf1ff..84d1c45a78 100644 --- a/lib/rules/indentation/index.js +++ b/lib/rules/indentation/index.js @@ -211,6 +211,9 @@ const rule = function (space, options, context) { return } + // Data for current node fixing + const fixPositions = [] + // `outsideParens` because function arguments and also non-standard parenthesized stuff like // Sass maps are ignored to allow for arbitrary indentation let parentheticalDepth = 0 @@ -290,17 +293,88 @@ const rule = function (space, options, context) { } const afterNewlineSpace = afterNewlineSpaceMatches[1] + const expectedIndentation = _.repeat(indentChar, expectedIndentLevel) + + if (afterNewlineSpace !== expectedIndentation) { + if (context.fix) { + // Adding fixes position in reverse order, because if we change indent in the beginning of the string it will break all following fixes for that string + fixPositions.unshift({ + expectedIndentation, + currentIndentation: afterNewlineSpace, + startIndex: match.startIndex, + }) + } else { + report({ + message: messages.expected(legibleExpectation(expectedIndentLevel)), + node, + index: match.startIndex + afterNewlineSpace.length + 1, + result, + ruleName, + }) + } + } + }) - if (afterNewlineSpace !== _.repeat(indentChar, expectedIndentLevel)) { - report({ - message: messages.expected(legibleExpectation(expectedIndentLevel)), - node, - index: match.startIndex + afterNewlineSpace.length + 1, - result, - ruleName, + if (fixPositions.length) { + if (node.type === "rule") { + fixPositions.forEach(function (fixPosition) { + node.selector = replaceIndentation( + node.selector, + fixPosition.currentIndentation, + fixPosition.expectedIndentation, + fixPosition.startIndex + ) }) } - }) + + if (node.type === "decl") { + const declProp = node.prop + const declBetween = node.raws.between + + fixPositions.forEach(function (fixPosition) { + if (fixPosition.startIndex < (declProp.length + declBetween.length)) { + node.raws.between = replaceIndentation( + declBetween, + fixPosition.currentIndentation, + fixPosition.expectedIndentation, + fixPosition.startIndex - declProp.length + ) + } else { + node.value = replaceIndentation( + node.value, + fixPosition.currentIndentation, + fixPosition.expectedIndentation, + fixPosition.startIndex - declProp.length - declBetween.length + ) + } + }) + } + + if (node.type === "atrule") { + const atRuleName = node.name + const atRuleAfterName = node.raws.afterName + const atRuleParams = node.params + + fixPositions.forEach(function (fixPosition) { + // 1 — it's a @ length + if (fixPosition.startIndex < (1 + atRuleName.length + atRuleAfterName.length)) { + node.raws.afterName = replaceIndentation( + atRuleAfterName, + fixPosition.currentIndentation, + fixPosition.expectedIndentation, + fixPosition.startIndex - atRuleName.length - 1 + ) + } else { + node.params = replaceIndentation( + atRuleParams, + fixPosition.currentIndentation, + fixPosition.expectedIndentation, + fixPosition.startIndex - atRuleName.length - atRuleAfterName.length - 1 + ) + } + }) + } + } } } @@ -334,6 +408,14 @@ function fixIndentation(str, whitespace) { return stringStart + whitespace + stringEnd } +function replaceIndentation(input, searchString, replaceString, startIndex) { + const offset = startIndex + 1 + const stringStart = input.slice(0, offset) + const stringEnd = input.slice(offset + searchString.length) + + return stringStart + replaceString + stringEnd +} + rule.ruleName = ruleName rule.messages = messages module.exports = rule