@atomicsmash/coding-standards 13.0.0-beta.2 → 13.0.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,189 @@
1
+ import baseConfig from "./base.mjs";
2
+
3
+ /**
4
+ *
5
+ * @param {({property: string|RegExp, varInfo: {type: "custom"|"preset", slug:string},shorthand?: 1 | 2 | 3 | 4 , additionalAllowedValues?: (string|RegExp)[]})[]} values
6
+ */
7
+ function createAllowedPropertyList(values) {
8
+ /**
9
+ * @type {Record<string,string[]>}
10
+ */
11
+ const allowedPropertyList = {};
12
+ for (const {
13
+ property,
14
+ varInfo,
15
+ shorthand = 1,
16
+ additionalAllowedValues = [],
17
+ } of values) {
18
+ const isRegexProperty = property instanceof RegExp;
19
+ const possibleValues = [];
20
+ const possibleVars = [
21
+ ...additionalAllowedValues.map((value) =>
22
+ value instanceof RegExp ? value.toString() : value,
23
+ ),
24
+ `(var\\([\\s]*--wp--${varInfo.type}--${varInfo.slug}--([a-z-0-9]+[\\s]*)\\))`,
25
+ ];
26
+ if (isRegexProperty) {
27
+ possibleVars.push(
28
+ `(var\\([\\s]*--[a-z-0-9]*${property.toString().slice(1, -1)}[a-z-0-9]*[\\s]*\\))`,
29
+ );
30
+ } else if (property) {
31
+ possibleVars.push(
32
+ `(var\\([\\s]*--[a-z-0-9]*${property}[a-z-0-9]*[\\s]*\\))`,
33
+ );
34
+ }
35
+ possibleValues.push(
36
+ // For each shorthand value, allow one of the possible values followed by a space, or allow it to be blank
37
+ `/^${shorthand > 1 ? `((${possibleVars.join("|")})[\\s]*){0,${shorthand - 1}}` : ""}(${possibleVars.join("|")})$/`,
38
+ );
39
+ allowedPropertyList[property.toString()] = possibleValues;
40
+ if (!isRegexProperty) {
41
+ allowedPropertyList[`/--[a-z-0-9]*${property}[a-z-0-9]*/`] =
42
+ possibleValues;
43
+ }
44
+ }
45
+ return allowedPropertyList;
46
+ }
47
+
48
+ const rules = {
49
+ "declaration-property-value-allowed-list": [
50
+ createAllowedPropertyList([
51
+ {
52
+ property: /border((-top|-bottom)(-left|-right))?-radius/,
53
+ varInfo: {
54
+ type: "custom",
55
+ slug: "radius",
56
+ },
57
+ },
58
+ {
59
+ property: "border-radius",
60
+ varInfo: {
61
+ type: "custom",
62
+ slug: "radius",
63
+ },
64
+ shorthand: 4,
65
+ },
66
+ {
67
+ property: "box-shadow",
68
+ varInfo: {
69
+ type: "preset",
70
+ slug: "shadow",
71
+ },
72
+ additionalAllowedValues: ["none", "unset"],
73
+ },
74
+ {
75
+ property: /color/,
76
+ varInfo: {
77
+ type: "preset",
78
+ slug: "color",
79
+ },
80
+ additionalAllowedValues: ["inherit", "currentcolor"],
81
+ },
82
+ {
83
+ property: "background-color",
84
+ varInfo: {
85
+ type: "preset",
86
+ slug: "color",
87
+ },
88
+ additionalAllowedValues: ["transparent"],
89
+ },
90
+ {
91
+ property: "font-family",
92
+ varInfo: {
93
+ type: "preset",
94
+ slug: "font-family",
95
+ },
96
+ additionalAllowedValues: ["inherit"],
97
+ },
98
+ {
99
+ property: "font-size",
100
+ varInfo: {
101
+ type: "preset",
102
+ slug: "font-size",
103
+ },
104
+ additionalAllowedValues: ["inherit"],
105
+ },
106
+ {
107
+ property: /(?<!scroll-)margin/,
108
+ varInfo: {
109
+ type: "preset",
110
+ slug: "spacing",
111
+ },
112
+ shorthand: 4,
113
+ additionalAllowedValues: ["auto", "var\\(--wp--style--block-gap\\)"],
114
+ },
115
+ {
116
+ property: /padding/,
117
+ varInfo: {
118
+ type: "preset",
119
+ slug: "spacing",
120
+ },
121
+ shorthand: 4,
122
+ },
123
+ {
124
+ property: "z-index",
125
+ varInfo: {
126
+ type: "custom",
127
+ slug: "z-index",
128
+ },
129
+ },
130
+ ]),
131
+ {
132
+ severity: "warning",
133
+ message: (property, value) => {
134
+ // Until updated, property and value will always return undefined.
135
+ return `Most values for this type of property should be defined in theme.json. (declaration-property-value-allowed-list)`;
136
+ // The following code is not complete, it has been left to show how this function could provide more helpful error
137
+ // messages once an update to stylelint is possible.
138
+ /* eslint-disable no-unreachable -- See above */
139
+ let message = `Invalid property provided for ${property}.`;
140
+ let ruleKey;
141
+ if (new RegExp(/color/).test(property)) {
142
+ ruleKey = "/color/";
143
+ } else if (new RegExp(/margin/).test(property)) {
144
+ ruleKey = "/margin/";
145
+ } else if (new RegExp(/padding/).test(property)) {
146
+ ruleKey = "/padding/";
147
+ } else {
148
+ ruleKey = property;
149
+ }
150
+ switch (ruleKey) {
151
+ case "z-index": {
152
+ message = `z-index must use one of the values specified in the theme.json file under \`settings.custom.z-index\`.`;
153
+ break;
154
+ }
155
+ case "/color/": {
156
+ message = `Colour values must use one of the values specified in the theme.json file under \`settings.color.palette\`.`;
157
+ break;
158
+ }
159
+ case "/margin/": {
160
+ message = `Margin values must use one of the values specified in the theme.json file under \`settings.spacing.spacingSizes\`.`;
161
+ break;
162
+ }
163
+ case "/padding/": {
164
+ message = `Padding values must use one of the values specified in the theme.json file under \`settings.spacing.spacingSizes\`.`;
165
+ break;
166
+ }
167
+ case "font-size": {
168
+ message = `Font size values must use one of the values specified in the theme.json file under \`settings.typography.fontSizes\`.`;
169
+ break;
170
+ }
171
+ case "font-family": {
172
+ message = `Font family values must use one of the values specified in the theme.json file under \`settings.typography.fontFamilies\`.`;
173
+ break;
174
+ }
175
+ }
176
+ return `${message} Value provided: ${value} (declaration-property-value-allowed-list)`;
177
+ /* eslint-enable no-unreachable */
178
+ },
179
+ },
180
+ ],
181
+ };
182
+
183
+ export default {
184
+ ...baseConfig,
185
+ rules: {
186
+ ...baseConfig.rules,
187
+ ...rules,
188
+ },
189
+ };