-
Notifications
You must be signed in to change notification settings - Fork 5
/
no-config-function-in-source.ts
110 lines (94 loc) · 3.09 KB
/
no-config-function-in-source.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { isIdentifier, isVariableDeclaration } from '../utils/nodes'
import { type Rule, createRule } from '../utils'
import { getAncestor, getImportSpecifiers, hasPkgImport, isPandaConfigFunction, isValidFile } from '../utils/helpers'
import { TSESTree } from '@typescript-eslint/utils'
export const RULE_NAME = 'no-config-function-in-source'
const CONFIG_FUNCTIONS = new Set([
'defineConfig',
'defineRecipe',
'defineSlotRecipe',
'defineParts',
'definePattern',
'definePreset',
'defineKeyframes',
'defineGlobalStyles',
'defineUtility',
'defineTextStyles',
'defineLayerStyles',
'defineStyles',
'defineTokens',
'defineSemanticTokens',
])
const rule: Rule = createRule({
name: RULE_NAME,
meta: {
docs: {
description: 'Prohibit the use of config functions outside the Panda config file.',
},
messages: {
configFunction: 'Unnecessary `{{name}}` call. Config functions should only be used in the Panda config file.',
delete: 'Delete `{{name}}` call.',
},
type: 'problem',
hasSuggestions: true,
schema: [],
},
defaultOptions: [],
create(context) {
// Check if the package is imported; if not, exit early
if (!hasPkgImport(context)) {
return {}
}
// Determine if the current file is the Panda config file
const isPandaFile = isValidFile(context)
// If we are in the config file, no need to proceed
if (!isPandaFile) {
return {}
}
return {
CallExpression(node: TSESTree.CallExpression) {
// Ensure the callee is an identifier
if (!isIdentifier(node.callee)) return
const functionName = node.callee.name
// Check if the function is a config function
if (!CONFIG_FUNCTIONS.has(functionName)) return
// Verify that it's a Panda config function
if (!isPandaConfigFunction(context, functionName)) return
context.report({
node,
messageId: 'configFunction',
data: {
name: functionName,
},
suggest: [
{
messageId: 'delete',
data: {
name: functionName,
},
fix(fixer) {
const declaration = getAncestor(isVariableDeclaration, node)
const importSpecifiers = getImportSpecifiers(context)
// Find the import specifier for the function
const importSpec = importSpecifiers.find((s) => s.specifier.local.name === functionName)
const fixes = []
// Remove the variable declaration if it exists; otherwise, remove the call expression
if (declaration) {
fixes.push(fixer.remove(declaration))
} else {
fixes.push(fixer.remove(node))
}
// Remove the import specifier if it exists
if (importSpec?.specifier) {
fixes.push(fixer.remove(importSpec.specifier))
}
return fixes
},
},
],
})
},
}
},
})
export default rule