Skip to content

Commit

Permalink
no-unused-private-members treats hyphens and underscores as equals
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Apr 4, 2024
1 parent 26c105c commit 73ab3da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
36 changes: 22 additions & 14 deletions src/rules/no-unused-private-members/__tests__/index.js
Expand Up @@ -80,7 +80,7 @@ testRule({
code: `
@import 'foo';
%-toolbelt:hover {
%_toolbelt:hover {
color: red;
}
Expand All @@ -96,7 +96,7 @@ testRule({
},
{
code: `
$-a: 3px;
$_a: 3px;
$_b: 1px;
.action-buttons {
Expand Down Expand Up @@ -124,7 +124,7 @@ testRule({
{
code: `
$_app-bar-height: 65px;
$_header-height: 70px;
$-header_height: 70px;
$_explorer-margin: 64px;
$_total-offset: $_app-bar-height + $_header-height + $_explorer-margin;
Expand All @@ -137,9 +137,9 @@ testRule({
{
code: `
@function _add-one($n1) { @return $n1 + 1 }
@function _add-two($n1) { @return $n1 + 2 }
@function _add-three($n1) {
@return _add-two (
@function -add-two($n1) { @return $n1 + 2 }
@function _add_three($n1) {
@return _add_two (
_add-one($n1)
)}
Expand All @@ -162,13 +162,21 @@ testRule({
{
code: `
$_gm-toolbar-item-state-offset: 0.5 * 3;
@mixin _position-offset($offset: $_gm-toolbar-item-state-offset) {
@mixin _position_offset($offset: $_gm-toolbar-item-state-offset) {
top: $offset;
}
.b {
@include _position-offset;
}`,
description: "Is used as a mixin default parameter"
},
{
code: `
$_a-b: 1px;
.b {
margin: $-a-b;
}`,
description: "Is used as a mixin default parameter"
}
],

Expand All @@ -187,7 +195,7 @@ testRule({
margin-left: _addNums(4, 6);
}
`,
message: messages.expected("_one"),
message: messages.expected("-one"),
description: "Private function",
line: 6,
column: 7
Expand All @@ -203,7 +211,7 @@ testRule({
margin: 0;
}
`,
message: messages.expected("_reset-list"),
message: messages.expected("-reset-list"),
description: "Private mixin",
line: 2,
column: 7
Expand Down Expand Up @@ -271,7 +279,7 @@ testRule({
@include column($_a);
@include footer();
}`,
message: messages.expected("$_c"),
message: messages.expected("$-c"),
line: 4,
column: 7,
description: "Variables used as function parameters"
Expand All @@ -285,7 +293,7 @@ testRule({
margin: 5px map.get($_a);
}
`,
message: messages.expected("$_b"),
message: messages.expected("$-b"),
line: 3,
column: 7,
description: "map.get uses variable"
Expand All @@ -304,7 +312,7 @@ testRule({
)
);
}`,
message: messages.expected("$_a-color"),
message: messages.expected("$-a-color"),
line: 3,
column: 7,
description: "Is in theme declaration"
Expand All @@ -319,7 +327,7 @@ testRule({
margin: 0px;
}
}`,
message: messages.expected("$_b"),
message: messages.expected("$-b"),
line: 3,
column: 7,
description: "Is used in @if"
Expand All @@ -334,7 +342,7 @@ testRule({
margin: 0px;
}
}`,
message: messages.expected("$_b"),
message: messages.expected("$-b"),
line: 3,
column: 7,
description: "Variable in interpolated selector."
Expand Down
14 changes: 10 additions & 4 deletions src/rules/no-unused-private-members/index.js
Expand Up @@ -25,6 +25,10 @@ function getPrivateMembers(inputString) {
return matches;
}

function matchUnderscores(inputString) {
return inputString.replaceAll("_", "-");
}

function rule(primaryOption) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, {
Expand Down Expand Up @@ -60,7 +64,7 @@ function rule(primaryOption) {
);
selectors.forEach(selector => {
if (!privateMembers.selectors.has(selector)) {
privateMembers.selectors.set(selector, node);
privateMembers.selectors.set(matchUnderscores(selector), node);
}
});
}
Expand All @@ -70,7 +74,7 @@ function rule(primaryOption) {
node.type === "decl" &&
(node.prop.startsWith("$-") || node.prop.startsWith("$_"));
if (isPrivateVariable) {
privateMembers.variables.set(node.prop, node);
privateMembers.variables.set(matchUnderscores(node.prop), node);
}

// Private functions
Expand All @@ -81,7 +85,7 @@ function rule(primaryOption) {
if (isPrivateFunction) {
const match = extractFunctionName(node.params);
if (match.length < 2) return;
privateMembers.functions.set(match[1], node);
privateMembers.functions.set(matchUnderscores(match[1]), node);
}

// Private mixins
Expand All @@ -92,7 +96,7 @@ function rule(primaryOption) {
if (isPrivateMixin) {
const match = extractFunctionName(node.params);
privateMembers.mixins.set(
match.length < 2 ? node.params : match[1],
matchUnderscores(match.length < 2 ? node.params : match[1]),
node
);
}
Expand All @@ -104,6 +108,7 @@ function rule(primaryOption) {
const valuePrivateMembers = getPrivateMembers(value);
if (valuePrivateMembers) {
valuePrivateMembers.forEach(privateMember => {
privateMember = matchUnderscores(privateMember);
if (privateMembers.mixins.get(privateMember) !== node)
privateMembers.mixins.delete(privateMember);
if (
Expand All @@ -124,6 +129,7 @@ function rule(primaryOption) {
const valuePrivateMembers = getPrivateMembers(decls.value);
if (valuePrivateMembers) {
valuePrivateMembers.forEach(privateMember => {
privateMember = matchUnderscores(privateMember);
if (privateMembers.variables.get(privateMember) !== decls)
privateMembers.variables.delete(privateMember);
if (privateMembers.functions.get(privateMember) !== decls)
Expand Down

0 comments on commit 73ab3da

Please sign in to comment.