Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from rambleraptor/at-if-no-null
New rule: `at-if-no-null`
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# at-if-no-null | ||
|
||
Check for equality to null is unnecessarily explicit since `null` is falsey in Sass. | ||
|
||
```scss | ||
a { | ||
@if $x == null {} | ||
/** ↑ ↑ | ||
* == or != null is unncessary */ | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
true | ||
|
||
### `true` | ||
|
||
The following patterns are considered warnings: | ||
```scss | ||
a { | ||
@if $x == null {} | ||
} | ||
``` | ||
|
||
```scss | ||
a { | ||
@if $x != null {} | ||
} | ||
``` | ||
|
||
The following patterns are *not* considered warnings: | ||
|
||
```scss | ||
a { | ||
@if $x {} | ||
} | ||
``` | ||
|
||
```scss | ||
a { | ||
@if not $x {} | ||
} | ||
``` |
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,47 @@ | ||
import rule, { ruleName, messages } from ".."; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
syntax: "scss", | ||
|
||
accept: [ | ||
{ | ||
code: `a { | ||
@if $x {} | ||
}`, | ||
description: "does not use the != null format" | ||
}, | ||
{ | ||
code: `a { | ||
@if not $x {} | ||
}`, | ||
description: "does not use the == null format" | ||
}, | ||
{ | ||
code: `a { | ||
@if $x != null and $x > 1 {} | ||
}`, | ||
description: "does not use the == null format" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: `a { | ||
@if $x == null {} | ||
}`, | ||
description: "uses the == null format", | ||
message: messages.equals_null, | ||
line: 2 | ||
}, | ||
{ | ||
code: `a { | ||
@if $x != null {} | ||
}`, | ||
description: "uses the != null format", | ||
message: messages.not_equals_null, | ||
line: 2 | ||
} | ||
] | ||
}); |
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,49 @@ | ||
import { namespace } from "../../utils"; | ||
import { utils } from "stylelint"; | ||
|
||
export const ruleName = namespace("at-if-no-null"); | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
equals_null: "Expected @if not statement rather than @if statement == null", | ||
not_equals_null: "Expected @if statement rather than @if statement != null" | ||
}); | ||
|
||
export default function(expectation) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { | ||
actual: expectation | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkAtRules(atrule => { | ||
// Do nothing if it's not an @if | ||
if (atrule.name !== "if") { | ||
return; | ||
} | ||
|
||
// If rule != null and (expr), skip | ||
if (atrule.params.match(/\(?[ \t]*.* != null and .*\)?/)) { | ||
return; | ||
} | ||
|
||
if (atrule.params.match(/\(?[ \t]*.* == null[ \t]*\)?/)) { | ||
utils.report({ | ||
message: messages.equals_null, | ||
node: atrule, | ||
result, | ||
ruleName | ||
}); | ||
} else if (atrule.params.match(/\(?[ \t]*.* != null[ \t]*\)?/)) { | ||
utils.report({ | ||
message: messages.not_equals_null, | ||
node: atrule, | ||
result, | ||
ruleName | ||
}); | ||
} | ||
}); | ||
}; | ||
} |
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