Skip to content

Commit

Permalink
Ignore template literals
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Feb 11, 2024
1 parent 6c2004d commit ca3d8da
Showing 1 changed file with 2 additions and 57 deletions.
59 changes: 2 additions & 57 deletions plugin/src/rules/no-margin-properties.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isPandaAttribute, isPandaIsh, isPandaProp, resolveLonghand } from '../utils/helpers'
import { isPandaAttribute, isPandaProp, resolveLonghand } from '../utils/helpers'
import { type Rule, createRule } from '../utils'
import { isCallExpression, isIdentifier, isJSXIdentifier, isMemberExpression } from '../utils/nodes'
import { isIdentifier, isJSXIdentifier } from '../utils/nodes'
import type { TSESTree } from '@typescript-eslint/utils'

export const RULE_NAME = 'no-margin-properties'
Expand Down Expand Up @@ -46,61 +46,6 @@ const rule: Rule = createRule({

sendReport(node.key)
},

TaggedTemplateExpression(node) {
const handleExpression = (caller: string) => {
if (!isPandaIsh(caller, context)) return

const quasis = node.quasi.quasis[0]
const styles = quasis.value.raw

const propertyRegex = /([a-zA-Z-]+):/g
const propertyMatches = [...styles.matchAll(propertyRegex)]
const properties = propertyMatches.map((match) => match[1].trim())

properties.forEach((property, i, arr) => {
if (!property.includes('margin')) return

// Avoid duplicate reports on the same token
if (arr.indexOf(property) < i) return

let index = styles.indexOf(property)

while (index !== -1) {
const start = quasis.range[0] + 1 + index
const end = start + property.length

context.report({
loc: {
start: context.sourceCode.getLocFromIndex(start),
end: context.sourceCode.getLocFromIndex(end),
},
messageId: 'noMargin',
})

// Check for other occurences of the invalid token
index = styles.indexOf(property, index + 1)
}
})
}

// css``
if (isIdentifier(node.tag)) {
handleExpression(node.tag.name)
}

// styled.h1``
if (isMemberExpression(node.tag)) {
if (!isIdentifier(node.tag.object)) return
handleExpression(node.tag.object.name)
}

// styled(Comp)``
if (isCallExpression(node.tag)) {
if (!isIdentifier(node.tag.callee)) return
handleExpression(node.tag.callee.name)
}
},
}
},
})
Expand Down

0 comments on commit ca3d8da

Please sign in to comment.