-
-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add selector-combinator-*list rules (#3088)
* Add selector-combinator-*list rules * Use whitespace regex in test and examples * Normalise whitespace descendant combinator
- Loading branch information
1 parent
b2712de
commit 66f0ad6
Showing
11 changed files
with
507 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
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,48 @@ | ||
# selector-combinator-blacklist | ||
|
||
Specify a blacklist of disallowed combinators. | ||
|
||
```css | ||
a + b {} | ||
/** ↑ | ||
* These combinators */ | ||
``` | ||
|
||
This rule normalizes the whitespace descendant combinator to be a single space. | ||
|
||
This rule ignores [reference combinators](https://www.w3.org/TR/selectors4/#idref-combinators) e.g. `/for/`. | ||
|
||
## Options | ||
|
||
`array|string`: `["array", "of", "combinators"]|"combinator"` | ||
|
||
Given: | ||
|
||
```js | ||
[">", " "] | ||
``` | ||
|
||
The following patterns are considered violations: | ||
|
||
```css | ||
a > b {} | ||
``` | ||
|
||
```css | ||
a b {} | ||
``` | ||
|
||
```css | ||
a | ||
b {} | ||
``` | ||
|
||
The following patterns are *not* considered violations: | ||
|
||
```css | ||
a + b {} | ||
``` | ||
|
||
```css | ||
a ~ b {} | ||
``` |
70 changes: 70 additions & 0 deletions
70
lib/rules/selector-combinator-blacklist/__tests__/index.js
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,70 @@ | ||
"use strict"; | ||
|
||
const messages = require("..").messages; | ||
const ruleName = require("..").ruleName; | ||
const rules = require("../../../rules"); | ||
|
||
const rule = rules[ruleName]; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [">", " "], | ||
skipBasicChecks: true, | ||
|
||
accept: [ | ||
{ | ||
code: "a {}" | ||
}, | ||
{ | ||
code: "a, b {}" | ||
}, | ||
{ | ||
code: "a /for/ b {}" | ||
}, | ||
{ | ||
code: "a + b {}" | ||
}, | ||
{ | ||
code: "a:not(b ~ c) {}" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: "a b {}", | ||
message: messages.rejected(" "), | ||
line: 1, | ||
column: 2 | ||
}, | ||
{ | ||
code: "a\tb {}", | ||
message: messages.rejected(" "), | ||
line: 1, | ||
column: 2 | ||
}, | ||
{ | ||
code: "a\n\tb {}", | ||
message: messages.rejected(" "), | ||
line: 1, | ||
column: 2 | ||
}, | ||
{ | ||
code: "a,\nb c {}", | ||
message: messages.rejected(" "), | ||
line: 2, | ||
column: 2 | ||
}, | ||
{ | ||
code: "a:not(b > c) {}", | ||
message: messages.rejected(">"), | ||
line: 1, | ||
column: 9 | ||
}, | ||
{ | ||
code: "a > b {}", | ||
message: messages.rejected(">"), | ||
line: 1, | ||
column: 3 | ||
} | ||
] | ||
}); |
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,67 @@ | ||
"use strict"; | ||
|
||
const _ = require("lodash"); | ||
const isStandardSyntaxCombinator = require("../../utils/isStandardSyntaxCombinator"); | ||
const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule"); | ||
const parseSelector = require("../../utils/parseSelector"); | ||
const report = require("../../utils/report"); | ||
const ruleMessages = require("../../utils/ruleMessages"); | ||
const validateOptions = require("../../utils/validateOptions"); | ||
|
||
const ruleName = "selector-combinator-blacklist"; | ||
|
||
const messages = ruleMessages(ruleName, { | ||
rejected: combinator => `Unexpected combinator "${combinator}"` | ||
}); | ||
|
||
const rule = function(blacklist) { | ||
return (root, result) => { | ||
const validOptions = validateOptions(result, ruleName, { | ||
actual: blacklist, | ||
possible: [_.isString] | ||
}); | ||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkRules(rule => { | ||
if (!isStandardSyntaxRule(rule)) { | ||
return; | ||
} | ||
|
||
const selector = rule.selector; | ||
|
||
parseSelector(selector, result, rule, fullSelector => { | ||
fullSelector.walkCombinators(combinatorNode => { | ||
if (!isStandardSyntaxCombinator(combinatorNode)) { | ||
return; | ||
} | ||
|
||
const value = normalizeCombinator(combinatorNode.value); | ||
|
||
if (blacklist.indexOf(value) === -1) { | ||
return; | ||
} | ||
|
||
report({ | ||
result, | ||
ruleName, | ||
message: messages.rejected(value), | ||
node: rule, | ||
index: combinatorNode.sourceIndex | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
function normalizeCombinator(value) { | ||
return value.replace(/\s+/g, " "); | ||
} | ||
|
||
rule.primaryOptionArray = true; | ||
|
||
rule.ruleName = ruleName; | ||
rule.messages = messages; | ||
module.exports = rule; |
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,48 @@ | ||
# selector-combinator-whitelist | ||
|
||
Specify a whitelist of allowed combinators. | ||
|
||
```css | ||
a + b {} | ||
/** ↑ | ||
* These combinators */ | ||
``` | ||
|
||
This rule normalizes the whitespace descendant combinator to be a single space. | ||
|
||
This rule ignores [reference combinators](https://www.w3.org/TR/selectors4/#idref-combinators) e.g. `/for/`. | ||
|
||
## Options | ||
|
||
`array|string`: `["array", "of", "combinators"]|"combinator"` | ||
|
||
Given: | ||
|
||
```js | ||
[">", " "] | ||
``` | ||
|
||
The following patterns are considered violations: | ||
|
||
```css | ||
a + b {} | ||
``` | ||
|
||
```css | ||
a ~ b {} | ||
``` | ||
|
||
The following patterns are *not* considered violations: | ||
|
||
```css | ||
a > b {} | ||
``` | ||
|
||
```css | ||
a b {} | ||
``` | ||
|
||
```css | ||
a | ||
b {} | ||
``` |
94 changes: 94 additions & 0 deletions
94
lib/rules/selector-combinator-whitelist/__tests__/index.js
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,94 @@ | ||
"use strict"; | ||
|
||
const messages = require("..").messages; | ||
const ruleName = require("..").ruleName; | ||
const rules = require("../../../rules"); | ||
|
||
const rule = rules[ruleName]; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [">", " "], | ||
skipBasicChecks: true, | ||
|
||
accept: [ | ||
{ | ||
code: "a {}" | ||
}, | ||
{ | ||
code: "a, b {}" | ||
}, | ||
{ | ||
code: "a /for/ b {}" | ||
}, | ||
{ | ||
code: "a > b {}" | ||
}, | ||
{ | ||
code: "a:not(b > c) {}" | ||
}, | ||
{ | ||
code: "a b {}" | ||
}, | ||
{ | ||
code: "a\tb {}" | ||
}, | ||
{ | ||
code: "a\nb {}" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: "a ~ b {}", | ||
message: messages.rejected("~"), | ||
line: 1, | ||
column: 3 | ||
}, | ||
{ | ||
code: "a:not(b ~ c) {}", | ||
message: messages.rejected("~"), | ||
line: 1, | ||
column: 9 | ||
}, | ||
{ | ||
code: "a,\nb + c {}", | ||
message: messages.rejected("+"), | ||
line: 2, | ||
column: 3 | ||
} | ||
] | ||
}); | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: ["~"], | ||
skipBasicChecks: true, | ||
|
||
accept: [ | ||
{ | ||
code: "a ~ b {}" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: "a b {}", | ||
message: messages.rejected(" "), | ||
line: 1, | ||
column: 2 | ||
}, | ||
{ | ||
code: "a\tb {}", | ||
message: messages.rejected(" "), | ||
line: 1, | ||
column: 2 | ||
}, | ||
{ | ||
code: "a\n\tb {}", | ||
message: messages.rejected(" "), | ||
line: 1, | ||
column: 2 | ||
} | ||
] | ||
}); |
Oops, something went wrong.