1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const report = require('../../utils/report');
|
6 | const ruleMessages = require('../../utils/ruleMessages');
|
7 | const validateOptions = require('../../utils/validateOptions');
|
8 |
|
9 | const ruleName = 'no-irregular-whitespace';
|
10 | const messages = ruleMessages(ruleName, {
|
11 | unexpected: 'Unexpected irregular whitespace',
|
12 | });
|
13 |
|
14 | const IRREGULAR_WHITESPACES = [
|
15 | '\u000B',
|
16 | '\u000C',
|
17 | '\u00A0',
|
18 | '\u0085',
|
19 | '\u1680',
|
20 | '\u180E',
|
21 | '\uFEFF',
|
22 | '\u2000',
|
23 | '\u2001',
|
24 | '\u2002',
|
25 | '\u2003',
|
26 | '\u2004',
|
27 | '\u2005',
|
28 | '\u2006',
|
29 | '\u2007',
|
30 | '\u2008',
|
31 | '\u2009',
|
32 | '\u200A',
|
33 | '\u200B',
|
34 | '\u2028',
|
35 | '\u2029',
|
36 | '\u202F',
|
37 | '\u205F',
|
38 | '\u3000',
|
39 | ];
|
40 |
|
41 | const IRREGULAR_WHITESPACES_PATTERN = new RegExp(`([${IRREGULAR_WHITESPACES.join('')}])`);
|
42 |
|
43 | const generateInvalidWhitespaceValidator = () => {
|
44 | return (str) => typeof str === 'string' && IRREGULAR_WHITESPACES_PATTERN.exec(str);
|
45 | };
|
46 |
|
47 | const declarationSchema = {
|
48 | prop: 'string',
|
49 | value: 'string',
|
50 | raws: {
|
51 | before: 'string',
|
52 | between: 'string',
|
53 | },
|
54 | };
|
55 |
|
56 | const atRuleSchema = {
|
57 | name: 'string',
|
58 | params: 'string',
|
59 | raws: {
|
60 | before: 'string',
|
61 | between: 'string',
|
62 | afterName: 'string',
|
63 | after: 'string',
|
64 | },
|
65 | };
|
66 |
|
67 | const ruleSchema = {
|
68 | selector: 'string',
|
69 | raws: {
|
70 | before: 'string',
|
71 | between: 'string',
|
72 | after: 'string',
|
73 | },
|
74 | };
|
75 |
|
76 | const generateNodeValidator = (nodeSchema, validator) => {
|
77 | const allKeys = Object.keys(nodeSchema);
|
78 | const validatorForKey = {};
|
79 |
|
80 | allKeys.forEach((key) => {
|
81 | if (typeof nodeSchema[key] === 'string') validatorForKey[key] = validator;
|
82 |
|
83 | if (typeof nodeSchema[key] === 'object')
|
84 | validatorForKey[key] = generateNodeValidator(nodeSchema[key], validator);
|
85 | });
|
86 |
|
87 |
|
88 |
|
89 | return (node) => {
|
90 | for (const currentKey of allKeys) {
|
91 | if (validatorForKey[currentKey](node[currentKey])) {
|
92 | return validatorForKey[currentKey](node[currentKey]);
|
93 | }
|
94 | }
|
95 | };
|
96 | };
|
97 |
|
98 | function rule(on) {
|
99 | return (root, result) => {
|
100 | const validOptions = validateOptions(result, ruleName, { actual: on });
|
101 |
|
102 | if (!validOptions) {
|
103 | return;
|
104 | }
|
105 |
|
106 | const genericValidator = generateInvalidWhitespaceValidator();
|
107 |
|
108 | const validate = (node, validator) => {
|
109 | const issue = validator(node);
|
110 |
|
111 | if (issue) {
|
112 | report({
|
113 | ruleName,
|
114 | result,
|
115 | message: messages.unexpected,
|
116 | node,
|
117 | word: issue[1],
|
118 | });
|
119 | }
|
120 | };
|
121 |
|
122 | const atRuleValidator = generateNodeValidator(atRuleSchema, genericValidator);
|
123 | const ruleValidator = generateNodeValidator(ruleSchema, genericValidator);
|
124 | const declValidator = generateNodeValidator(declarationSchema, genericValidator);
|
125 |
|
126 | root.walkAtRules((atRule) => validate(atRule, atRuleValidator));
|
127 | root.walkRules((selector) => validate(selector, ruleValidator));
|
128 | root.walkDecls((declaration) => validate(declaration, declValidator));
|
129 | };
|
130 | }
|
131 |
|
132 | rule.ruleName = ruleName;
|
133 | rule.messages = messages;
|
134 | module.exports = rule;
|