New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add selector-attribute-name-disallowed-list #4992
Merged
jeddy3
merged 6 commits into
stylelint:master
from
rletsin:selector-attribute-name-disallowed-list
Nov 17, 2020
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
60b3f50
Add 'selector-attribute-name-disallowed-list' rule
rletsin c986fef
Add 'selector-attribute-name-disallowed-list' rule
rletsin 7a88958
Merge branch 'selector-attribute-name-disallowed-list' of github.com:…
rletsin aaa97d9
Add new rule to docs, fix linting error
rletsin 85200c8
Merge branch 'master' into selector-attribute-name-disallowed-list
rletsin 1dbd9bb
Add regex as input option to selector-attribute-name-disallowed-list
rletsin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -274,6 +274,9 @@ const rules = { | |
'selector-attribute-operator-whitelist': importLazy(() => | ||
require('./selector-attribute-operator-whitelist'), | ||
)(), | ||
'selector-attribute-name-disallowed-list': importLazy(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's order this alphabetically, i.e. put it before the |
||
require('./selector-attribute-name-disallowed-list'), | ||
)(), | ||
'selector-attribute-quotes': importLazy(() => require('./selector-attribute-quotes'))(), | ||
'selector-class-pattern': importLazy(() => require('./selector-class-pattern'))(), | ||
'selector-combinator-allowed-list': importLazy(() => | ||
|
49 changes: 49 additions & 0 deletions
49
lib/rules/selector-attribute-name-disallowed-list/README.md
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 @@ | ||
# selector-attribute-name-disallowed-list | ||
|
||
Specify a list of disallowed attribute names. | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
[class~="foo"] {} | ||
/** ↑ | ||
* This name */ | ||
``` | ||
|
||
## Options | ||
|
||
`array|string`: `["array", "of", "names"]|"name"` | ||
|
||
Given: | ||
|
||
``` | ||
["class", "id"] | ||
``` | ||
|
||
The following patterns are considered violations: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
[class*="foo"] {} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
[id~="bar"] {} | ||
``` | ||
|
||
The following patterns are _not_ considered violations: | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
[lang~="en-us"] {} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
[target="_blank"] {} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
[href$=".bar"] {} | ||
``` |
51 changes: 51 additions & 0 deletions
51
lib/rules/selector-attribute-name-disallowed-list/__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,51 @@ | ||
'use strict'; | ||
|
||
const { messages, ruleName } = require('..'); | ||
|
||
testRule({ | ||
ruleName, | ||
|
||
config: ['class', 'id'], | ||
|
||
accept: [ | ||
{ | ||
code: 'a[title] { }', | ||
}, | ||
{ | ||
code: 'a[target="_blank"] { }', | ||
}, | ||
{ | ||
code: 'a[href$=".pdf"] { }', | ||
}, | ||
{ | ||
code: 'div[lang~="en-us"] { }', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: '[class*="foo"] { }', | ||
message: messages.rejected('class'), | ||
line: 1, | ||
column: 2, | ||
}, | ||
{ | ||
code: '[ class*="foo" ] { }', | ||
message: messages.rejected('class'), | ||
line: 1, | ||
column: 3, | ||
}, | ||
{ | ||
code: '[class *= "foo"] { }', | ||
message: messages.rejected('class'), | ||
line: 1, | ||
column: 2, | ||
}, | ||
{ | ||
code: '[id~="bar"] { }', | ||
message: messages.rejected('id'), | ||
line: 1, | ||
column: 2, | ||
}, | ||
], | ||
}); |
65 changes: 65 additions & 0 deletions
65
lib/rules/selector-attribute-name-disallowed-list/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,65 @@ | ||
// @ts-nocheck | ||
|
||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
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-attribute-name-disallowed-list'; | ||
|
||
const messages = ruleMessages(ruleName, { | ||
rejected: (name) => `Unexpected name "${name}"`, | ||
}); | ||
|
||
function rule(listInput) { | ||
const list = [].concat(listInput); | ||
|
||
return (root, result) => { | ||
const validOptions = validateOptions(result, ruleName, { | ||
actual: list, | ||
possible: [_.isString], | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkRules((ruleNode) => { | ||
if (!isStandardSyntaxRule(ruleNode)) { | ||
return; | ||
} | ||
|
||
if (!ruleNode.selector.includes('[') || !ruleNode.selector.includes('=')) { | ||
return; | ||
} | ||
|
||
parseSelector(ruleNode.selector, result, ruleNode, (selectorTree) => { | ||
selectorTree.walkAttributes((attributeNode) => { | ||
const attributeName = attributeNode.qualifiedAttribute; | ||
|
||
if (!attributeName || (attributeName && !list.includes(attributeName))) { | ||
return; | ||
} | ||
|
||
report({ | ||
message: messages.rejected(attributeName), | ||
node: ruleNode, | ||
index: attributeNode.sourceIndex + attributeNode.offsetOf('attribute'), | ||
result, | ||
ruleName, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
rule.primaryOptionArray = true; | ||
|
||
rule.ruleName = ruleName; | ||
rule.messages = messages; | ||
module.exports = rule; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's order this alphabetically, i.e. put it before the
operator
rules.