.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / declaration-colon-newline-after / index.js
1 "use strict";
2
3 const declarationValueIndex = require("../../utils/declarationValueIndex");
4 const isStandardSyntaxDeclaration = require("../../utils/isStandardSyntaxDeclaration");
5 const report = require("../../utils/report");
6 const ruleMessages = require("../../utils/ruleMessages");
7 const validateOptions = require("../../utils/validateOptions");
8 const whitespaceChecker = require("../../utils/whitespaceChecker");
9
10 const ruleName = "declaration-colon-newline-after";
11
12 const messages = ruleMessages(ruleName, {
13   expectedAfter: () => 'Expected newline after ":"',
14   expectedAfterMultiLine: () =>
15     'Expected newline after ":" with a multi-line declaration'
16 });
17
18 const rule = function(expectation) {
19   const checker = whitespaceChecker("newline", expectation, messages);
20   return (root, result) => {
21     const validOptions = validateOptions(result, ruleName, {
22       actual: expectation,
23       possible: ["always", "always-multi-line"]
24     });
25     if (!validOptions) {
26       return;
27     }
28
29     root.walkDecls(decl => {
30       if (!isStandardSyntaxDeclaration(decl)) {
31         return;
32       }
33
34       // Get the raw prop, and only the prop
35       const endOfPropIndex =
36         declarationValueIndex(decl) + (decl.raws.between || "").length - 1;
37
38       // The extra characters tacked onto the end ensure that there is a character to check
39       // after the colon. Otherwise, with `background:pink` the character after the
40       const propPlusColon = decl.toString().slice(0, endOfPropIndex) + "xxx";
41
42       for (let i = 0, l = propPlusColon.length; i < l; i++) {
43         if (propPlusColon[i] !== ":") {
44           continue;
45         }
46         const indexToCheck =
47           propPlusColon.substr(propPlusColon[i], 3) === "/*"
48             ? propPlusColon.indexOf("*/", i) + 1
49             : i;
50
51         checker.afterOneOnly({
52           source: propPlusColon,
53           index: indexToCheck,
54           lineCheckStr: decl.value,
55           err: m => {
56             report({
57               message: m,
58               node: decl,
59               index: indexToCheck,
60               result,
61               ruleName
62             });
63           }
64         });
65       }
66     });
67   };
68 };
69
70 rule.ruleName = ruleName;
71 rule.messages = messages;
72 module.exports = rule;