-
Notifications
You must be signed in to change notification settings - Fork 28
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
feat: New rule to disallow certain attribute values #145
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a973f3e
feat: attr value rule to disallow certain attribute value combinations
Adam-Gillespie345 dd2f391
chore: spelling error
Adam-Gillespie345 221e411
Update packages/eslint-plugin/lib/rules/no-restricted-attr-values.js
Adam-Gillespie345 8e39aa7
chore: addressing feedback and upgrading lint
Adam-Gillespie345 36cbe11
chore: reverting extra changes to package files
Adam-Gillespie345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
id: no-restricted-attr-values | ||
title: "no-restricted-attr-values" | ||
--- | ||
|
||
# no-restricted-attrs | ||
|
||
Disallow specified attribute values | ||
|
||
## How to use | ||
|
||
.eslintrc.js | ||
|
||
```js | ||
module.exports = { | ||
rules: { | ||
'@html-eslint/no-restricted-attr-values': ["error", { | ||
attrPatterns: ["class"], | ||
attrValuePatterns: ["data-.*"] | ||
message: "\'data-x\' is restricted." | ||
}] | ||
} | ||
}; | ||
``` | ||
|
||
## Rule Details | ||
|
||
This rule allows you to specify attribute values that you don't want to use in your application. | ||
|
||
### Options | ||
|
||
This rule takes an array of option objects, where the `attrPatterns` and `attrValuePatterns` are specified. | ||
|
||
- `attrPatterns`: an array of strings representing regular expression pattern, disallows attribute names that match any of the patterns. | ||
- `attrValuePatterns`: an array of strings representing regular expression pattern, disallows attribute values that match any of the patterns. | ||
- `message` (optional): a string for custom message. | ||
|
||
```js | ||
module.exports = { | ||
"rules": { | ||
"@html-eslint/no-restricted-attrs": [ | ||
"error", | ||
{ | ||
attrPatterns: ["class", "alt"], | ||
attrValuePatterns: ["data-.*"] | ||
message: "\'data-x\' is restricted." | ||
}, | ||
{ | ||
attrPatterns: [".*"], | ||
attrValuePatterns: ["^foo$"] | ||
message: "\'foo\' is restricted." | ||
} | ||
], | ||
} | ||
}; | ||
``` | ||
|
||
Examples of **incorrect** code for this rule with the option below: | ||
|
||
```json | ||
{ | ||
"attrPatterns": ["data-.*"], | ||
"attrValuePatterns": ["foo"], | ||
"message": "Do not use foo attr value" | ||
} | ||
``` | ||
|
||
```html | ||
<div data-name="foo"></div> | ||
<img data-name="foo"></div> | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
139 changes: 139 additions & 0 deletions
139
packages/eslint-plugin/lib/rules/no-restricted-attr-values.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,139 @@ | ||
/** | ||
* @typedef {import("../types").Rule} Rule | ||
* @typedef {{attrPatterns: string[], attrValuePatterns: string[], message?: string}[]} Options | ||
*/ | ||
|
||
const { RULE_CATEGORY } = require("../constants"); | ||
|
||
const MESSAGE_IDS = { | ||
RESTRICTED: "restricted", | ||
}; | ||
|
||
/** | ||
* @type {Rule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: "code", | ||
|
||
docs: { | ||
description: "Disallow specified attributes", | ||
category: RULE_CATEGORY.BEST_PRACTICE, | ||
recommended: false, | ||
}, | ||
|
||
fixable: null, | ||
schema: { | ||
type: "array", | ||
|
||
items: { | ||
type: "object", | ||
required: ["attrPatterns", "attrValuePatterns"], | ||
properties: { | ||
attrPatterns: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
}, | ||
attrValuePatterns: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
}, | ||
message: { | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
messages: { | ||
[MESSAGE_IDS.RESTRICTED]: | ||
"'{{attrValuePatterns}}' is restricted from being used.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
/** | ||
* @type {Options} | ||
*/ | ||
const options = context.options; | ||
const checkers = options.map((option) => new PatternChecker(option)); | ||
|
||
return { | ||
[["Tag", "StyleTag", "ScriptTag"].join(",")](node) { | ||
node.attributes.forEach((attr) => { | ||
if ( | ||
!attr.key || | ||
typeof attr.value?.value !== "string" || | ||
!attr.key?.value | ||
) { | ||
return; | ||
} | ||
const matched = checkers.find((checker) => | ||
checker.test(attr.key.value, attr.value.value) | ||
); | ||
|
||
if (!matched) { | ||
return; | ||
} | ||
|
||
const result = { | ||
node: attr, | ||
message: "", | ||
}; | ||
|
||
const customMessage = matched.getMessage(); | ||
|
||
if (customMessage) { | ||
result.message = customMessage; | ||
} else { | ||
result.messageId = MESSAGE_IDS.RESTRICTED; | ||
} | ||
|
||
context.report({ | ||
...result, | ||
data: { attrValuePatterns: attr.value.value }, | ||
}); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
class PatternChecker { | ||
/** | ||
* @param {Options[number]} option | ||
*/ | ||
constructor(option) { | ||
this.option = option; | ||
this.attrRegExps = option.attrPatterns.map( | ||
(pattern) => new RegExp(pattern, "u") | ||
); | ||
|
||
this.valueRegExps = option.attrValuePatterns.map( | ||
(pattern) => new RegExp(pattern, "u") | ||
); | ||
this.message = option.message; | ||
} | ||
|
||
/** | ||
* @param {string} attrName | ||
* @param {string} attrValue | ||
* @returns {boolean} | ||
*/ | ||
test(attrName, attrValue) { | ||
const result = | ||
this.attrRegExps.some((exp) => exp.test(attrName)) && | ||
this.valueRegExps.some((exp) => exp.test(attrValue)); | ||
return result; | ||
} | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
getMessage() { | ||
return this.message || ""; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
packages/eslint-plugin/tests/rules/no-restricted-attr-values.test.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,114 @@ | ||
const createRuleTester = require("../rule-tester"); | ||
const rule = require("../../lib/rules/no-restricted-attr-values"); | ||
|
||
const ruleTester = createRuleTester(); | ||
|
||
ruleTester.run("no-restricted-attr-values", rule, { | ||
valid: [ | ||
{ | ||
code: `<div> </div>`, | ||
options: [ | ||
{ | ||
attrPatterns: [".*"], | ||
attrValuePatterns: ["data-.*"], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<div class="foo"> </div>`, | ||
options: [ | ||
{ | ||
attrPatterns: ["class"], | ||
attrValuePatterns: ["data-.*"], | ||
}, | ||
], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<div foo="data-x"> </div>`, | ||
options: [ | ||
{ | ||
attrPatterns: [".*"], | ||
attrValuePatterns: ["data-.*"], | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
messageId: "restricted", | ||
data: { | ||
attrValuePatterns: "data-x", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<div foo=""> </div>`, | ||
options: [ | ||
{ | ||
attrPatterns: [".*"], | ||
attrValuePatterns: [""], | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
messageId: "restricted", | ||
data: { | ||
attrValuePatterns: "", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<div class="foo"> </div> <a alt="foo"></a>`, | ||
options: [ | ||
{ | ||
attrPatterns: ["alt"], | ||
attrValuePatterns: ["foo"], | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
messageId: "restricted", | ||
data: { | ||
attrValuePatterns: "foo", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<div alt="foo"> </div> <custom-element class="foo"></custom-element>`, | ||
options: [ | ||
{ | ||
attrPatterns: ["alt", "class"], | ||
attrValuePatterns: ["^foo$"], | ||
message: "no foo for alt or class", | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
message: "no foo for alt or class", | ||
}, | ||
{ | ||
message: "no foo for alt or class", | ||
}, | ||
], | ||
}, | ||
// custom message | ||
{ | ||
code: `<div foo="data-x"> </div>`, | ||
options: [ | ||
{ | ||
attrPatterns: [".*"], | ||
attrValuePatterns: ["data.*"], | ||
message: "please do not use 'data-x'", | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
message: "please do not use 'data-x'", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
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.
Had to update esLint to handle the optional chaining