.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / comment-whitespace-inside / index.js
1 "use strict";
2
3 const isWhitespace = require("../../utils/isWhitespace");
4 const report = require("../../utils/report");
5 const ruleMessages = require("../../utils/ruleMessages");
6 const validateOptions = require("../../utils/validateOptions");
7
8 const ruleName = "comment-whitespace-inside";
9
10 const messages = ruleMessages(ruleName, {
11   expectedOpening: 'Expected whitespace after "/*"',
12   rejectedOpening: 'Unexpected whitespace after "/*"',
13   expectedClosing: 'Expected whitespace before "*/"',
14   rejectedClosing: 'Unexpected whitespace before "*/"'
15 });
16
17 const rule = function(expectation) {
18   return function(root, result) {
19     const validOptions = validateOptions(result, ruleName, {
20       actual: expectation,
21       possible: ["always", "never"]
22     });
23     if (!validOptions) {
24       return;
25     }
26
27     root.walkComments(function(comment) {
28       if (comment.raws.inline || comment.inline) {
29         return;
30       }
31
32       const rawComment = comment.toString();
33       const firstFourChars = rawComment.substr(0, 4);
34
35       // Return early if sourcemap or copyright comment
36       if (/^\/\*[#!]\s/.test(firstFourChars)) {
37         return;
38       }
39
40       const leftMatches = rawComment.match(/(^\/\*+)(\s)?/);
41       const rightMatches = rawComment.match(/(\s)?(\*+\/)$/);
42       const opener = leftMatches[1];
43       const leftSpace = leftMatches[2] || "";
44       const rightSpace = rightMatches[1] || "";
45       const closer = rightMatches[2];
46
47       if (expectation === "never" && leftSpace !== "") {
48         complain(messages.rejectedOpening, opener.length);
49       }
50
51       if (expectation === "always" && !isWhitespace(leftSpace)) {
52         complain(messages.expectedOpening, opener.length);
53       }
54
55       if (expectation === "never" && rightSpace !== "") {
56         complain(
57           messages.rejectedClosing,
58           comment.toString().length - closer.length - 1
59         );
60       }
61
62       if (expectation === "always" && !isWhitespace(rightSpace)) {
63         complain(
64           messages.expectedClosing,
65           comment.toString().length - closer.length - 1
66         );
67       }
68
69       function complain(message, index) {
70         report({
71           message,
72           index,
73           result,
74           ruleName,
75           node: comment
76         });
77       }
78     });
79   };
80 };
81
82 rule.ruleName = ruleName;
83 rule.messages = messages;
84 module.exports = rule;