forked from destefanis/design-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintingFunctions.ts
414 lines (364 loc) · 11 KB
/
lintingFunctions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Linting functions
// Generic function for creating an error object to pass to the app.
export function createErrorObject(node, type, message, value?) {
let error = {
message: "",
type: "",
node: "",
value: ""
};
error.message = message;
error.type = type;
error.node = node;
if (value !== undefined) {
error.value = value;
}
return error;
}
// Determine a nodes fills
export function determineFill(fills) {
let fillValues = [];
fills.forEach(fill => {
if (fill.type === "SOLID") {
let rgbObj = convertColor(fill.color);
fillValues.push(RGBToHex(rgbObj["r"], rgbObj["g"], rgbObj["b"]));
} else if (fill.type === "IMAGE") {
fillValues.push("Image - " + fill.imageHash);
} else if (fill.type === "VIDEO") {
fillValues.push("Video Fill");
} else {
const gradientValues = [];
fill.gradientStops.forEach(gradientStops => {
let gradientColorObject = convertColor(gradientStops.color);
gradientValues.push(
RGBToHex(
gradientColorObject["r"],
gradientColorObject["g"],
gradientColorObject["b"]
)
);
});
let gradientValueString = gradientValues.toString();
fillValues.push(`${fill.type} ${gradientValueString}`);
}
});
return fillValues[0];
}
// Lint border radius
export function checkRadius(node, errors, radiusValues) {
let cornerType = node.cornerRadius;
if (typeof cornerType !== "symbol") {
if (cornerType === 0) {
return;
}
if (cornerType === node.height) {
return;
}
}
if (typeof node.boundVariables.bottomLeftRadius !== "undefined") {
return;
}
// If the radius isn't even on all sides, check each corner.
if (typeof cornerType === "symbol") {
if (radiusValues.indexOf(node.topLeftRadius) === -1) {
return errors.push(
createErrorObject(
node,
"radius",
"Incorrect Top Left Radius",
node.topRightRadius
)
);
} else if (radiusValues.indexOf(node.topRightRadius) === -1) {
return errors.push(
createErrorObject(
node,
"radius",
"Incorrect top right radius",
node.topRightRadius
)
);
} else if (radiusValues.indexOf(node.bottomLeftRadius) === -1) {
return errors.push(
createErrorObject(
node,
"radius",
"Incorrect bottom left radius",
node.bottomLeftRadius
)
);
} else if (radiusValues.indexOf(node.bottomRightRadius) === -1) {
return errors.push(
createErrorObject(
node,
"radius",
"Incorrect bottom right radius",
node.bottomRightRadius
)
);
} else {
return;
}
} else {
if (radiusValues.indexOf(node.cornerRadius) === -1) {
return errors.push(
createErrorObject(
node,
"radius",
"Incorrect border radius",
node.cornerRadius
)
);
} else {
return;
}
}
}
// Custom Lint rule that isn't being used yet!
// that ensures our text fills aren't using styles (design tokens) meant for backgrounds.
export function customCheckTextFills(node, errors) {
// Here we create an array of style keys (https://www.figma.com/plugin-docs/api/PaintStyle/#key)
// that we want to make sure our text layers aren't using.
const fillsToCheck = [
"4b93d40f61be15e255e87948a715521c3ae957e6"
// To collect style keys, use a plugin like Inspector, or use console commands like figma.getLocalPaintStyles();
// in your design system file.
];
let nodeFillStyle = node.fillStyleId;
// If there are multiple text styles on a single text layer, we can't lint it
// we can return an error instead.
if (typeof nodeFillStyle === "symbol") {
return errors.push(
createErrorObject(
node, // Node object we use to reference the error (id, layer name, etc)
"fill", // Type of error (fill, text, effect, etc)
"Mixing two styles together", // Message we show to the user
"Multiple Styles" // Normally we return a hex value here
)
);
}
// We strip the additional style key characters so we can check
// to see if the fill is being used incorrectly.
nodeFillStyle = nodeFillStyle.replace("S:", "");
nodeFillStyle = nodeFillStyle.split(",")[0];
// If the node (layer) has a fill style, then check to see if there's an error.
if (nodeFillStyle !== "") {
// If we find the layer has a fillStyle that matches in the array create an error.
if (fillsToCheck.includes(nodeFillStyle)) {
return errors.push(
createErrorObject(
node, // Node object we use to reference the error (id, layer name, etc)
"fill", // Type of error (fill, text, effect, etc)
"Incorrect text color use", // Message we show to the user
"Using a background color on a text layer" // Determines the fill, so we can show a hex value.
)
);
}
// If there is no fillStyle on this layer,
// check to see why with our default linting function for fills.
} else {
checkFills(node, errors);
}
}
// Check for effects like shadows, blurs etc.
export function checkEffects(node, errors) {
if (node.effects.length && node.visible === true) {
if (node.effectStyleId === "") {
const effectsArray = [];
node.effects.forEach(effect => {
let effectsObject = {
type: "",
radius: "",
offsetX: "",
offsetY: "",
fill: "",
value: ""
};
// All effects have a radius.
effectsObject.radius = effect.radius;
if (effect.type === "DROP_SHADOW") {
effectsObject.type = "Drop Shadow";
} else if (effect.type === "INNER_SHADOW") {
effectsObject.type = "Inner Shadow";
} else if (effect.type === "LAYER_BLUR") {
effectsObject.type = "Layer Blur";
} else {
effectsObject.type = "Background Blur";
}
if (effect.color) {
let effectsFill = convertColor(effect.color);
effectsObject.fill = RGBToHex(
effectsFill["r"],
effectsFill["g"],
effectsFill["b"]
);
effectsObject.offsetX = effect.offset.x;
effectsObject.offsetY = effect.offset.y;
effectsObject.value = `${effectsObject.type} ${effectsObject.fill} ${effectsObject.radius}px X: ${effectsObject.offsetX}, Y: ${effectsObject.offsetY}`;
} else {
effectsObject.value = `${effectsObject.type} ${effectsObject.radius}px`;
}
effectsArray.unshift(effectsObject);
});
let currentStyle = effectsArray[0].value;
return errors.push(
createErrorObject(
node,
"effects",
"Missing effects style",
currentStyle
)
);
} else {
return;
}
}
}
export function checkFills(node, errors) {
if (typeof node.boundVariables.fills !== "undefined") {
return;
}
if (
(node.fills.length && node.visible === true) ||
typeof node.fills === "symbol"
) {
let nodeFills = node.fills;
let fillStyleId = node.fillStyleId;
if (typeof nodeFills === "symbol") {
return errors.push(
createErrorObject(node, "fill", "Missing fill style", "Mixed values")
);
}
if (typeof fillStyleId === "symbol") {
return errors.push(
createErrorObject(node, "fill", "Missing fill style", "Mixed values")
);
}
if (
node.fillStyleId === "" &&
node.fills[0].type !== "IMAGE" &&
node.fills[0].type !== "VIDEO" &&
node.fills[0].visible === true
) {
// We may need an array to loop through fill types.
return errors.push(
createErrorObject(
node,
"fill",
"Missing fill style",
determineFill(node.fills)
)
);
} else {
return;
}
}
}
export function checkStrokes(node, errors) {
if (node.strokes.length) {
if (typeof node.boundVariables.strokes !== "undefined") {
return;
}
if (node.strokeStyleId === "" && node.visible === true) {
let strokeObject = {
strokeWeight: "",
strokeAlign: "",
strokeFills: []
};
// With the update to stroke alignment, sometimes
// strokes can be symhols (figma.mixed)
let strokeWeight = node.strokeWeight;
// Check for a mixed stroke weight and return a generic error
if (typeof strokeWeight === "symbol") {
return errors.push(
createErrorObject(
node,
"stroke",
"Missing stroke style",
"Mixed sizes or alignment"
)
);
}
strokeObject.strokeWeight = node.strokeWeight;
strokeObject.strokeAlign = node.strokeAlign;
strokeObject.strokeFills = determineFill(node.strokes);
let currentStyle = `${strokeObject.strokeFills} / ${strokeObject.strokeWeight} / ${strokeObject.strokeAlign}`;
return errors.push(
createErrorObject(node, "stroke", "Missing stroke style", currentStyle)
);
} else {
return;
}
}
}
export function checkType(node, errors) {
if (node.textStyleId === "" && node.visible === true) {
let textObject = {
font: "",
fontStyle: "",
fontSize: "",
lineHeight: {}
};
let fontStyle = node.fontName;
let fontSize = node.fontName;
if (typeof fontSize === "symbol") {
return errors.push(
createErrorObject(
node,
"text",
"Missing text style",
"Mixed sizes or families"
)
);
}
if (typeof fontStyle === "symbol") {
return errors.push(
createErrorObject(
node,
"text",
"Missing text style",
"Mixed sizes or families"
)
);
}
textObject.font = node.fontName.family;
textObject.fontStyle = node.fontName.style;
textObject.fontSize = node.fontSize;
// Line height can be "auto" or a pixel value
if (node.lineHeight.value !== undefined) {
textObject.lineHeight = node.lineHeight.value;
} else {
textObject.lineHeight = "Auto";
}
let currentStyle = `${textObject.font} ${textObject.fontStyle} / ${textObject.fontSize} (${textObject.lineHeight} line-height)`;
return errors.push(
createErrorObject(node, "text", "Missing text style", currentStyle)
);
} else {
return;
}
}
// Utility functions for color conversion.
const convertColor = color => {
const colorObj = color;
const figmaColor = {};
Object.entries(colorObj).forEach(cf => {
const [key, value] = cf;
if (["r", "g", "b"].includes(key)) {
figmaColor[key] = (255 * (value as number)).toFixed(0);
}
if (key === "a") {
figmaColor[key] = value;
}
});
return figmaColor;
};
function RGBToHex(r, g, b) {
r = Number(r).toString(16);
g = Number(g).toString(16);
b = Number(b).toString(16);
if (r.length == 1) r = "0" + r;
if (g.length == 1) g = "0" + g;
if (b.length == 1) b = "0" + b;
return "#" + r + g + b;
}