.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / custom-property-empty-line-before / index.js
1 "use strict";
2
3 const addEmptyLineBefore = require("../../utils/addEmptyLineBefore");
4 const blockString = require("../../utils/blockString");
5 const getPreviousNonSharedLineCommentNode = require("../../utils/getPreviousNonSharedLineCommentNode");
6 const hasEmptyLine = require("../../utils/hasEmptyLine");
7 const isAfterComment = require("../../utils/isAfterComment");
8 const isCustomProperty = require("../../utils/isCustomProperty");
9 const isFirstNested = require("../../utils/isFirstNested");
10 const isSingleLineString = require("../../utils/isSingleLineString");
11 const isStandardSyntaxDeclaration = require("../../utils/isStandardSyntaxDeclaration");
12 const optionsMatches = require("../../utils/optionsMatches");
13 const removeEmptyLinesBefore = require("../../utils/removeEmptyLinesBefore");
14 const report = require("../../utils/report");
15 const ruleMessages = require("../../utils/ruleMessages");
16 const validateOptions = require("../../utils/validateOptions");
17
18 const ruleName = "custom-property-empty-line-before";
19
20 const messages = ruleMessages(ruleName, {
21   expected: "Expected empty line before custom property",
22   rejected: "Unexpected empty line before custom property"
23 });
24
25 const rule = function(expectation, options, context) {
26   return (root, result) => {
27     const validOptions = validateOptions(
28       result,
29       ruleName,
30       {
31         actual: expectation,
32         possible: ["always", "never"]
33       },
34       {
35         actual: options,
36         possible: {
37           except: ["first-nested", "after-comment", "after-custom-property"],
38           ignore: ["after-comment", "inside-single-line-block"]
39         },
40         optional: true
41       }
42     );
43     if (!validOptions) {
44       return;
45     }
46
47     root.walkDecls(decl => {
48       const prop = decl.prop,
49         parent = decl.parent;
50
51       if (!isStandardSyntaxDeclaration(decl)) {
52         return;
53       }
54       if (!isCustomProperty(prop)) {
55         return;
56       }
57
58       // Optionally ignore the node if a comment precedes it
59       if (
60         optionsMatches(options, "ignore", "after-comment") &&
61         isAfterComment(decl)
62       ) {
63         return;
64       }
65
66       // Optionally ignore nodes inside single-line blocks
67       if (
68         optionsMatches(options, "ignore", "inside-single-line-block") &&
69         isSingleLineString(blockString(parent))
70       ) {
71         return;
72       }
73
74       let expectEmptyLineBefore = expectation === "always" ? true : false;
75
76       // Optionally reverse the expectation if any exceptions apply
77       if (
78         (optionsMatches(options, "except", "first-nested") &&
79           isFirstNested(decl)) ||
80         (optionsMatches(options, "except", "after-comment") &&
81           isAfterComment(decl)) ||
82         (optionsMatches(options, "except", "after-custom-property") &&
83           isAfterCustomProperty(decl))
84       ) {
85         expectEmptyLineBefore = !expectEmptyLineBefore;
86       }
87
88       const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
89
90       // Return if the expectation is met
91       if (expectEmptyLineBefore === hasEmptyLineBefore) {
92         return;
93       }
94
95       // Fix
96       if (context.fix) {
97         if (expectEmptyLineBefore) {
98           addEmptyLineBefore(decl, context.newline);
99         } else {
100           removeEmptyLinesBefore(decl, context.newline);
101         }
102
103         return;
104       }
105
106       const message = expectEmptyLineBefore
107         ? messages.expected
108         : messages.rejected;
109       report({
110         message,
111         node: decl,
112         result,
113         ruleName
114       });
115     });
116   };
117 };
118
119 function isAfterCustomProperty(decl) {
120   const prevNode = getPreviousNonSharedLineCommentNode(decl);
121   return prevNode && prevNode.prop && isCustomProperty(prevNode.prop);
122 }
123
124 rule.ruleName = ruleName;
125 rule.messages = messages;
126 module.exports = rule;