.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / no-eol-whitespace / index.js
1 "use strict";
2
3 const isOnlyWhitespace = require("../../utils/isOnlyWhitespace");
4 const optionsMatches = require("../../utils/optionsMatches");
5 const report = require("../../utils/report");
6 const ruleMessages = require("../../utils/ruleMessages");
7 const styleSearch = require("style-search");
8 const validateOptions = require("../../utils/validateOptions");
9
10 const ruleName = "no-eol-whitespace";
11
12 const messages = ruleMessages(ruleName, {
13   rejected: "Unexpected whitespace at end of line"
14 });
15
16 const whitespacesToReject = new Set([" ", "\t"]);
17
18 const rule = function(on, options) {
19   return (root, result) => {
20     const validOptions = validateOptions(
21       result,
22       ruleName,
23       {
24         actual: on
25       },
26       {
27         optional: true,
28         actual: options,
29         possible: {
30           ignore: ["empty-lines"]
31         }
32       }
33     );
34     if (!validOptions) {
35       return;
36     }
37
38     const rootString = root.toString();
39     styleSearch(
40       {
41         source: rootString,
42         target: ["\n", "\r"],
43         comments: "check"
44       },
45       match => {
46         // If the character before newline is not whitespace, ignore
47         if (!whitespacesToReject.has(rootString[match.startIndex - 1])) {
48           return;
49         }
50
51         if (optionsMatches(options, "ignore", "empty-lines")) {
52           // If there is only whitespace between the previous newline and
53           // this newline, ignore
54           const lineBefore = rootString.substring(
55             match.startIndex + 1,
56             rootString.lastIndexOf("\n", match.startIndex - 1)
57           );
58           if (isOnlyWhitespace(lineBefore)) {
59             return;
60           }
61         }
62
63         report({
64           message: messages.rejected,
65           node: root,
66           index: match.startIndex - 1,
67           result,
68           ruleName
69         });
70       }
71     );
72   };
73 };
74
75 rule.ruleName = ruleName;
76 rule.messages = messages;
77 module.exports = rule;