UNPKG

4.59 kBJavaScriptView Raw
1const matchesStringOrRegExp = require("./utils/matchesStringOrRegExp");
2const postcss = require("postcss");
3const stylelint = require("stylelint");
4const report = stylelint.utils.report;
5const ruleMessages = stylelint.utils.ruleMessages;
6const validateOptions = stylelint.utils.validateOptions;
7
8const ruleName = "plugin/declaration-block-no-ignored-properties";
9
10const messages = ruleMessages(ruleName, {
11 rejected: (ignore, cause) => `Unexpected "${ignore}" with "${cause}"`
12});
13
14const ignored = [
15 {
16 property: "display",
17 value: "inline",
18 ignoredProperties: [
19 "width",
20 "min-width",
21 "max-width",
22 "height",
23 "min-height",
24 "max-height",
25 "margin",
26 "margin-top",
27 "margin-bottom",
28 "overflow",
29 "overflow-x",
30 "overflow-y"
31 ]
32 },
33 {
34 property: "display",
35 value: "list-item",
36 ignoredProperties: ["vertical-align"]
37 },
38 {
39 property: "display",
40 value: "block",
41 ignoredProperties: ["vertical-align"]
42 },
43 {
44 property: "display",
45 value: "flex",
46 ignoredProperties: ["vertical-align"]
47 },
48 {
49 property: "display",
50 value: "table",
51 ignoredProperties: ["vertical-align"]
52 },
53 {
54 property: "display",
55 value:
56 "/^table-(row|row-group|column|column-group|header-group|footer-group|cell)$/",
57 ignoredProperties: [
58 "margin",
59 "margin-top",
60 "margin-right",
61 "margin-bottom",
62 "margin-left"
63 ]
64 },
65 {
66 property: "display",
67 value:
68 "/^table-(row|row-group|column|column-group|header-group|footer-group)$/",
69 ignoredProperties: [
70 "padding",
71 "padding-top",
72 "padding-right",
73 "padding-bottom",
74 "padding-left"
75 ]
76 },
77 {
78 property: "display",
79 value:
80 "/^table-(row|row-group|column|column-group|header-group|footer-group|caption)$/",
81 ignoredProperties: ["vertical-align"]
82 },
83 {
84 property: "display",
85 value: "/^table-(row|row-group)$/",
86 ignoredProperties: ["width", "min-width", "max-width"]
87 },
88 {
89 property: "display",
90 value: "/^table-(column|column-group)$/",
91 ignoredProperties: ["height", "min-height", "max-height"]
92 },
93 {
94 property: "float",
95 value: "left",
96 ignoredProperties: ["vertical-align"]
97 },
98 {
99 property: "float",
100 value: "right",
101 ignoredProperties: ["vertical-align"]
102 },
103 {
104 property: "position",
105 value: "static",
106 ignoredProperties: ["top", "right", "bottom", "left", "z-index"]
107 },
108 {
109 property: "position",
110 value: "absolute",
111 ignoredProperties: ["float", "clear", "vertical-align"]
112 },
113 {
114 property: "position",
115 value: "fixed",
116 ignoredProperties: ["float", "clear", "vertical-align"]
117 },
118 {
119 property: "list-style-type",
120 value: "none",
121 ignoredProperties: ["list-style-image"]
122 },
123 {
124 property: "overflow",
125 value: "visible",
126 ignoredProperties: ["resize"]
127 }
128];
129
130const rule = actual => {
131 return (root, result) => {
132 const validOptions = validateOptions(result, ruleName, { actual });
133
134 if (!validOptions) {
135 return;
136 }
137
138 root.walkRules(rule => {
139 const uniqueDecls = {};
140 rule.walkDecls(decl => {
141 uniqueDecls[decl.prop] = decl;
142 });
143
144 function check(prop, index) {
145 const decl = uniqueDecls[prop];
146 const value = decl.value;
147 const unprefixedProp = postcss.vendor.unprefixed(prop);
148 const unprefixedValue = postcss.vendor.unprefixed(value);
149
150 ignored.forEach(ignore => {
151 const matchProperty = matchesStringOrRegExp(
152 unprefixedProp.toLowerCase(),
153 ignore.property
154 );
155 const matchValue = matchesStringOrRegExp(
156 unprefixedValue.toLowerCase(),
157 ignore.value
158 );
159
160 if (!matchProperty || !matchValue) {
161 return;
162 }
163
164 const ignoredProperties = ignore.ignoredProperties;
165
166 decl.parent.nodes.forEach((node, nodeIndex) => {
167 if (
168 !node.prop ||
169 ignoredProperties.indexOf(node.prop.toLowerCase()) === -1 ||
170 index === nodeIndex
171 ) {
172 return;
173 }
174
175 report({
176 message: messages.rejected(node.prop, decl.toString()),
177 node,
178 result,
179 ruleName
180 });
181 });
182 });
183 }
184
185 Object.keys(uniqueDecls).forEach(check);
186 });
187 };
188};
189
190module.exports = stylelint.createPlugin(ruleName, rule);
191module.exports.ruleName = ruleName;
192module.exports.messages = messages;