Skip to content

Fix false positives when using <svg> in vue/valid-v-slot and vue/valid-v-model rule #1367

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 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 0 additions & 23 deletions lib/rules/require-toggle-inside-transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@

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

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------

/**
* Check whether the given node is an well-known element or not.
* @param {VElement} node The element node to check.
* @returns {boolean} `true` if the name is an well-known element name.
*/
function isWellKnownElement(node) {
if (
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
utils.isHtmlWellKnownElementName(node.rawName) ||
utils.isSvgWellKnownElementName(node.rawName)
) {
return true
}
return false
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -61,9 +41,6 @@ module.exports = {
if (utils.isCustomComponent(element)) {
return
}
if (!isWellKnownElement(element)) {
return
}
if (
!utils.hasDirective(element, 'if') &&
!utils.hasDirective(element, 'show')
Expand Down
24 changes: 2 additions & 22 deletions lib/rules/valid-v-bind-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const utils = require('../utils')
* @returns {boolean} `true` if the node is valid.
*/
function isValidElement(node) {
if (
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
utils.isHtmlWellKnownElementName(node.rawName) ||
utils.isSvgWellKnownElementName(node.rawName)
) {
if (!utils.isCustomComponent(node)) {
// non Vue-component
return false
}
Expand Down Expand Up @@ -82,22 +78,6 @@ function maybeNullObjectMemberExpression(node) {
return false
}

function isValidIs(node) {
const { attributes } = node
const isAttribute = attributes.some((attr) => {
// check for `VAttribute`
if (attr.type === 'VAttribute') {
// check for `is` attribute
if (attr.key.type === 'VIdentifier' && attr.key.name === 'is') return true

// check for `:is` `bind` attribute
if (attr.key.type === 'VDirectiveKey' && attr.key.argument.name === 'is')
return true
}
})
return isAttribute
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -136,7 +116,7 @@ module.exports = {
const element = node.parent.parent
const name = element.name

if (!isValidElement(element) && !isValidIs(node.parent)) {
if (!isValidElement(element)) {
context.report({
node,
loc: node.loc,
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ module.exports = {
return (
(this.isHtmlElementNode(node) &&
!this.isHtmlWellKnownElementName(node.rawName)) ||
(this.isSvgElementNode(node) &&
!this.isSvgWellKnownElementName(node.rawName)) ||
this.hasAttribute(node, 'is') ||
this.hasDirective(node, 'bind', 'is') ||
this.hasDirective(node, 'is')
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/valid-v-bind-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,18 @@ tester.run('valid-v-bind-sync', rule, {
filename: 'test.vue',
code: '<template><MyComponent :foo.sync="(a?.b).c.d" /></template>',
errors: ["'.sync' modifier has potential null object property access."]
},
{
filename: 'test.vue',
code:
'<template><tr v-on:is="myRow" :some-prop.sync="somePropValue"></template>',
errors: ["'.sync' modifiers aren't supported on <tr> non Vue-components."]
},
{
filename: 'test.vue',
code:
'<template><tr v-bind="myRow" :some-prop.sync="somePropValue"></template>',
errors: ["'.sync' modifiers aren't supported on <tr> non Vue-components."]
}
]
})
9 changes: 9 additions & 0 deletions tests/lib/rules/valid-v-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ tester.run('valid-v-model', rule, {
code:
'<template><MyComponent v-model.modifier.modifierTwo="a"></MyComponent></template>'
},
// svg
{
code: `
<template>
<svg>
<MyComponent v-model="slotProps"></MyComponent>
</svg>
</template>`
},
// parsing error
{
filename: 'parsing-error.vue',
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/valid-v-slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ tester.run('valid-v-slot', rule, {
`,
options: [{ allowModifiers: true }]
},
// svg
{
code: `
<template>
<svg>
<MyComponent v-slot="slotProps">
<MyChildComponent :thing="slotProps.thing" />
</MyComponent>
</svg>
</template>`
},
// parsing error
{
filename: 'parsing-error.vue',
Expand Down