-
-
Notifications
You must be signed in to change notification settings - Fork 780
/
blocklist.ts
96 lines (91 loc) · 2.9 KB
/
blocklist.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import type { ESLintUtils } from '@typescript-eslint/utils'
import type { RuleListener } from '@typescript-eslint/utils/ts-eslint'
import type { TSESTree } from '@typescript-eslint/types'
import { CLASS_FIELDS } from '../constants'
import { createRule, syncAction } from './_'
import { IGNORE_ATTRIBUTES } from './order-attributify'
export default createRule({
name: 'blocklist',
meta: {
type: 'problem',
fixable: 'code',
docs: {
description: 'Utilities in UnoCSS blocklist',
recommended: 'recommended',
},
messages: {
'in-blocklist': 'Utility \'{{ name }}\' is in blocklist',
},
schema: [],
},
defaultOptions: [],
create(context) {
const checkLiteral = (node: TSESTree.Literal) => {
if (typeof node.value !== 'string' || !node.value.trim())
return
const input = node.value
const blocked = syncAction('blocklist', input, context.filename)
blocked.forEach((i) => {
context.report({
node,
messageId: 'in-blocklist',
data: {
name: i,
},
})
})
}
const scriptVisitor: RuleListener = {
JSXAttribute(node) {
if (typeof node.name.name === 'string' && CLASS_FIELDS.includes(node.name.name.toLowerCase()) && node.value) {
if (node.value.type === 'Literal')
checkLiteral(node.value)
}
},
SvelteAttribute(node: any) {
if (node.key.name === 'class') {
if (node.value?.[0].type === 'SvelteLiteral')
checkLiteral(node.value[0])
}
},
}
const templateBodyVisitor: RuleListener = {
VAttribute(node: any) {
if (node.key.name === 'class') {
if (node.value.type === 'VLiteral')
checkLiteral(node.value)
}
},
// Attributify
VStartTag(node: any) {
const valueless = node.attributes.filter((i: any) => typeof i.key?.name === 'string' && !IGNORE_ATTRIBUTES.includes(i.key?.name?.toLowerCase()) && i.value == null)
if (!valueless.length)
return
for (const node of valueless) {
if (!node?.key?.name)
continue
const blocked = syncAction('blocklist', node.key.name, context.filename)
blocked.forEach((i) => {
context.report({
node,
messageId: 'in-blocklist',
data: {
name: i,
},
})
})
}
},
}
const parserServices = context?.sourceCode.parserServices || context.parserServices
// @ts-expect-error missing-types
if (parserServices == null || parserServices.defineTemplateBodyVisitor == null) {
return scriptVisitor
}
else {
// For Vue
// @ts-expect-error missing-types
return parserServices?.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor)
}
},
}) as any as ESLintUtils.RuleWithMeta<[], ''>