1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
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 = 'color-no-hex';
|
11 |
|
12 | const messages = ruleMessages(ruleName, {
|
13 | rejected: (hex) => `Unexpected hex color "${hex}"`,
|
14 | });
|
15 |
|
16 | function rule(actual) {
|
17 | return (root, result) => {
|
18 | const validOptions = validateOptions(result, ruleName, { actual });
|
19 |
|
20 | if (!validOptions) {
|
21 | return;
|
22 | }
|
23 |
|
24 | root.walkDecls((decl) => {
|
25 | const declString = decl.toString();
|
26 |
|
27 | styleSearch({ source: declString, target: '#' }, (match) => {
|
28 |
|
29 |
|
30 |
|
31 | if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
|
32 | return;
|
33 | }
|
34 |
|
35 | const hexMatch = /^#[0-9A-Za-z]+/.exec(declString.substr(match.startIndex));
|
36 |
|
37 | if (!hexMatch) {
|
38 | return;
|
39 | }
|
40 |
|
41 | const hexValue = hexMatch[0];
|
42 |
|
43 | report({
|
44 | message: messages.rejected(hexValue),
|
45 | node: decl,
|
46 | index: match.startIndex,
|
47 | result,
|
48 | ruleName,
|
49 | });
|
50 | });
|
51 | });
|
52 | };
|
53 | }
|
54 |
|
55 | rule.ruleName = ruleName;
|
56 | rule.messages = messages;
|
57 | module.exports = rule;
|