-
-
Notifications
You must be signed in to change notification settings - Fork 669
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-reserved-props
rule (#1678)
* Add `vue/no-reserved-props` rule * add 'class' and 'style'
- Loading branch information
Showing
10 changed files
with
576 additions
and
7 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,52 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-reserved-props | ||
description: disallow reserved names in props | ||
--- | ||
# vue/no-reserved-props | ||
|
||
> disallow reserved names in props | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `"plugin:vue/essential"`, `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-recommended"` and `"plugin:vue/recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule disallow reserved names to be used in props. | ||
|
||
<eslint-code-block :rules="{'vue/no-reserved-props': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
props: { | ||
/* ✗ BAD */ | ||
ref: String, | ||
key: String, | ||
/* ✓ GOOD */ | ||
foo: String, | ||
bar: String, | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/no-reserved-props": ["error", { | ||
"vueVersion": 3, // or 2 | ||
}] | ||
} | ||
``` | ||
|
||
- `vueVersion` (`2 | 3`) ... Specify the version of Vue you are using. Default is `3`. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-reserved-props.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-reserved-props.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
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 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
/** | ||
* @typedef {import('../utils').ComponentArrayProp} ComponentArrayProp | ||
* @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp | ||
* @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RESERVED = { | ||
3: ['key', 'ref'], | ||
2: ['key', 'ref', 'is', 'slot', 'slot-scope', 'slotScope', 'class', 'style'] | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow reserved names in props', | ||
categories: ['vue3-essential', 'essential'], | ||
url: 'https://eslint.vuejs.org/rules/no-reserved-props.html', | ||
defaultOptions: { | ||
vue2: [{ vueVersion: 2 }] | ||
} | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
vueVersion: { | ||
enum: [2, 3] | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
reserved: | ||
"'{{propName}}' is a reserved attribute and cannot be used as props." | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const options = context.options[0] || {} | ||
/** @type {2|3} */ | ||
const vueVersion = options.vueVersion || 3 | ||
|
||
const reserved = new Set(RESERVED[vueVersion]) | ||
|
||
/** | ||
* @param {(ComponentArrayProp | ComponentObjectProp | ComponentTypeProp)[]} props | ||
*/ | ||
function processProps(props) { | ||
for (const prop of props) { | ||
if (prop.propName != null && reserved.has(prop.propName)) { | ||
context.report({ | ||
node: prop.node, | ||
messageId: `reserved`, | ||
data: { | ||
propName: casing.kebabCase(prop.propName) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return utils.compositingVisitors( | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(_node, props) { | ||
processProps(props) | ||
} | ||
}), | ||
utils.executeOnVue(context, (obj) => { | ||
processProps(utils.getComponentProps(obj)) | ||
}) | ||
) | ||
} | ||
} |
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
Oops, something went wrong.