Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh committed Nov 19, 2024
1 parent f3a6ad1 commit ba16740
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
21 changes: 4 additions & 17 deletions lib/rules/no-v-text-v-html-on-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,16 @@ module.exports = {
)
}

/** @param {VElement} element */
function isCustomComponent(element) {
if (ignoreElementNamespaces) {
return (
(!utils.isHtmlWellKnownElementName(element.rawName) &&
!utils.isSvgWellKnownElementName(element.rawName) &&
!utils.isMathWellKnownElementName(element.rawName)) ||
utils.hasAttribute(element, 'is') ||
utils.hasDirective(element, 'bind', 'is') ||
utils.hasDirective(element, 'is')
)
}

return utils.isCustomComponent(element)
}

/**
* Verify for v-text and v-html directive
* @param {VDirective} node
*/
function verify(node) {
const element = node.parent.parent
if (isCustomComponent(element) && !isAllowedComponent(element)) {
if (
utils.isCustomComponent(element, ignoreElementNamespaces) &&
!isAllowedComponent(element)
) {
context.report({
node,
loc: node.loc,
Expand Down
27 changes: 19 additions & 8 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,19 +941,30 @@ module.exports = {
/**
* Check whether the given node is a custom component or not.
* @param {VElement} node The start tag node to check.
* @param {boolean} [ignoreElementNamespaces=false] If `true`, ignore element namespaces.
* @returns {boolean} `true` if the node is a custom component.
*/
isCustomComponent(node) {
return (
(this.isHtmlElementNode(node) &&
!this.isHtmlWellKnownElementName(node.rawName)) ||
(this.isSvgElementNode(node) &&
!this.isSvgWellKnownElementName(node.rawName)) ||
(this.isMathElementNode(node) &&
!this.isMathWellKnownElementName(node.rawName)) ||
isCustomComponent(node, ignoreElementNamespaces = false) {
if (
hasAttribute(node, 'is') ||
hasDirective(node, 'bind', 'is') ||
hasDirective(node, 'is')
) {
return true
}

const isHtmlName = this.isHtmlWellKnownElementName(node.rawName)
const isSvgName = this.isSvgWellKnownElementName(node.rawName)
const isMathName = this.isMathWellKnownElementName(node.rawName)

if (ignoreElementNamespaces) {
return !isHtmlName && !isSvgName && !isMathName
}

return (
(this.isHtmlElementNode(node) && !isHtmlName) ||
(this.isSvgElementNode(node) && !isSvgName) ||
(this.isMathElementNode(node) && !isMathName)
)
},

Expand Down

0 comments on commit ba16740

Please sign in to comment.