-
-
Notifications
You must be signed in to change notification settings - Fork 680
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
}) | ||
|
||
|
@@ -77,6 +98,19 @@ module.exports = { | |
recommended: false | ||
}, | ||
fixable: false, | ||
schema: [] | ||
|
||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allowCoexistClass: { | ||
type: 'boolean' | ||
}, | ||
allowCoexistStyle: { | ||
type: 'boolean' | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, that's correct