-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add no-unused-private-members rule (#949)
- Loading branch information
1 parent
9bdfb71
commit c67f54d
Showing
8 changed files
with
1,099 additions
and
781 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# no-unused-private-members | ||
|
||
Disallow unused private members. | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@function _addNums($n1, $n2) { | ||
/** ↑ | ||
* This function */ | ||
@return calc($n1 + $n2); | ||
} | ||
|
||
@mixin _no-margin { | ||
/** ↑ | ||
* This mixin */ | ||
margin: 0; | ||
} | ||
|
||
%-toolbelt:hover { | ||
/* ↑ | ||
* This placeholder selector */ | ||
color: red; | ||
} | ||
|
||
.a { | ||
$-b: 1; | ||
/** ↑ | ||
* This variable */ | ||
} | ||
``` | ||
|
||
[Private members](https://sass-lang.com/documentation/at-rules/use/#private-members), such as functions, mixins, variables, and selector placeholders are defined with names that start with either `-` or `_` (in the case of variables the name follows `$`, and the same applies to the `%` prefix for selector placeholders). These private members remain confined within the module, therefore, if they go unused within the module, they are considered unused. | ||
|
||
This rule ignores files using `@import` since all files loaded through it are consolidated within the same module. This setup makes it tricky to ensure all usages of private members. | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered problems: | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@function _addNums($n1, $n2) { | ||
@return calc($n1 + $n2); | ||
} | ||
|
||
@function _plusOne($n1) { | ||
@return $n1 + 1; | ||
} | ||
|
||
.sidebar { | ||
margin-left: _addNums(4, 6); // _plusOne() is not used. | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@mixin _no-margin { | ||
margin: 0; | ||
} | ||
|
||
@mixin blue-color { | ||
color: blue; | ||
} | ||
|
||
nav ul { | ||
@include blue-color; // _no-margin is not used. | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$radius: 3px; | ||
$-margin: 3px; | ||
|
||
.rounded { | ||
border-radius: $radius; // $-margin is not used. | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
%-toolbelt:hover { | ||
color: red; | ||
} | ||
|
||
.a.%b { | ||
margin: 1px; | ||
} | ||
|
||
.action-buttons { | ||
@extend %b; // %-toolbelt is not used. | ||
color: blue; | ||
} | ||
``` | ||
|
||
The following patterns are _not_ considered problems: | ||
|
||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@function _addNums($n1, $n2) { | ||
@return calc($n1 + $n2); | ||
} | ||
|
||
@function _plusOne($n1) { | ||
@return $n1 + 1; | ||
} | ||
|
||
.sidebar { | ||
margin-left: _addNums(4, 6) + _plusOne(1); | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@mixin _no-margin { | ||
margin: 0; | ||
} | ||
|
||
@mixin blue-color { | ||
color: blue; | ||
} | ||
|
||
nav ul { | ||
@include blue-color; | ||
@include _no-margin; | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$radius: 3px; | ||
$-margin: 3px; | ||
|
||
.rounded { | ||
border-radius: $radius; | ||
margin: $-margin; | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
%-toolbelt:hover { | ||
color: red; | ||
} | ||
|
||
.a.%b { | ||
margin: 1px; | ||
} | ||
|
||
.action-buttons { | ||
@extend %b; | ||
@extend %-toolbelt; | ||
color: blue; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
"use strict"; | ||
|
||
const { messages, ruleName } = require(".."); | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true], | ||
customSyntax: "postcss-scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
@function _addNums($n1, $n2) { | ||
@return calc($n1 + $n2); | ||
} | ||
@function one($n1) { | ||
@return $n1; | ||
} | ||
.sidebar { | ||
margin-left: _addNums(4, 6); | ||
} | ||
`, | ||
description: "Private function" | ||
}, | ||
{ | ||
code: ` | ||
@mixin _reset-list { | ||
margin: 0; | ||
} | ||
nav ul { | ||
@include _reset-list; | ||
} | ||
`, | ||
description: "Private mixin" | ||
}, | ||
{ | ||
code: ` | ||
$-radius: 3px; | ||
.rounded { | ||
border-radius: $-radius; | ||
} | ||
`, | ||
description: "Private variable" | ||
}, | ||
{ | ||
code: ` | ||
%-toolbelt:hover { | ||
color: red; | ||
} | ||
.a.%-b { | ||
margin: 1px; | ||
} | ||
.action-buttons { | ||
@extend %-toolbelt; | ||
@extend %-b; | ||
color: blue; | ||
} | ||
`, | ||
description: "Private placeholder selector" | ||
}, | ||
{ | ||
code: ` | ||
@import "fff" | ||
$-radius: 3px; | ||
rounded { | ||
border-radius: 0px; | ||
} | ||
`, | ||
message: messages.expected("$-radius"), | ||
description: "Skip files using @import", | ||
line: 2, | ||
column: 7 | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
@function _addNums($n1, $n2) { | ||
@return calc($n1 + $n2); | ||
} | ||
@function _one($n1) { | ||
@return $n1; | ||
} | ||
.sidebar { | ||
margin-left: _addNums(4, 6); | ||
} | ||
`, | ||
message: messages.expected("_one"), | ||
description: "Private function", | ||
line: 6, | ||
column: 7 | ||
}, | ||
{ | ||
code: ` | ||
@mixin _reset-list { | ||
margin: 0; | ||
padding: 0; | ||
list-style: none; | ||
} | ||
nav ul { | ||
margin: 0; | ||
} | ||
`, | ||
message: messages.expected("_reset-list"), | ||
description: "Private mixin", | ||
line: 2, | ||
column: 7 | ||
}, | ||
{ | ||
code: ` | ||
$-radius: 3px; | ||
rounded { | ||
border-radius: 0px; | ||
} | ||
`, | ||
message: messages.expected("$-radius"), | ||
description: "Private variable", | ||
line: 2, | ||
column: 7 | ||
}, | ||
{ | ||
code: ` | ||
%-toolbelt { | ||
box-sizing: border-box; | ||
} | ||
%-toolbelt:hover { | ||
color: red; | ||
} | ||
.b.%-toolbelt{ | ||
color: blue; | ||
} | ||
`, | ||
message: messages.expected("%-toolbelt"), | ||
description: "Private placeholder selector", | ||
line: 2, | ||
column: 7 | ||
}, | ||
{ | ||
code: ` | ||
%-toolbelt:hover { | ||
color: red; | ||
} | ||
.a.%-b { | ||
margin: 1px; | ||
} | ||
.action-buttons { | ||
@extend %-toolbelt; | ||
color: blue; | ||
} | ||
`, | ||
message: messages.expected("%-b"), | ||
line: 6, | ||
column: 7, | ||
description: "Private placeholder selector with class" | ||
} | ||
] | ||
}); |
Oops, something went wrong.