Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/no-restricted-static-attribute
rule (#1192)
* Add `vue/no-restricted-static-attribute` rule * fix
- Loading branch information
Showing
5 changed files
with
417 additions
and
1 deletion.
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,97 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-restricted-static-attribute | ||
description: disallow specific attribute | ||
--- | ||
# vue/no-restricted-static-attribute | ||
> disallow specific attribute | ||
## :book: Rule Details | ||
|
||
This rule allows you to specify attribute names that you don't want to use in your application. | ||
|
||
## :wrench: Options | ||
|
||
This rule takes a list of strings, where each string is a attribute name or pattern to be restricted: | ||
|
||
```json | ||
{ | ||
"vue/no-restricted-static-attribute": ["error", "foo", "bar"] | ||
} | ||
``` | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-static-attribute': ['error', 'foo', 'bar']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✘ BAD --> | ||
<div foo="x" /> | ||
<div bar /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Alternatively, the rule also accepts objects. | ||
|
||
```json | ||
{ | ||
"vue/no-restricted-static-attribute": ["error", | ||
{ | ||
"key": "stlye", | ||
"message": "Using \"stlye\" is not allowed. Use \"style\" instead." | ||
} | ||
] | ||
} | ||
``` | ||
|
||
The following properties can be specified for the object. | ||
|
||
- `key` ... Specify the attribute key name or pattern. | ||
- `value` ... Specify the value text or pattern or `true`. If specified, it will only be reported if the specified value is used. If `true`, it will only be reported if there is no value or if the value and key are same. | ||
- `element` ... Specify the element name or pattern. If specified, it will only be reported if used on the specified element. | ||
- `message` ... Specify an optional custom message. | ||
|
||
### `{ "key": "foo", "value": "bar" }` | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-static-attribute': ['error', { key: 'foo', value: 'bar' }]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<div foo="foo" /> | ||
<!-- ✘ BAD --> | ||
<div foo="bar" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `{ "key": "foo", "element": "MyButton" }` | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-static-attribute': ['error', { key: 'foo', element: 'MyButton' }]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<CoolButton foo="x" /> | ||
<!-- ✘ BAD --> | ||
<MyButton foo="x" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :couple: Related rules | ||
|
||
- [vue/no-restricted-v-bind] | ||
|
||
[vue/no-restricted-v-bind]: ./no-restricted-v-bind.md | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-static-attribute.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-static-attribute.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
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,166 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const regexp = require('../utils/regexp') | ||
|
||
/** | ||
* @typedef {import('vue-eslint-parser').AST.VAttribute} VAttribute | ||
*/ | ||
/** | ||
* @typedef {object} ParsedOption | ||
* @property { (key: VAttribute) => boolean } test | ||
* @property {boolean} [useValue] | ||
* @property {boolean} [useElement] | ||
* @property {string} [message] | ||
*/ | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {(str: string) => boolean} | ||
*/ | ||
function buildMatcher(str) { | ||
if (regexp.isRegExp(str)) { | ||
const re = regexp.toRegExp(str) | ||
return (s) => { | ||
re.lastIndex = 0 | ||
return re.test(s) | ||
} | ||
} | ||
return (s) => s === str | ||
} | ||
/** | ||
* @param {any} option | ||
* @returns {ParsedOption} | ||
*/ | ||
function parseOption(option) { | ||
if (typeof option === 'string') { | ||
const matcher = buildMatcher(option) | ||
return { | ||
test({ key }) { | ||
return matcher(key.rawName) | ||
} | ||
} | ||
} | ||
const parsed = parseOption(option.key) | ||
if (option.value) { | ||
const keyTest = parsed.test | ||
if (option.value === true) { | ||
parsed.test = (node) => { | ||
if (!keyTest(node)) { | ||
return false | ||
} | ||
return node.value == null || node.value.value === node.key.rawName | ||
} | ||
} else { | ||
const valueMatcher = buildMatcher(option.value) | ||
parsed.test = (node) => { | ||
if (!keyTest(node)) { | ||
return false | ||
} | ||
return node.value != null && valueMatcher(node.value.value) | ||
} | ||
} | ||
parsed.useValue = true | ||
} | ||
if (option.element) { | ||
const argTest = parsed.test | ||
const tagMatcher = buildMatcher(option.element) | ||
parsed.test = (node) => { | ||
if (!argTest(node)) { | ||
return false | ||
} | ||
const element = node.parent.parent | ||
return tagMatcher(element.rawName) | ||
} | ||
parsed.useElement = true | ||
} | ||
parsed.message = option.message | ||
return parsed | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow specific attribute', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-restricted-static-attribute.html' | ||
}, | ||
fixable: null, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ | ||
type: 'object', | ||
properties: { | ||
key: { type: 'string' }, | ||
value: { anyOf: [{ type: 'string' }, { enum: [true] }] }, | ||
element: { type: 'string' }, | ||
message: { type: 'string', minLength: 1 } | ||
}, | ||
required: ['key'], | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
uniqueItems: true, | ||
minItems: 0 | ||
}, | ||
|
||
messages: { | ||
// eslint-disable-next-line eslint-plugin/report-message-format | ||
restrictedAttr: '{{message}}' | ||
} | ||
}, | ||
create(context) { | ||
if (!context.options.length) { | ||
return {} | ||
} | ||
/** @type {ParsedOption[]} */ | ||
const options = context.options.map(parseOption) | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
/** | ||
* @param {VAttribute} node | ||
*/ | ||
'VAttribute[directive=false]'(node) { | ||
for (const option of options) { | ||
if (option.test(node)) { | ||
const message = option.message || defaultMessage(node, option) | ||
context.report({ | ||
node, | ||
messageId: 'restrictedAttr', | ||
data: { message } | ||
}) | ||
return | ||
} | ||
} | ||
} | ||
}) | ||
|
||
/** | ||
* @param {VAttribute} node | ||
* @param {ParsedOption} option | ||
*/ | ||
function defaultMessage(node, option) { | ||
const key = node.key.rawName | ||
const value = !option.useValue | ||
? '' | ||
: node.value == null | ||
? '` set to `true' | ||
: `="${node.value.value}"` | ||
|
||
let on = '' | ||
if (option.useElement) { | ||
on = ` on \`<${node.parent.parent.rawName}>\`` | ||
} | ||
return `Using \`${key + value}\`${on} is not allowed.` | ||
} | ||
} | ||
} |
Oops, something went wrong.