Skip to content

Add vue/no-invalid-attribute-name rule #1851

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

Merged
merged 22 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ For example:
| [vue/no-duplicate-attr-inheritance](./no-duplicate-attr-inheritance.md) | enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"` | |
| [vue/no-empty-component-block](./no-empty-component-block.md) | disallow the `<template>` `<script>` `<style>` block to be empty | |
| [vue/no-expose-after-await](./no-expose-after-await.md) | disallow asynchronously registered `expose` | |
| [vue/no-invalid-attribute-name](./no-invalid-attribute-name.md) | require the attributes to match the imported component name | |
| [vue/no-invalid-model-keys](./no-invalid-model-keys.md) | require valid keys in model option | |
| [vue/no-multiple-objects-in-class](./no-multiple-objects-in-class.md) | disallow to pass multiple objects into array to class | |
| [vue/no-potential-component-option-typo](./no-potential-component-option-typo.md) | disallow a potential typo in your component property | :bulb: |
Expand Down
43 changes: 43 additions & 0 deletions docs/rules/no-invalid-attribute-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-invalid-attribute-name
description: require the attributes to match the imported component name
---
# vue/no-invalid-attribute-name

> require the attributes to match the imported component name

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

This rule detects invalid html attributes.

<eslint-code-block :rules="{'vue/no-invalid-attribute-name': ['error']}">

```vue
<template>
<!-- ✓ GOOD -->
<p foo.bar></p>
<p foo-bar></p>
<p _foo.bar></p>
<p :foo-bar></p>

<!-- ✗ BAD -->
<p 0abc></p>
<p -def></p>
<p !ghi></p>
</template>
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-invalid-attribute-name.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-invalid-attribute-name.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module.exports = {
'no-export-in-script-setup': require('./rules/no-export-in-script-setup'),
'no-expose-after-await': require('./rules/no-expose-after-await'),
'no-extra-parens': require('./rules/no-extra-parens'),
'no-invalid-attribute-name': require('./rules/no-invalid-attribute-name'),
'no-invalid-model-keys': require('./rules/no-invalid-model-keys'),
'no-irregular-whitespace': require('./rules/no-irregular-whitespace'),
'no-lifecycle-after-await': require('./rules/no-lifecycle-after-await'),
Expand Down
54 changes: 54 additions & 0 deletions lib/rules/no-invalid-attribute-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @author Doug Wade <[email protected]>
* See LICENSE file in root directory for full license.
*/
'use strict'

const utils = require('../utils')

module.exports = {
meta: {
type: 'problem',
docs: {
description:
'require the attributes to match the imported component name',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-invalid-attribute-name.html'
},
fixable: null,
schema: [],
messages: {
unexpected: '{{name}} is not a valid attribute name'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @param {string | VIdentifier} key
* @return {string}
*/
const getName = (key) => {
if (typeof key === 'string') {
return key
}

return key.name
}

return utils.defineTemplateBodyVisitor(context, {
VAttribute(node) {
const name = getName(node.key.name)

if (!/^[_:a-zA-Z][_:.\-a-zA-Z0-9]+/.test(name)) {
context.report({
node,
messageId: 'unexpected',
data: {
name
}
})
}
}
})
}
}
158 changes: 158 additions & 0 deletions tests/lib/rules/no-invalid-attribute-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* @author *****your name*****
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-invalid-attribute-name')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('no-invalid-attribute-name', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<div>
<p foo>
{{ content }}
</p>
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p foo="bar">
{{ content }}
</p>
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p foo-bar>
{{ content }}
</p>
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p _foo-bar>
{{ content }}
</p>
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p :foo-bar>
{{ content }}
</p>
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p foo.bar>
{{ content }}
</p>
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p quux-.9>
{{ content }}
</p>
</div>
</template>
`
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<div>
<p 0abc>
{{ content }}
</p>
</div>
</template>
`,
errors: [
{
message: '0abc is not a valid attribute name'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p -def>
{{ content }}
</p>
</div>
</template>
`,
errors: [
{
message: '-def is not a valid attribute name'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<p !ghi>
{{ content }}
</p>
</div>
</template>
`,
errors: [
{
message: '!ghi is not a valid attribute name'
}
]
}
]
})