-
-
Notifications
You must be signed in to change notification settings - Fork 679
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
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
325a777
Fix #1373: Add rule no-invalid-attribute-name
doug-wade 05c60a0
Remove stray newline
doug-wade 34c2976
Apply suggestions from code review
doug-wade 01ed0d7
#1373 Use xml-name-validator
doug-wade 54eeafe
Merge branch 'fix-1373' of https://github.com/doug-wade/eslint-plugin…
doug-wade 5004cb8
Fix linting error
doug-wade 770a2cf
remove stray newline
doug-wade 9766245
refactor test code
doug-wade f4e7ab9
Update lib/rules/no-invalid-attribute-name.js
doug-wade 195984f
fix bad commit from github ui
doug-wade 0027adf
fix typechecking error
doug-wade 8850c38
Respond to PR feedback
doug-wade d31ee8d
Include the added types in package.json
doug-wade 1bd8928
Merge branch 'master' into fix-1373
doug-wade 8e5758a
check v-bind directives
doug-wade 5c71916
Update tests/lib/rules/no-invalid-attribute-name.js
doug-wade bed2351
Fix failing unit test
doug-wade 8925e65
Merge branch 'master' into fix-1373
ota-meshi 70ef9ed
Update lib/rules/no-invalid-attribute-name.js
ota-meshi c91073f
Update lib/rules/no-invalid-attribute-name.js
ota-meshi c610a5f
Update tests/lib/rules/no-invalid-attribute-name.js
ota-meshi 1a0834b
Update tests/lib/rules/no-invalid-attribute-name.js
ota-meshi 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 hidden or 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 hidden or 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,43 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-invalid-attribute-name | ||
description: require valid attribute names | ||
--- | ||
# vue/no-invalid-attribute-name | ||
|
||
> require valid attribute names | ||
|
||
- :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) |
This file contains hidden or 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 hidden or 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,69 @@ | ||
/** | ||
* @author Doug Wade <douglas.b.wade@gmail.com> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const xnv = require('xml-name-validator') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'require valid attribute names', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-invalid-attribute-name.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
attribute: 'Attribute name {{name}} is not valid.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** | ||
* @param {string | VIdentifier} key | ||
* @return {string} | ||
*/ | ||
const getName = (key) => (typeof key === 'string' ? key : key.name) | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
/** @param {VDirective | VAttribute} node */ | ||
VAttribute(node) { | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (utils.isCustomComponent(node.parent.parent)) { | ||
return | ||
} | ||
|
||
const name = getName(node.key.name) | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ( | ||
node.directive && | ||
name === 'bind' && | ||
node.key.argument && | ||
node.key.argument.type === 'VIdentifier' && | ||
!xnv.name(node.key.argument.name) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'attribute', | ||
data: { | ||
name: node.key.argument.name | ||
} | ||
}) | ||
} | ||
|
||
if (!node.directive && !xnv.name(name)) { | ||
context.report({ | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node, | ||
messageId: 'attribute', | ||
data: { | ||
name | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,146 @@ | ||
/** | ||
* @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><p foo /></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p foo="bar" /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p foo-bar /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p _foo-bar /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p :foo-bar /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p foo.bar /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p quux-.9 /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><MyComponent 0abc="foo" /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><MyComponent :0abc="foo" /></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><a :href="url"> ... </a></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div v-bind:class="{ active: isActive }"></div></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p v-if="seen">Now you see me</p></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<a v-on:[eventName]="doSomething"> ... </a>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<form v-on:submit.prevent="onSubmit"> ... </form>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<a @[event]="doSomething"> ... </a>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div v-bind="..."></div></template>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div v-0abc="..."></div></template>` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p 0abc /></template>`, | ||
errors: [ | ||
{ | ||
message: 'Attribute name 0abc is not valid.', | ||
line: 1, | ||
column: 14 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p -def></template>`, | ||
errors: [ | ||
{ | ||
message: 'Attribute name -def is not valid.', | ||
line: 1, | ||
column: 14 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p !ghi /></template>`, | ||
errors: [ | ||
{ | ||
message: 'Attribute name !ghi is not valid.', | ||
line: 1, | ||
column: 14 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p v-bind:0abc=""></template>`, | ||
errors: [ | ||
{ | ||
message: 'Attribute name 0abc is not valid.', | ||
line: 1, | ||
column: 14 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><p :0abc="..." /></template>`, | ||
errors: [ | ||
{ | ||
message: 'Attribute name 0abc is not valid.', | ||
line: 1, | ||
column: 14 | ||
} | ||
] | ||
} | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.