Skip to content

Add new options for no-duplicate-attributes #112 #113

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 3 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
13 changes: 12 additions & 1 deletion docs/rules/no-duplicate-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ This rule reports duplicate attributes.

## :wrench: Options

Nothing.
`allowCoexistClass` - Enables [`v-bind:class`] directive can coexist with the plain `class` attribute.
`allowCoexistStyle` - Enables [`v-bind:style`] directive can coexist with the plain `style` attribute.

```
'vue/name-property-casing': [2, {
allowCoexistClass: Boolean // default: true
allowCoexistStyle: Boolean, // default: true
}]
```

## TODO: `<div foo foo></div>`

`parse5` remove duplicate attributes on the tokenization phase.
Needs investigation to check.

[`v-bind:class`]: https://vuejs.org/v2/guide/class-and-style.html
[`v-bind:style`]: https://vuejs.org/v2/guide/class-and-style.html
44 changes: 39 additions & 5 deletions lib/rules/no-duplicate-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,48 @@ function getName (attribute) {
* @returns {Object} AST event handlers.
*/
function create (context) {
const names = new Set()
const options = context.options[0] || {}
const allowCoexistStyle = Boolean(options.allowCoexistStyle !== false)
const allowCoexistClass = Boolean(options.allowCoexistClass !== false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this cast is redundant since the result of !== expression is always boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, that's correct


const directiveNames = new Set()
const attributeNames = new Set()

function isDuplicate (name) {
if (allowCoexistStyle && name === 'style') {
return directiveNames.has(name)
}
if (allowCoexistClass && name === 'class') {
return directiveNames.has(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see the test of the following case:

<div :class="a" class="b"></div>

I guess that the class="b" is warned even if allowCoexistClass is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mysticatea you are completely right on this, 👍

}
return directiveNames.has(name) || attributeNames.has(name)
}

utils.registerTemplateBodyVisitor(context, {
'VStartTag' () {
names.clear()
directiveNames.clear()
attributeNames.clear()
},
'VAttribute' (node) {
const name = getName(node)
if (name == null) {
return
}

if (names.has(name)) {
if (isDuplicate(name)) {
context.report({
node,
loc: node.loc,
message: "Duplicate attribute '{{name}}'.",
data: { name }
})
}
names.add(name)

if (node.directive) {
directiveNames.add(name)
} else {
attributeNames.add(name)
}
}
})

Expand All @@ -77,6 +98,19 @@ module.exports = {
recommended: false
},
fixable: false,
schema: []

schema: [
{
type: 'object',
properties: {
allowCoexistClass: {
type: 'boolean'
},
allowCoexistStyle: {
type: 'boolean'
}
}
}
]
}
}
30 changes: 25 additions & 5 deletions tests/lib/rules/no-duplicate-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ tester.run('no-duplicate-attributes', rule, {
{
filename: 'test.vue',
code: '<template><div><div @click="foo" @click="bar"></div></div></template>'
},
{
filename: 'test.vue',
code: '<template><div><div style :style></div></div></template>'
},
{
filename: 'test.vue',
code: '<template><div><div class :class></div></div></template>'
}
],
invalid: [
// {
// filename: "test.vue",
// code: "<template><div><div foo foo></div></div></template>",
// errors: ["Duplicate attribute 'foo'."],
// },
// {
// filename: 'test.vue',
// code: '<template><div><div foo foo></div></div></template>',
// errors: ["Duplicate attribute 'foo'."]
// },
{
filename: 'test.vue',
code: '<template><div><div foo v-bind:foo></div></div></template>',
Expand All @@ -51,6 +59,18 @@ tester.run('no-duplicate-attributes', rule, {
filename: 'test.vue',
code: '<template><div><div foo :foo></div></div></template>',
errors: ["Duplicate attribute 'foo'."]
},
{
filename: 'test.vue',
code: '<template><div><div style :style></div></div></template>',
errors: ["Duplicate attribute 'style'."],
options: [{ allowCoexistStyle: false }]
},
{
filename: 'test.vue',
code: '<template><div><div class :class></div></div></template>',
errors: ["Duplicate attribute 'class'."],
options: [{ allowCoexistClass: false }]
}
]
})