Skip to content

Commit e337088

Browse files
asamuzaKdomenic
authored andcommitted
Add property name exports to property modules
Each property module now exports its property name as `module.exports.property`. This change enables more consistent and DRY usage of property names throughout the codebase, especially for shorthand and initial value maps, reducing hardcoded strings and potential typos.
1 parent a3413ab commit e337088

77 files changed

Lines changed: 467 additions & 296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/properties/background.js

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,27 @@ const backgroundAttachment = require("./backgroundAttachment");
1111
const backgroundColor = require("./backgroundColor");
1212

1313
const property = "background";
14-
const initialValues = new Map([
15-
["background-image", "none"],
16-
["background-position", "0% 0%"],
17-
["background-size", "auto"],
18-
["background-repeat", "repeat"],
19-
["background-origin", "padding-box"],
20-
["background-clip", "border-box"],
21-
["background-attachment", "scroll"],
22-
["background-color", "transparent"]
14+
15+
module.exports.initialValues = new Map([
16+
[backgroundImage.property, "none"],
17+
[backgroundPosition.property, "0% 0%"],
18+
[backgroundSize.property, "auto"],
19+
[backgroundRepeat.property, "repeat"],
20+
[backgroundOrigin.property, "padding-box"],
21+
[backgroundClip.property, "border-box"],
22+
[backgroundAttachment.property, "scroll"],
23+
[backgroundColor.property, "transparent"]
2324
]);
2425

2526
module.exports.shorthandFor = new Map([
26-
["background-image", backgroundImage],
27-
["background-position", backgroundPosition],
28-
["background-size", backgroundSize],
29-
["background-repeat", backgroundRepeat],
30-
["background-origin", backgroundOrigin],
31-
["background-clip", backgroundClip],
32-
["background-attachment", backgroundAttachment],
33-
["background-color", backgroundColor]
27+
[backgroundImage.property, backgroundImage],
28+
[backgroundPosition.property, backgroundPosition],
29+
[backgroundSize.property, backgroundSize],
30+
[backgroundRepeat.property, backgroundRepeat],
31+
[backgroundOrigin.property, backgroundOrigin],
32+
[backgroundClip.property, backgroundClip],
33+
[backgroundAttachment.property, backgroundAttachment],
34+
[backgroundColor.property, backgroundColor]
3435
]);
3536

3637
module.exports.parse = function parse(v, opt = {}) {
@@ -50,24 +51,30 @@ module.exports.parse = function parse(v, opt = {}) {
5051
const l = values.length;
5152
for (let i = 0; i < l; i++) {
5253
let bg = {
53-
"background-image": initialValues.get("background-image"),
54-
"background-position": initialValues.get("background-position"),
55-
"background-size": initialValues.get("background-size"),
56-
"background-repeat": initialValues.get("background-repeat"),
57-
"background-origin": initialValues.get("background-origin"),
58-
"background-clip": initialValues.get("background-clip"),
59-
"background-attachment": initialValues.get("background-attachment"),
60-
"background-color": initialValues.get("background-color")
54+
[backgroundImage.property]: module.exports.initialValues.get(backgroundImage.property),
55+
[backgroundPosition.property]: module.exports.initialValues.get(backgroundPosition.property),
56+
[backgroundSize.property]: module.exports.initialValues.get(backgroundSize.property),
57+
[backgroundRepeat.property]: module.exports.initialValues.get(backgroundRepeat.property),
58+
[backgroundOrigin.property]: module.exports.initialValues.get(backgroundOrigin.property),
59+
[backgroundClip.property]: module.exports.initialValues.get(backgroundClip.property),
60+
[backgroundAttachment.property]: module.exports.initialValues.get(
61+
backgroundAttachment.property
62+
),
63+
[backgroundColor.property]: module.exports.initialValues.get(backgroundColor.property)
6164
};
6265
if (l > 1 && i !== l - 1) {
6366
bg = {
64-
"background-image": initialValues.get("background-image"),
65-
"background-position": initialValues.get("background-position"),
66-
"background-size": initialValues.get("background-size"),
67-
"background-repeat": initialValues.get("background-repeat"),
68-
"background-origin": initialValues.get("background-origin"),
69-
"background-clip": initialValues.get("background-clip"),
70-
"background-attachment": initialValues.get("background-attachment")
67+
[backgroundImage.property]: module.exports.initialValues.get(backgroundImage.property),
68+
[backgroundPosition.property]: module.exports.initialValues.get(
69+
backgroundPosition.property
70+
),
71+
[backgroundSize.property]: module.exports.initialValues.get(backgroundSize.property),
72+
[backgroundRepeat.property]: module.exports.initialValues.get(backgroundRepeat.property),
73+
[backgroundOrigin.property]: module.exports.initialValues.get(backgroundOrigin.property),
74+
[backgroundClip.property]: module.exports.initialValues.get(backgroundClip.property),
75+
[backgroundAttachment.property]: module.exports.initialValues.get(
76+
backgroundAttachment.property
77+
)
7178
};
7279
}
7380
const bgPosition = [];
@@ -88,15 +95,15 @@ module.exports.parse = function parse(v, opt = {}) {
8895
if (parsers.isValidPropertyValue(longhand, part)) {
8996
partValid = true;
9097
switch (longhand) {
91-
case "background-clip":
92-
case "background-origin": {
98+
case backgroundClip.property:
99+
case backgroundOrigin.property: {
93100
const parsedValue = value.parse(part, { globalObject });
94101
if (parsedValue) {
95102
bgBox.push(parsedValue);
96103
}
97104
break;
98105
}
99-
case "background-color": {
106+
case backgroundColor.property: {
100107
if (i !== values.length - 1) {
101108
return;
102109
}
@@ -106,21 +113,21 @@ module.exports.parse = function parse(v, opt = {}) {
106113
}
107114
break;
108115
}
109-
case "background-position": {
116+
case backgroundPosition.property: {
110117
const parsedValue = value.parse(part, { globalObject });
111118
if (parsedValue) {
112119
bgPosition.push(parsedValue);
113120
}
114121
break;
115122
}
116-
case "background-repeat": {
123+
case backgroundRepeat.property: {
117124
const parsedValue = value.parse(part, { globalObject });
118125
if (parsedValue) {
119126
bgRepeat.push(parsedValue);
120127
}
121128
break;
122129
}
123-
case "background-size": {
130+
case backgroundSize.property: {
124131
break;
125132
}
126133
default: {
@@ -144,15 +151,15 @@ module.exports.parse = function parse(v, opt = {}) {
144151
if (parsers.isValidPropertyValue(longhand, part)) {
145152
partValid = true;
146153
switch (longhand) {
147-
case "background-clip":
148-
case "background-origin": {
154+
case backgroundClip.property:
155+
case backgroundOrigin.property: {
149156
const parsedValue = value.parse(part, { globalObject });
150157
if (parsedValue) {
151158
bgBox.push(parsedValue);
152159
}
153160
break;
154161
}
155-
case "background-color": {
162+
case backgroundColor.property: {
156163
if (i !== l - 1) {
157164
return;
158165
}
@@ -162,17 +169,17 @@ module.exports.parse = function parse(v, opt = {}) {
162169
}
163170
break;
164171
}
165-
case "background-position": {
172+
case backgroundPosition.property: {
166173
break;
167174
}
168-
case "background-repeat": {
175+
case backgroundRepeat.property: {
169176
const parsedValue = value.parse(part, { globalObject });
170177
if (parsedValue) {
171178
bgRepeat.push(parsedValue);
172179
}
173180
break;
174181
}
175-
case "background-size": {
182+
case backgroundSize.property: {
176183
const parsedValue = value.parse(part, { globalObject });
177184
if (parsedValue) {
178185
bgSize.push(parsedValue);
@@ -194,38 +201,38 @@ module.exports.parse = function parse(v, opt = {}) {
194201
}
195202
}
196203
if (bgPosition.length) {
197-
const { parse: parser } = module.exports.shorthandFor.get("background-position");
204+
const { parse: parser } = module.exports.shorthandFor.get(backgroundPosition.property);
198205
const value = parser(bgPosition.join(" "), { globalObject });
199206
if (value) {
200-
bg["background-position"] = value;
207+
bg[backgroundPosition.property] = value;
201208
}
202209
}
203210
if (bgSize.length) {
204-
const { parse: parser } = module.exports.shorthandFor.get("background-size");
211+
const { parse: parser } = module.exports.shorthandFor.get(backgroundSize.property);
205212
const value = parser(bgSize.join(" "), { globalObject });
206213
if (value) {
207-
bg["background-size"] = value;
214+
bg[backgroundSize.property] = value;
208215
}
209216
}
210217
if (bgRepeat.length) {
211-
const { parse: parser } = module.exports.shorthandFor.get("background-repeat");
218+
const { parse: parser } = module.exports.shorthandFor.get(backgroundRepeat.property);
212219
const value = parser(bgRepeat.join(" "), { globalObject });
213220
if (value) {
214-
bg["background-repeat"] = value;
221+
bg[backgroundRepeat.property] = value;
215222
}
216223
}
217224
if (bgBox.length) {
218225
switch (bgBox.length) {
219226
case 1: {
220227
const [value] = bgBox;
221-
bg["background-origin"] = value;
222-
bg["background-clip"] = value;
228+
bg[backgroundOrigin.property] = value;
229+
bg[backgroundClip.property] = value;
223230
break;
224231
}
225232
case 2: {
226233
const [value1, value2] = bgBox;
227-
bg["background-origin"] = value1;
228-
bg["background-clip"] = value2;
234+
bg[backgroundOrigin.property] = value1;
235+
bg[backgroundClip.property] = value2;
229236
break;
230237
}
231238
default: {
@@ -254,14 +261,14 @@ module.exports.definition = {
254261
return;
255262
}
256263
const bgMap = new Map([
257-
["background-image", []],
258-
["background-position", []],
259-
["background-size", []],
260-
["background-repeat", []],
261-
["background-origin", []],
262-
["background-clip", []],
263-
["background-attachment", []],
264-
["background-color", []]
264+
[backgroundImage.property, []],
265+
[backgroundPosition.property, []],
266+
[backgroundSize.property, []],
267+
[backgroundRepeat.property, []],
268+
[backgroundOrigin.property, []],
269+
[backgroundClip.property, []],
270+
[backgroundAttachment.property, []],
271+
[backgroundColor.property, []]
265272
]);
266273
const backgrounds = [];
267274
for (const bgValue of bgValues) {
@@ -271,17 +278,17 @@ module.exports.definition = {
271278
const arr = bgMap.get(longhand);
272279
arr.push(value);
273280
bgMap.set(longhand, arr);
274-
if (value !== initialValues.get(longhand)) {
275-
if (longhand === "background-size") {
281+
if (value !== module.exports.initialValues.get(longhand)) {
282+
if (longhand === backgroundSize.property) {
276283
bg.push(`/ ${value}`);
277284
} else {
278285
bg.push(value);
279286
}
280-
} else if (longhand === "background-image") {
287+
} else if (longhand === backgroundImage.property) {
281288
if (v === "none") {
282289
bg.push(value);
283290
}
284-
} else if (longhand === "background-color") {
291+
} else if (longhand === backgroundColor.property) {
285292
if (v === "transparent") {
286293
bg.push(value);
287294
}
@@ -306,26 +313,26 @@ module.exports.definition = {
306313
let l = 0;
307314
for (const [longhand] of module.exports.shorthandFor) {
308315
const val = this.getPropertyValue(longhand);
309-
if (longhand === "background-image") {
316+
if (longhand === backgroundImage.property) {
310317
if (
311318
val === "none" &&
312319
v === "none" &&
313-
this.getPropertyValue("background-color") === "transparent"
320+
this.getPropertyValue(backgroundColor.property) === "transparent"
314321
) {
315322
return val;
316323
}
317-
if (val !== initialValues.get(longhand)) {
324+
if (val !== module.exports.initialValues.get(longhand)) {
318325
const imgValues = parsers.splitValue(val, {
319326
delimiter: ","
320327
});
321328
l = imgValues.length;
322329
bgMap.set(longhand, imgValues);
323330
}
324-
} else if (longhand === "background-color") {
325-
if (val !== initialValues.get(longhand) || v.includes(val)) {
331+
} else if (longhand === backgroundColor.property) {
332+
if (val !== module.exports.initialValues.get(longhand) || v.includes(val)) {
326333
bgMap.set(longhand, [val]);
327334
}
328-
} else if (val !== initialValues.get(longhand)) {
335+
} else if (val !== module.exports.initialValues.get(longhand)) {
329336
bgMap.set(
330337
longhand,
331338
parsers.splitValue(val, {
@@ -335,7 +342,8 @@ module.exports.definition = {
335342
}
336343
}
337344
if (l === 0) {
338-
const [background] = bgMap.get("background-color");
345+
const bgColArr = bgMap.get(backgroundColor.property);
346+
const background = bgColArr ? bgColArr[0] : null;
339347
if (background) {
340348
return background;
341349
}
@@ -348,25 +356,25 @@ module.exports.definition = {
348356
for (const [longhand, values] of bgMap) {
349357
for (let i = 0; i < l; i++) {
350358
switch (longhand) {
351-
case "background-color": {
359+
case backgroundColor.property: {
352360
if (i === l - 1) {
353361
const value = values[0];
354362
if (parsers.hasVarFunc(value)) {
355363
return "";
356364
}
357-
if (value && value !== initialValues.get(longhand)) {
365+
if (value && value !== module.exports.initialValues.get(longhand)) {
358366
const bgValue = bgValues[i];
359367
bgValue.push(value);
360368
}
361369
}
362370
break;
363371
}
364-
case "background-size": {
372+
case backgroundSize.property: {
365373
const value = values[i];
366374
if (parsers.hasVarFunc(value)) {
367375
return "";
368376
}
369-
if (value && value !== initialValues.get(longhand)) {
377+
if (value && value !== module.exports.initialValues.get(longhand)) {
370378
const bgValue = bgValues[i];
371379
bgValue.push(`/ ${value}`);
372380
}
@@ -377,7 +385,7 @@ module.exports.definition = {
377385
if (parsers.hasVarFunc(value)) {
378386
return "";
379387
}
380-
if (value && value !== initialValues.get(longhand)) {
388+
if (value && value !== module.exports.initialValues.get(longhand)) {
381389
const bgValue = bgValues[i];
382390
bgValue.push(value);
383391
}
@@ -394,3 +402,5 @@ module.exports.definition = {
394402
enumerable: true,
395403
configurable: true
396404
};
405+
406+
module.exports.property = property;

lib/properties/backgroundAttachment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ module.exports.definition = {
6262
enumerable: true,
6363
configurable: true
6464
};
65+
66+
module.exports.property = property;

lib/properties/backgroundClip.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ module.exports.definition = {
6262
enumerable: true,
6363
configurable: true
6464
};
65+
66+
module.exports.property = property;

lib/properties/backgroundColor.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ module.exports.definition = {
5353
enumerable: true,
5454
configurable: true
5555
};
56+
57+
module.exports.property = property;

lib/properties/backgroundImage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ module.exports.definition = {
7676
enumerable: true,
7777
configurable: true
7878
};
79+
80+
module.exports.property = property;

lib/properties/backgroundOrigin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ module.exports.definition = {
6262
enumerable: true,
6363
configurable: true
6464
};
65+
66+
module.exports.property = property;

lib/properties/backgroundPosition.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,5 @@ module.exports.definition = {
191191
enumerable: true,
192192
configurable: true
193193
};
194+
195+
module.exports.property = property;

lib/properties/backgroundRepeat.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ module.exports.definition = {
8787
enumerable: true,
8888
configurable: true
8989
};
90+
91+
module.exports.property = property;

0 commit comments

Comments
 (0)