diff --git a/src/rules/grid-unknown-attributes.js b/src/rules/grid-unknown-attributes.js index 8667833..4fe5686 100644 --- a/src/rules/grid-unknown-attributes.js +++ b/src/rules/grid-unknown-attributes.js @@ -1,6 +1,6 @@ 'use strict' -const { hyphenate, classify, getAttributes } = require('../util/helpers') +const { hyphenate, classify, getAttributes, isVueTemplate } = require('../util/helpers') const { isGridAttribute } = require('../util/grid-attributes') const { addClass, removeAttr } = require('../util/fixers') @@ -32,6 +32,8 @@ module.exports = { schema: [], }, create (context) { + if (!isVueTemplate(context)) return {} + return context.sourceCode.parserServices.defineTemplateBodyVisitor({ VElement (element) { const tag = classify(element.rawName) diff --git a/src/rules/icon-button-variant.js b/src/rules/icon-button-variant.js index 6a7cf0d..e1476b3 100644 --- a/src/rules/icon-button-variant.js +++ b/src/rules/icon-button-variant.js @@ -1,6 +1,6 @@ 'use strict' -const { classify, getAttributes } = require('../util/helpers') +const { classify, getAttributes, isVueTemplate } = require('../util/helpers') // ------------------------------------------------------------------------------ // Rule Definition @@ -22,6 +22,8 @@ module.exports = { }, create (context) { + if (!isVueTemplate(context)) return {} + return context.sourceCode.parserServices.defineTemplateBodyVisitor({ VElement (element) { const tag = classify(element.rawName) diff --git a/src/rules/no-deprecated-classes.js b/src/rules/no-deprecated-classes.js index bfe2a21..113ad4d 100755 --- a/src/rules/no-deprecated-classes.js +++ b/src/rules/no-deprecated-classes.js @@ -1,5 +1,7 @@ 'use strict' +const { isVueTemplate } = require('../util/helpers') + /** @type {Map string | false) | false> | Map} */ const replacements = new Map([ [/^rounded-(r|l|tr|tl|br|bl)(-.*)?$/, ([side, rest]) => { @@ -56,6 +58,8 @@ module.exports = { }, create (context) { + if (!isVueTemplate(context)) return {} + return context.sourceCode.parserServices.defineTemplateBodyVisitor({ 'VAttribute[key.name="class"]' (node) { if (!node.value || !node.value.value) return diff --git a/src/rules/no-deprecated-colors.js b/src/rules/no-deprecated-colors.js index e5de6a0..013b374 100644 --- a/src/rules/no-deprecated-colors.js +++ b/src/rules/no-deprecated-colors.js @@ -1,5 +1,7 @@ 'use strict' +const { isVueTemplate } = require('../util/helpers') + const cssColors = [ 'red', 'pink', 'purple', 'deep-purple', 'indigo', 'blue', 'light-blue', 'cyan', @@ -45,6 +47,8 @@ module.exports = { }, create (context) { + if (!isVueTemplate(context)) return {} + const themeColors = ['primary', 'secondary', 'accent', 'error', 'warning', 'info', 'success', ...(context.options[0]?.themeColors || [])] const themeTextColors = themeColors.map(v => `${v}--text`) diff --git a/src/rules/no-deprecated-components.js b/src/rules/no-deprecated-components.js index e46264f..6b7b302 100644 --- a/src/rules/no-deprecated-components.js +++ b/src/rules/no-deprecated-components.js @@ -1,6 +1,6 @@ 'use strict' -const { hyphenate, classify } = require('../util/helpers') +const { hyphenate, classify, isVueTemplate } = require('../util/helpers') const replacements = { VListTile: 'v-list-item', @@ -55,6 +55,8 @@ module.exports = { }, }, create (context) { + if (!isVueTemplate(context)) return {} + return context.sourceCode.parserServices.defineTemplateBodyVisitor({ VElement (element) { const tag = classify(element.rawName) diff --git a/src/rules/no-deprecated-events.js b/src/rules/no-deprecated-events.js index bc525b6..e1aeadd 100644 --- a/src/rules/no-deprecated-events.js +++ b/src/rules/no-deprecated-events.js @@ -1,6 +1,6 @@ 'use strict' -const { hyphenate, classify } = require('../util/helpers') +const { hyphenate, classify, isVueTemplate } = require('../util/helpers') const model = { input: 'update:modelValue', @@ -158,6 +158,8 @@ module.exports = { }, create (context) { + if (!isVueTemplate(context)) return {} + return context.sourceCode.parserServices.defineTemplateBodyVisitor({ VAttribute (attr) { if (!(attr.directive && attr.key.name.name === 'on' && attr.key.argument?.type === 'VIdentifier')) return diff --git a/src/rules/no-deprecated-props.js b/src/rules/no-deprecated-props.js index 50ad6cd..47018e4 100644 --- a/src/rules/no-deprecated-props.js +++ b/src/rules/no-deprecated-props.js @@ -1,6 +1,6 @@ 'use strict' -const { hyphenate, classify } = require('../util/helpers') +const { hyphenate, classify, isVueTemplate } = require('../util/helpers') const size = { maxHeight: false, @@ -632,6 +632,8 @@ module.exports = { }, create (context) { + if (!isVueTemplate(context)) return {} + return context.sourceCode.parserServices.defineTemplateBodyVisitor({ VStartTag (tag) { const attrGroups = {} diff --git a/src/rules/no-deprecated-slots.js b/src/rules/no-deprecated-slots.js index 4dc5bcc..6ba76cd 100644 --- a/src/rules/no-deprecated-slots.js +++ b/src/rules/no-deprecated-slots.js @@ -1,6 +1,6 @@ 'use strict' -const { classify, getAttributes } = require('../util/helpers') +const { classify, getAttributes, isVueTemplate } = require('../util/helpers') const groups = [ { @@ -185,6 +185,8 @@ module.exports = { }, create (context) { + if (!isVueTemplate(context)) return {} + let scopeStack return context.sourceCode.parserServices.defineTemplateBodyVisitor({ diff --git a/src/util/helpers.js b/src/util/helpers.js index ed0f244..cfdd9cb 100644 --- a/src/util/helpers.js +++ b/src/util/helpers.js @@ -1,5 +1,7 @@ 'use strict' +const path = require('node:path') + function hyphenate ( /* istanbul ignore next */ str = '' @@ -64,6 +66,13 @@ function mergeDeep (source, target) { return source } +function isVueTemplate (context) { + if (context.sourceCode.parserServices.defineTemplateBodyVisitor == null) { + return path.extname(context.getFilename()) === '.vue' + } + return true +} + module.exports = { hyphenate, classify, @@ -71,4 +80,5 @@ module.exports = { getAttributes, isObject, mergeDeep, + isVueTemplate, }