Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
270 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# selector-not-notation | ||
|
||
## Options | ||
|
||
`string`: `"simple"|"complex"` | ||
|
||
### `"simple"` | ||
|
||
The following patterns are considered problems: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(input, select) { color: pink; } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(p.foo) { color: pink; } | ||
``` | ||
|
||
The following patterns are _not_ considered problems: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(input):not(select) { color: pink; } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(div) { color: pink; } | ||
``` | ||
|
||
### `"complex"` | ||
|
||
The following pattern is considered a problem: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(input):not(select) { color: pink; } | ||
``` | ||
|
||
The following patterns are _not_ considered problems: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(input, select) { color: pink; } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(p.foo) { color: pink; } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
:not(div).bar:not(:empty) { color: pink; } | ||
``` |
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,95 @@ | ||
'use strict'; | ||
|
||
const { messages, ruleName } = require('..'); | ||
|
||
testRule({ | ||
ruleName, | ||
config: ['simple'], | ||
fix: false, | ||
|
||
accept: [ | ||
{ | ||
code: ':not() {}', | ||
}, | ||
{ | ||
code: ':not( div ) {}', | ||
}, | ||
{ | ||
code: ':nOt(div) {}', | ||
}, | ||
{ | ||
code: ':not(*) {}', | ||
}, | ||
{ | ||
code: ':not(:link) {}', | ||
}, | ||
{ | ||
code: ':not(.foo) {}', | ||
}, | ||
{ | ||
code: ':not([title]) {}', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ':not(:not()) {}', | ||
fixed: ':not()', | ||
message: messages.expected('simple'), | ||
line: 1, | ||
column: 1, | ||
}, | ||
{ | ||
code: ':not(:before) {}', | ||
fixed: ':not()', | ||
message: messages.expected('simple'), | ||
line: 1, | ||
column: 1, | ||
}, | ||
{ | ||
code: ':not(input, select) {}', | ||
fixed: ':not(input):not(select) {}', | ||
message: messages.expected('simple'), | ||
line: 1, | ||
column: 1, | ||
}, | ||
{ | ||
code: ':not(p.foo) {}', | ||
fixed: '' /* how to fix this one? */, | ||
message: messages.expected('simple'), | ||
line: 1, | ||
column: 1, | ||
}, | ||
], | ||
}); | ||
|
||
testRule({ | ||
ruleName, | ||
config: ['complex'], | ||
fix: false, | ||
|
||
accept: [ | ||
{ | ||
code: ':not():after {}', | ||
}, | ||
{ | ||
code: ':not(input, select) {}', | ||
}, | ||
{ | ||
code: ':not(p.foo) {}', | ||
}, | ||
{ | ||
code: ':not(div).bar:not(:empty) {}', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ':not(.bar):not(span) {}', | ||
fixed: ':not(.bar, span) {} {}', | ||
message: messages.expected('complex'), | ||
line: 1, | ||
column: 11, | ||
}, | ||
], | ||
}); |
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,117 @@ | ||
'use strict'; | ||
|
||
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 { | ||
isPseudoClass, | ||
isAttribute, | ||
isClassName, | ||
isUniversal, | ||
isIdentifier, | ||
isTag, | ||
} = require('postcss-selector-parser'); | ||
|
||
const ruleName = 'selector-not-notation'; | ||
const messages = ruleMessages(ruleName, { | ||
expected: (type) => `Expected ${type} :not() pseudo-class notation`, | ||
}); | ||
const meta = { url: 'https://stylelint.io/user-guide/rules/list/selector-not-notation' }; | ||
|
||
/** @typedef {import('postcss-selector-parser').Node} Node */ | ||
|
||
/** | ||
* @param {Node} node | ||
* @returns {boolean} | ||
*/ | ||
const isSimpleSelector = (node) => | ||
isPseudoClass(node) || | ||
isAttribute(node) || | ||
isClassName(node) || | ||
isUniversal(node) || | ||
isIdentifier(node) || | ||
isTag(node); | ||
|
||
/** | ||
* @param {Node} node | ||
* @returns {boolean} | ||
*/ | ||
const isNot = (node) => isPseudoClass(node) && node.value.toLowerCase() === ':not'; | ||
|
||
/** | ||
* @param {import('postcss-selector-parser').Pseudo} node | ||
* @returns {boolean} | ||
*/ | ||
const isSimple = ({ nodes: list }) => { | ||
if (list.length > 1) return false; | ||
|
||
const [first, second] = list[0].nodes; // list is never empty | ||
|
||
if (!first) return true; | ||
|
||
if (second) return false; | ||
|
||
return isSimpleSelector(first) && !isNot(first); | ||
}; | ||
|
||
/** @type {import('stylelint').Rule} */ | ||
const rule = (primary, _, context) => { | ||
return (root, result) => { | ||
const validOptions = validateOptions(result, ruleName, { | ||
actual: primary, | ||
possible: ['simple', 'complex'], | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkRules((ruleNode) => { | ||
if (!isStandardSyntaxRule(ruleNode)) { | ||
return; | ||
} | ||
|
||
const selector = ruleNode.selector; | ||
|
||
/*const fixedSelector = */ parseSelector(selector, result, ruleNode, (container) => { | ||
container.walkPseudos((pseudo) => { | ||
if (!isNot(pseudo)) return; | ||
|
||
if (primary === 'complex') { | ||
const prev = pseudo.prev(); | ||
const hasConsecutiveNot = prev && isNot(prev); | ||
|
||
if (!hasConsecutiveNot) return; | ||
|
||
if (context.fix) { | ||
/* TODO */ | ||
return; | ||
} | ||
} else { | ||
if (isSimple(pseudo)) return; | ||
|
||
if (context.fix) { | ||
/* TODO */ | ||
return; | ||
} | ||
} | ||
|
||
report({ | ||
message: messages.expected(primary), | ||
node: ruleNode, | ||
index: pseudo.sourceIndex, | ||
result, | ||
ruleName, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
rule.ruleName = ruleName; | ||
rule.messages = messages; | ||
rule.meta = meta; | ||
module.exports = rule; |