Skip to content

Commit 92c0d88

Browse files
authored
Add vueFeatures.customMacros option (#194)
1 parent 8e8aa96 commit 92c0d88

21 files changed

+1015
-3
lines changed

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ For example:
142142
"filter": true,
143143
"interpolationAsNonHTML": true,
144144
"styleCSSVariableInjection": true,
145+
"customMacros": []
145146
}
146147
}
147148
}
@@ -213,6 +214,13 @@ If set to `true`, to parse expressions in `v-bind` CSS functions inside `<style>
213214

214215
See also to [here](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0043-sfc-style-variables.md).
215216

217+
### parserOptions.vueFeatures.customMacros
218+
219+
Specifies an array of names of custom macros other than Vue standard macros.
220+
For example, if you have a custom macro `defineFoo()` and you want it processed by the parser, specify `["defineFoo"]`.
221+
222+
Note that this option only works in `<script setup>`.
223+
216224
### parserOptions.templateTokenizer
217225

218226
**This is an experimental feature. It may be changed or deleted without notice in the minor version.**

Diff for: src/common/parser-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ParserOptions {
1616
interpolationAsNonHTML?: boolean // default true
1717
filter?: boolean // default true
1818
styleCSSVariableInjection?: boolean // default true
19+
customMacros?: string[]
1920
}
2021

2122
// espree options

Diff for: src/script-setup/scope-analyzer.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ export function analyzeScriptSetupScope(
113113
scopeManager: escopeTypes.ScopeManager,
114114
templateBody: VElement | undefined,
115115
df: VDocumentFragment,
116-
_parserOptions: ParserOptions,
116+
parserOptions: ParserOptions,
117117
): void {
118118
analyzeUsedInTemplateVariables(scopeManager, templateBody, df)
119119

120-
analyzeCompilerMacrosVariables(scopeManager)
120+
analyzeCompilerMacrosVariables(scopeManager, parserOptions)
121121
}
122122

123123
function extractVariables(scopeManager: escopeTypes.ScopeManager) {
@@ -290,11 +290,19 @@ function analyzeUsedInTemplateVariables(
290290
*/
291291
function analyzeCompilerMacrosVariables(
292292
scopeManager: escopeTypes.ScopeManager,
293+
parserOptions: ParserOptions,
293294
) {
294295
const globalScope = scopeManager.globalScope
295296
if (!globalScope) {
296297
return
297298
}
299+
const customMacros = new Set(
300+
parserOptions.vueFeatures?.customMacros &&
301+
Array.isArray(parserOptions.vueFeatures.customMacros)
302+
? parserOptions.vueFeatures.customMacros
303+
: [],
304+
)
305+
298306
const compilerMacroVariables = new Map<string, escopeTypes.Variable>()
299307

300308
function addCompilerMacroVariable(reference: escopeTypes.Reference) {
@@ -315,7 +323,10 @@ function analyzeCompilerMacrosVariables(
315323

316324
const newThrough: escopeTypes.Reference[] = []
317325
for (const reference of globalScope.through) {
318-
if (COMPILER_MACROS_AT_ROOT.has(reference.identifier.name)) {
326+
if (
327+
COMPILER_MACROS_AT_ROOT.has(reference.identifier.name) ||
328+
customMacros.has(reference.identifier.name)
329+
) {
319330
if (
320331
reference.from.type === "global" ||
321332
reference.from.type === "module"

Diff for: test/fixtures/ast/user-macro01/ast.json

+283
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
{
2+
"type": "Program",
3+
"start": 15,
4+
"end": 38,
5+
"loc": {
6+
"start": {
7+
"line": 2,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 2,
12+
"column": 23
13+
}
14+
},
15+
"range": [
16+
15,
17+
38
18+
],
19+
"body": [
20+
{
21+
"type": "VariableDeclaration",
22+
"start": 15,
23+
"end": 38,
24+
"loc": {
25+
"start": {
26+
"line": 2,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 2,
31+
"column": 23
32+
}
33+
},
34+
"range": [
35+
15,
36+
38
37+
],
38+
"declarations": [
39+
{
40+
"type": "VariableDeclarator",
41+
"start": 21,
42+
"end": 38,
43+
"loc": {
44+
"start": {
45+
"line": 2,
46+
"column": 6
47+
},
48+
"end": {
49+
"line": 2,
50+
"column": 23
51+
}
52+
},
53+
"range": [
54+
21,
55+
38
56+
],
57+
"id": {
58+
"type": "Identifier",
59+
"start": 21,
60+
"end": 24,
61+
"loc": {
62+
"start": {
63+
"line": 2,
64+
"column": 6
65+
},
66+
"end": {
67+
"line": 2,
68+
"column": 9
69+
}
70+
},
71+
"range": [
72+
21,
73+
24
74+
],
75+
"name": "foo"
76+
},
77+
"init": {
78+
"type": "CallExpression",
79+
"start": 27,
80+
"end": 38,
81+
"loc": {
82+
"start": {
83+
"line": 2,
84+
"column": 12
85+
},
86+
"end": {
87+
"line": 2,
88+
"column": 23
89+
}
90+
},
91+
"range": [
92+
27,
93+
38
94+
],
95+
"callee": {
96+
"type": "Identifier",
97+
"start": 27,
98+
"end": 36,
99+
"loc": {
100+
"start": {
101+
"line": 2,
102+
"column": 12
103+
},
104+
"end": {
105+
"line": 2,
106+
"column": 21
107+
}
108+
},
109+
"range": [
110+
27,
111+
36
112+
],
113+
"name": "userMacro"
114+
},
115+
"arguments": [],
116+
"optional": false
117+
}
118+
}
119+
],
120+
"kind": "const"
121+
}
122+
],
123+
"sourceType": "module",
124+
"comments": [],
125+
"tokens": [
126+
{
127+
"type": "Punctuator",
128+
"range": [
129+
0,
130+
14
131+
],
132+
"loc": {
133+
"start": {
134+
"line": 1,
135+
"column": 0
136+
},
137+
"end": {
138+
"line": 1,
139+
"column": 14
140+
}
141+
},
142+
"value": "<script>"
143+
},
144+
{
145+
"type": "Keyword",
146+
"value": "const",
147+
"start": 15,
148+
"end": 20,
149+
"loc": {
150+
"start": {
151+
"line": 2,
152+
"column": 0
153+
},
154+
"end": {
155+
"line": 2,
156+
"column": 5
157+
}
158+
},
159+
"range": [
160+
15,
161+
20
162+
]
163+
},
164+
{
165+
"type": "Identifier",
166+
"value": "foo",
167+
"start": 21,
168+
"end": 24,
169+
"loc": {
170+
"start": {
171+
"line": 2,
172+
"column": 6
173+
},
174+
"end": {
175+
"line": 2,
176+
"column": 9
177+
}
178+
},
179+
"range": [
180+
21,
181+
24
182+
]
183+
},
184+
{
185+
"type": "Punctuator",
186+
"value": "=",
187+
"start": 25,
188+
"end": 26,
189+
"loc": {
190+
"start": {
191+
"line": 2,
192+
"column": 10
193+
},
194+
"end": {
195+
"line": 2,
196+
"column": 11
197+
}
198+
},
199+
"range": [
200+
25,
201+
26
202+
]
203+
},
204+
{
205+
"type": "Identifier",
206+
"value": "userMacro",
207+
"start": 27,
208+
"end": 36,
209+
"loc": {
210+
"start": {
211+
"line": 2,
212+
"column": 12
213+
},
214+
"end": {
215+
"line": 2,
216+
"column": 21
217+
}
218+
},
219+
"range": [
220+
27,
221+
36
222+
]
223+
},
224+
{
225+
"type": "Punctuator",
226+
"value": "(",
227+
"start": 36,
228+
"end": 37,
229+
"loc": {
230+
"start": {
231+
"line": 2,
232+
"column": 21
233+
},
234+
"end": {
235+
"line": 2,
236+
"column": 22
237+
}
238+
},
239+
"range": [
240+
36,
241+
37
242+
]
243+
},
244+
{
245+
"type": "Punctuator",
246+
"value": ")",
247+
"start": 37,
248+
"end": 38,
249+
"loc": {
250+
"start": {
251+
"line": 2,
252+
"column": 22
253+
},
254+
"end": {
255+
"line": 2,
256+
"column": 23
257+
}
258+
},
259+
"range": [
260+
37,
261+
38
262+
]
263+
},
264+
{
265+
"type": "Punctuator",
266+
"range": [
267+
39,
268+
48
269+
],
270+
"loc": {
271+
"start": {
272+
"line": 3,
273+
"column": 0
274+
},
275+
"end": {
276+
"line": 3,
277+
"column": 9
278+
}
279+
},
280+
"value": "</script>"
281+
}
282+
]
283+
}

Diff for: test/fixtures/ast/user-macro01/parser-options.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sourceType": "module",
3+
"vueFeatures": {
4+
"customMacros": ["userMacro"]
5+
}
6+
}

Diff for: test/fixtures/ast/user-macro01/requirements.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"eslint": ">=8"
3+
}

0 commit comments

Comments
 (0)