Skip to content

Add vueFeatures.customMacros option #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ For example:
"filter": true,
"interpolationAsNonHTML": true,
"styleCSSVariableInjection": true,
"customMacros": []
}
}
}
Expand Down Expand Up @@ -213,6 +214,13 @@ If set to `true`, to parse expressions in `v-bind` CSS functions inside `<style>

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

### parserOptions.vueFeatures.customMacros

Specifies an array of names of custom macros other than Vue standard macros.
For example, if you have a custom macro `defineFoo()` and you want it processed by the parser, specify `["defineFoo"]`.

Note that this option only works in `<script setup>`.

### parserOptions.templateTokenizer

**This is an experimental feature. It may be changed or deleted without notice in the minor version.**
Expand Down
1 change: 1 addition & 0 deletions src/common/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ParserOptions {
interpolationAsNonHTML?: boolean // default true
filter?: boolean // default true
styleCSSVariableInjection?: boolean // default true
customMacros?: string[]
}

// espree options
Expand Down
17 changes: 14 additions & 3 deletions src/script-setup/scope-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export function analyzeScriptSetupScope(
scopeManager: escopeTypes.ScopeManager,
templateBody: VElement | undefined,
df: VDocumentFragment,
_parserOptions: ParserOptions,
parserOptions: ParserOptions,
): void {
analyzeUsedInTemplateVariables(scopeManager, templateBody, df)

analyzeCompilerMacrosVariables(scopeManager)
analyzeCompilerMacrosVariables(scopeManager, parserOptions)
}

function extractVariables(scopeManager: escopeTypes.ScopeManager) {
Expand Down Expand Up @@ -290,11 +290,19 @@ function analyzeUsedInTemplateVariables(
*/
function analyzeCompilerMacrosVariables(
scopeManager: escopeTypes.ScopeManager,
parserOptions: ParserOptions,
) {
const globalScope = scopeManager.globalScope
if (!globalScope) {
return
}
const customMacros = new Set(
parserOptions.vueFeatures?.customMacros &&
Array.isArray(parserOptions.vueFeatures.customMacros)
? parserOptions.vueFeatures.customMacros
: [],
)

const compilerMacroVariables = new Map<string, escopeTypes.Variable>()

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

const newThrough: escopeTypes.Reference[] = []
for (const reference of globalScope.through) {
if (COMPILER_MACROS_AT_ROOT.has(reference.identifier.name)) {
if (
COMPILER_MACROS_AT_ROOT.has(reference.identifier.name) ||
customMacros.has(reference.identifier.name)
) {
if (
reference.from.type === "global" ||
reference.from.type === "module"
Expand Down
283 changes: 283 additions & 0 deletions test/fixtures/ast/user-macro01/ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
{
"type": "Program",
"start": 15,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 23
}
},
"range": [
15,
38
],
"body": [
{
"type": "VariableDeclaration",
"start": 15,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 23
}
},
"range": [
15,
38
],
"declarations": [
{
"type": "VariableDeclarator",
"start": 21,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 23
}
},
"range": [
21,
38
],
"id": {
"type": "Identifier",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
},
"range": [
21,
24
],
"name": "foo"
},
"init": {
"type": "CallExpression",
"start": 27,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 23
}
},
"range": [
27,
38
],
"callee": {
"type": "Identifier",
"start": 27,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 21
}
},
"range": [
27,
36
],
"name": "userMacro"
},
"arguments": [],
"optional": false
}
}
],
"kind": "const"
}
],
"sourceType": "module",
"comments": [],
"tokens": [
{
"type": "Punctuator",
"range": [
0,
14
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"value": "<script>"
},
{
"type": "Keyword",
"value": "const",
"start": 15,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 5
}
},
"range": [
15,
20
]
},
{
"type": "Identifier",
"value": "foo",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
},
"range": [
21,
24
]
},
{
"type": "Punctuator",
"value": "=",
"start": 25,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
}
},
"range": [
25,
26
]
},
{
"type": "Identifier",
"value": "userMacro",
"start": 27,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 21
}
},
"range": [
27,
36
]
},
{
"type": "Punctuator",
"value": "(",
"start": 36,
"end": 37,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 22
}
},
"range": [
36,
37
]
},
{
"type": "Punctuator",
"value": ")",
"start": 37,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 22
},
"end": {
"line": 2,
"column": 23
}
},
"range": [
37,
38
]
},
{
"type": "Punctuator",
"range": [
39,
48
],
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 9
}
},
"value": "</script>"
}
]
}
6 changes: 6 additions & 0 deletions test/fixtures/ast/user-macro01/parser-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sourceType": "module",
"vueFeatures": {
"customMacros": ["userMacro"]
}
}
3 changes: 3 additions & 0 deletions test/fixtures/ast/user-macro01/requirements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
Loading