Skip to content

Commit b618278

Browse files
author
Barzilay, Oshri (ob143h)
committed
fixed styles destructuring support
1 parent a428adb commit b618278

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

lib/rules/no-unused-styles.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,9 @@ module.exports = Components.detect((context, components) => {
3636
}
3737
},
3838

39-
ArrayExpression: function (arrayNode) {
40-
if (arrayNode && arrayNode.elements && arrayNode.elements.length) {
41-
arrayNode.elements
42-
.flatMap((node) => {
43-
const styleRef = astHelpers.getPotentialStyleReferenceFromArrayExpression(node);
44-
return styleRef ? [styleRef] : [];
45-
})
46-
.forEach((styleRef) => {
47-
styleReferences.add(styleRef);
48-
});
49-
}
50-
},
51-
52-
JSXExpressionContainer: function (node) {
53-
const styleRef = astHelpers.getPotentialStyleReferenceFromJSXExpressionContainer(node);
54-
if (styleRef) {
55-
styleReferences.add(styleRef);
56-
}
39+
VariableDeclarator: function (node) {
40+
const styleRefs = astHelpers.getPotentialDestructuredStyleReferences(node);
41+
styleRefs.forEach(styleReferences.add, styleReferences);
5742
},
5843

5944
CallExpression: function (node) {
@@ -68,9 +53,7 @@ module.exports = Components.detect((context, components) => {
6853
'Program:exit': function () {
6954
const list = components.all();
7055
if (Object.keys(list).length > 0) {
71-
styleReferences.forEach((reference) => {
72-
styleSheets.markAsUsed(reference);
73-
});
56+
styleReferences.forEach(styleSheets.markAsUsed, styleSheets);
7457
reportUnusedStyles(styleSheets.getUnusedReferences());
7558
}
7659
},

lib/util/stylesheet.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ StyleSheets.prototype.markAsUsed = function (fullyQualifiedName) {
3030
const styleSheetName = nameSplit[0];
3131
const styleSheetProperty = nameSplit[1];
3232

33-
if (nameSplit.length === 1) {
34-
Object.keys(this.styleSheets).forEach((key) => {
35-
this.styleSheets[key] = this.styleSheets[key].filter(
36-
(property) => property.key.name !== styleSheetName
37-
);
38-
});
39-
} else if (this.styleSheets[styleSheetName]) {
33+
if (this.styleSheets[styleSheetName]) {
4034
this.styleSheets[styleSheetName] = this.styleSheets[styleSheetName].filter(
4135
(property) => property.key.name !== styleSheetProperty
4236
);
@@ -122,6 +116,14 @@ const astHelpers = {
122116
);
123117
},
124118

119+
getDestructuringAssignmentParts: function (node) {
120+
if (node && node.id && node.id.type === 'ObjectPattern' && node.id.properties && node.init) {
121+
return [node.init.name, node.id.properties];
122+
}
123+
124+
return [null, null];
125+
},
126+
125127
getStyleSheetName: function (node) {
126128
if (node && node.parent && node.parent.id) {
127129
return node.parent.id.name;
@@ -470,24 +472,16 @@ const astHelpers = {
470472
}
471473
},
472474

473-
getPotentialStyleReferenceFromArrayExpression: function (node) {
474-
if (node && node.type === 'Identifier' && node.name) {
475-
return node.name;
476-
}
477-
},
475+
getPotentialDestructuredStyleReferences: function (node) {
476+
const [styleSheetName, properties] = this.getDestructuringAssignmentParts(node);
478477

479-
getPotentialStyleReferenceFromJSXExpressionContainer: function (node) {
480-
if (
481-
node
482-
&& node.expression
483-
&& node.expression.type === 'Identifier'
484-
&& node.expression.name
485-
&& node.parent
486-
&& node.parent.name
487-
&& ['style', 'textStyle'].includes(node.parent.name.name)
488-
) {
489-
return node.expression.name;
490-
}
478+
return styleSheetName && properties
479+
? properties.flatMap((property) =>
480+
property.key && property.key.type === 'Identifier' && property.key.name
481+
? `${styleSheetName}.${property.key.name}`
482+
: []
483+
)
484+
: [];
491485
},
492486

493487
isEitherShortHand: function (property1, property2) {

0 commit comments

Comments
 (0)