Skip to content

Commit

Permalink
fix: skip rules on non-vue files
Browse files Browse the repository at this point in the history
fixes #108
  • Loading branch information
KaelWD committed Nov 18, 2024
1 parent 1e3c28d commit c703efe
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/rules/grid-unknown-attributes.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/rules/icon-button-variant.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { classify, getAttributes } = require('../util/helpers')
const { classify, getAttributes, isVueTemplate } = require('../util/helpers')

// ------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -22,6 +22,8 @@ module.exports = {
},

create (context) {
if (!isVueTemplate(context)) return {}

return context.sourceCode.parserServices.defineTemplateBodyVisitor({
VElement (element) {
const tag = classify(element.rawName)
Expand Down
4 changes: 4 additions & 0 deletions src/rules/no-deprecated-classes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { isVueTemplate } = require('../util/helpers')

/** @type {Map<RegExp, ((args: string[]) => string | false) | false> | Map<string, string | false>} */
const replacements = new Map([
[/^rounded-(r|l|tr|tl|br|bl)(-.*)?$/, ([side, rest]) => {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/rules/no-deprecated-colors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { isVueTemplate } = require('../util/helpers')

const cssColors = [
'red', 'pink', 'purple', 'deep-purple',
'indigo', 'blue', 'light-blue', 'cyan',
Expand Down Expand Up @@ -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`)

Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-deprecated-components.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -55,6 +55,8 @@ module.exports = {
},
},
create (context) {
if (!isVueTemplate(context)) return {}

return context.sourceCode.parserServices.defineTemplateBodyVisitor({
VElement (element) {
const tag = classify(element.rawName)
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-deprecated-events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { hyphenate, classify } = require('../util/helpers')
const { hyphenate, classify, isVueTemplate } = require('../util/helpers')

const model = {
input: 'update:modelValue',
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-deprecated-props.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { hyphenate, classify } = require('../util/helpers')
const { hyphenate, classify, isVueTemplate } = require('../util/helpers')

const size = {
maxHeight: false,
Expand Down Expand Up @@ -632,6 +632,8 @@ module.exports = {
},

create (context) {
if (!isVueTemplate(context)) return {}

return context.sourceCode.parserServices.defineTemplateBodyVisitor({
VStartTag (tag) {
const attrGroups = {}
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-deprecated-slots.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { classify, getAttributes } = require('../util/helpers')
const { classify, getAttributes, isVueTemplate } = require('../util/helpers')

const groups = [
{
Expand Down Expand Up @@ -185,6 +185,8 @@ module.exports = {
},

create (context) {
if (!isVueTemplate(context)) return {}

let scopeStack

return context.sourceCode.parserServices.defineTemplateBodyVisitor({
Expand Down
10 changes: 10 additions & 0 deletions src/util/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const path = require('node:path')

function hyphenate (
/* istanbul ignore next */
str = ''
Expand Down Expand Up @@ -64,11 +66,19 @@ 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,
isBuiltinAttribute,
getAttributes,
isObject,
mergeDeep,
isVueTemplate,
}

0 comments on commit c703efe

Please sign in to comment.