Skip to content

Add support for v-bind same-name shorthand #215

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 4 commits into from
Jan 8, 2024
Merged
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
10 changes: 1 addition & 9 deletions src/script-setup/scope-analyzer.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ import {
isScriptElement,
isScriptSetupElement,
} from "../common/ast-utils"
import { camelize } from "../utils/utils"

const BUILTIN_COMPONENTS = new Set([
"template",
@@ -94,15 +95,6 @@ const COMPILER_MACROS_AT_ROOT = new Set([
"defineModel",
])

/**
* `casing.camelCase()` converts the beginning to lowercase,
* but does not convert the case of the beginning character when converting with Vue3.
* @see https://github.com/vuejs/vue-next/blob/48de8a42b7fed7a03f7f1ff5d53d6a704252cafe/packages/shared/src/index.ts#L109
*/
function camelize(str: string) {
return str.replace(/-(\w)/gu, (_, c) => (c ? c.toUpperCase() : ""))
}

function capitalize(str: string) {
return str[0].toUpperCase() + str.slice(1)
}
71 changes: 71 additions & 0 deletions src/template/index.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ import type { ParserOptions } from "../common/parser-options"
import { isSFCFile } from "../common/parser-options"
import type {
ESLintExpression,
ESLintExtendedProgram,
ESLintIdentifier,
Reference,
Token,
VAttribute,
@@ -34,6 +36,7 @@ import {
parseVOnExpression,
parseSlotScopeExpression,
parseGenericExpression,
parseScriptFragment,
} from "../script"
import {
createSimpleToken,
@@ -46,6 +49,7 @@ import {
isTSLang,
} from "../common/ast-utils"
import { insertError } from "../common/error-utils"
import { camelize } from "../utils/utils"

const shorthandSign = /^[.:@#]/u
const shorthandNameMap = { ":": "bind", ".": "bind", "@": "on", "#": "slot" }
@@ -626,6 +630,14 @@ export function convertToDirective(
}

if (node.value == null) {
if (directive.key.name.name === "bind") {
// v-bind same-name shorthand (Vue 3.4+)
convertForVBindSameNameShorthandValue(
directive,
parserOptions,
locationCalculator,
)
}
return
}

@@ -677,6 +689,65 @@ export function convertToDirective(
}
}

function convertForVBindSameNameShorthandValue(
directive: VDirective,
parserOptions: ParserOptions,
locationCalculator: LocationCalculatorForHtml,
) {
if (
directive.key.name.name !== "bind" ||
directive.key.argument == null ||
directive.key.argument.type !== "VIdentifier"
) {
return
}
// v-bind same-name shorthand (Vue 3.4+)
const vId = directive.key.argument
const camelName = camelize(vId.name)
let result: ESLintExtendedProgram | null = null
try {
result = parseScriptFragment(
camelName,
locationCalculator.getSubCalculatorAfter(vId.range[0]),
parserOptions,
)
} catch (err) {
debug("[template] Parse error: %s", err)
}
if (
result == null ||
result.ast.body.length !== 1 ||
result.ast.body[0].type !== "ExpressionStatement" ||
result.ast.body[0].expression.type !== "Identifier"
) {
return
}
const id: ESLintIdentifier = result.ast.body[0].expression
id.range[1] = vId.range[1]
id.loc.end = { ...vId.loc.end }
if (id.end != null) {
id.end = vId.end
}
directive.value = {
type: "VExpressionContainer",
range: [...vId.range],
loc: {
start: { ...vId.loc.start },
end: { ...vId.loc.end },
},
parent: directive,
expression: id,
references: [
{
id,
mode: "r",
variable: null,
},
],
}
id.parent = directive.value
}

/**
* Parse the content of the given mustache.
* @param parserOptions The parser options to parse expressions.
6 changes: 6 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @see https://github.com/vuejs/vue-next/blob/48de8a42b7fed7a03f7f1ff5d53d6a704252cafe/packages/shared/src/index.ts#L109
*/
export function camelize(str: string) {
return str.replace(/-(\w)/gu, (_, c) => (c ? c.toUpperCase() : ""))
}
62 changes: 61 additions & 1 deletion test/fixtures/ast/directive-shorthands/ast.json
Original file line number Diff line number Diff line change
@@ -207,7 +207,67 @@
}
]
},
"value": null
"value": {
"type": "VExpressionContainer",
"range": [
21,
22
],
"loc": {
"start": {
"column": 10,
"line": 2
},
"end": {
"column": 11,
"line": 2
}
},
"expression": {
"type": "Identifier",
"start": 21,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"column": 11,
"line": 2
}
},
"range": [
21,
22
],
"name": "a"
},
"references": [
{
"id": {
"type": "Identifier",
"start": 21,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"column": 11,
"line": 2
}
},
"range": [
21,
22
],
"name": "a"
},
"mode": "r",
"variable": null
}
]
}
}
]
},
11 changes: 11 additions & 0 deletions test/fixtures/ast/directive-shorthands/tree.json
Original file line number Diff line number Diff line change
@@ -45,6 +45,17 @@
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "a",
"children": [
{
"type": "Identifier",
"text": "a",
"children": []
}
]
}
]
}
968 changes: 968 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand01-script-setup/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
154 changes: 154 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand01-script-setup/scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [
{
"name": "src",
"identifiers": [
{
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
}
],
"defs": [
{
"type": "Variable",
"node": {
"type": "VariableDeclarator",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 42
}
}
},
"name": "src"
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
},
"init": true
},
{
"identifier": {
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
},
"init": null,
"vueUsedInTemplate": true
}
]
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "src",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
}
},
"init": true
}
],
"childScopes": [],
"through": []
}
],
"through": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup>
const src = "https://github.com/vuejs.png"
</script>

<template>
<img :src>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
"<script setup>",
"const",
"src",
"=",
"\"https://github.com/vuejs.png\"",
"</script>",
"<script",
"setup",
">",
"\n",
"const",
" ",
"src",
" ",
"=",
" ",
"\"https://github.com/vuejs.png\"",
"\n",
"</script",
">",
"\n\n",
"<template",
">",
"\n ",
"<img",
":",
"src",
">",
"\n",
"</template",
">",
"\n"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[
{
"type": "VElement",
"text": "<template>\n <img :src>\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<img :src>",
"children": [
{
"type": "VStartTag",
"text": "<img :src>",
"children": [
{
"type": "VAttribute",
"text": ":src",
"children": [
{
"type": "VDirectiveKey",
"text": ":src",
"children": [
{
"type": "VIdentifier",
"text": ":",
"children": []
},
{
"type": "VIdentifier",
"text": "src",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "src",
"children": [
{
"type": "Identifier",
"text": "src",
"children": []
}
]
}
]
}
]
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
1,644 changes: 1,644 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand02-options/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
30 changes: 30 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand02-options/scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [],
"references": [],
"childScopes": [
{
"type": "function",
"variables": [
{
"name": "arguments",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
"childScopes": [],
"through": []
}
],
"through": []
}
],
"through": []
}
13 changes: 13 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand02-options/source.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
export default {
data () {
return {
src: "https://github.com/vuejs.png"
}
}
}
</script>

<template>
<img :src>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
"<script>",
"export",
"default",
"{",
"data",
"(",
")",
"{",
"return",
"{",
"src",
":",
"\"https://github.com/vuejs.png\"",
"}",
"}",
"}",
"</script>",
"<script",
">",
"\n",
"export",
" ",
"default",
" ",
"{",
"\n ",
"data",
" ",
"()",
" ",
"{",
"\n ",
"return",
" ",
"{",
"\n ",
"src:",
" ",
"\"https://github.com/vuejs.png\"",
"\n ",
"}",
"\n ",
"}",
"\n",
"}",
"\n",
"</script",
">",
"\n\n",
"<template",
">",
"\n ",
"<img",
":",
"src",
">",
"\n",
"</template",
">",
"\n"
]
73 changes: 73 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand02-options/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[
{
"type": "VElement",
"text": "<template>\n <img :src>\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<img :src>",
"children": [
{
"type": "VStartTag",
"text": "<img :src>",
"children": [
{
"type": "VAttribute",
"text": ":src",
"children": [
{
"type": "VDirectiveKey",
"text": ":src",
"children": [
{
"type": "VIdentifier",
"text": ":",
"children": []
},
{
"type": "VIdentifier",
"text": "src",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "src",
"children": [
{
"type": "Identifier",
"text": "src",
"children": []
}
]
}
]
}
]
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
1,736 changes: 1,736 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand03-with-v-for/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
154 changes: 154 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand03-with-v-for/scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [
{
"name": "srcList",
"identifiers": [
{
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
}
],
"defs": [
{
"type": "Variable",
"node": {
"type": "VariableDeclarator",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 5,
"column": 7
}
}
},
"name": "srcList"
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
},
"init": true
},
{
"identifier": {
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
},
"init": null,
"vueUsedInTemplate": true
}
]
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "srcList",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
}
}
},
"init": true
}
],
"childScopes": [],
"through": []
}
],
"through": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup>
const srcList = [
"https://github.com/vuejs.png",
"https://github.com/vitejs.png"
]
</script>

<template>
<div>
<img v-for="src in srcList" :src>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[
"<script setup>",
"const",
"srcList",
"=",
"[",
"\"https://github.com/vuejs.png\"",
",",
"\"https://github.com/vitejs.png\"",
"]",
"</script>",
"<script",
"setup",
">",
"\n",
"const",
" ",
"srcList",
" ",
"=",
" ",
"[",
"\n ",
"\"https://github.com/vuejs.png\",",
"\n ",
"\"https://github.com/vitejs.png\"",
"\n ",
"]",
"\n",
"</script",
">",
"\n\n",
"<template",
">",
"\n ",
"<div",
">",
"\n ",
"<img",
"v-for",
"=",
"\"",
"src",
"in",
"srcList",
"\"",
":",
"src",
">",
"\n ",
"</div",
">",
"\n",
"</template",
">",
"\n"
]
138 changes: 138 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand03-with-v-for/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[
{
"type": "VElement",
"text": "<template>\n <div>\n <img v-for=\"src in srcList\" :src>\n </div>\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<div>\n <img v-for=\"src in srcList\" :src>\n </div>",
"children": [
{
"type": "VStartTag",
"text": "<div>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<img v-for=\"src in srcList\" :src>",
"children": [
{
"type": "VStartTag",
"text": "<img v-for=\"src in srcList\" :src>",
"children": [
{
"type": "VAttribute",
"text": "v-for=\"src in srcList\"",
"children": [
{
"type": "VDirectiveKey",
"text": "v-for",
"children": [
{
"type": "VIdentifier",
"text": "v-for",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "\"src in srcList\"",
"children": [
{
"type": "VForExpression",
"text": "src in srcList",
"children": [
{
"type": "Identifier",
"text": "src",
"children": []
},
{
"type": "Identifier",
"text": "srcList",
"children": []
}
]
}
]
}
]
},
{
"type": "VAttribute",
"text": ":src",
"children": [
{
"type": "VDirectiveKey",
"text": ":src",
"children": [
{
"type": "VIdentifier",
"text": ":",
"children": []
},
{
"type": "VIdentifier",
"text": "src",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "src",
"children": [
{
"type": "Identifier",
"text": "src",
"children": []
}
]
}
]
}
]
}
]
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VEndTag",
"text": "</div>",
"children": []
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
2,412 changes: 2,412 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand04-with-v-for/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [],
"references": [],
"childScopes": [
{
"type": "function",
"variables": [
{
"name": "arguments",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
"childScopes": [],
"through": []
}
],
"through": []
}
],
"through": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
export default {
data () {
return {
srcList: [
"https://github.com/vuejs.png",
"https://github.com/vitejs.png"
]
}
}
}
</script>

<template>
<div>
<img v-for="src in srcList" :src>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[
"<script>",
"export",
"default",
"{",
"data",
"(",
")",
"{",
"return",
"{",
"srcList",
":",
"[",
"\"https://github.com/vuejs.png\"",
",",
"\"https://github.com/vitejs.png\"",
"]",
"}",
"}",
"}",
"</script>",
"<script",
">",
"\n",
"export",
" ",
"default",
" ",
"{",
"\n ",
"data",
" ",
"()",
" ",
"{",
"\n ",
"return",
" ",
"{",
"\n ",
"srcList:",
" ",
"[",
"\n ",
"\"https://github.com/vuejs.png\",",
"\n ",
"\"https://github.com/vitejs.png\"",
"\n ",
"]",
"\n ",
"}",
"\n ",
"}",
"\n",
"}",
"\n",
"</script",
">",
"\n\n",
"<template",
">",
"\n ",
"<div",
">",
"\n ",
"<img",
"v-for",
"=",
"\"",
"src",
"in",
"srcList",
"\"",
":",
"src",
">",
"\n ",
"</div",
">",
"\n",
"</template",
">",
"\n"
]
138 changes: 138 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand04-with-v-for/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[
{
"type": "VElement",
"text": "<template>\n <div>\n <img v-for=\"src in srcList\" :src>\n </div>\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<div>\n <img v-for=\"src in srcList\" :src>\n </div>",
"children": [
{
"type": "VStartTag",
"text": "<div>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<img v-for=\"src in srcList\" :src>",
"children": [
{
"type": "VStartTag",
"text": "<img v-for=\"src in srcList\" :src>",
"children": [
{
"type": "VAttribute",
"text": "v-for=\"src in srcList\"",
"children": [
{
"type": "VDirectiveKey",
"text": "v-for",
"children": [
{
"type": "VIdentifier",
"text": "v-for",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "\"src in srcList\"",
"children": [
{
"type": "VForExpression",
"text": "src in srcList",
"children": [
{
"type": "Identifier",
"text": "src",
"children": []
},
{
"type": "Identifier",
"text": "srcList",
"children": []
}
]
}
]
}
]
},
{
"type": "VAttribute",
"text": ":src",
"children": [
{
"type": "VDirectiveKey",
"text": ":src",
"children": [
{
"type": "VIdentifier",
"text": ":",
"children": []
},
{
"type": "VIdentifier",
"text": "src",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "src",
"children": [
{
"type": "Identifier",
"text": "src",
"children": []
}
]
}
]
}
]
}
]
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VEndTag",
"text": "</div>",
"children": []
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
1,166 changes: 1,166 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand05-kebab/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
154 changes: 154 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand05-kebab/scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [
{
"name": "ariaLabel",
"identifiers": [
{
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
}
],
"defs": [
{
"type": "Variable",
"node": {
"type": "VariableDeclarator",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 25
}
}
},
"name": "ariaLabel"
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
},
"init": true
},
{
"identifier": {
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
},
"init": null,
"vueUsedInTemplate": true
}
]
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "ariaLabel",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
}
},
"init": true
}
],
"childScopes": [],
"through": []
}
],
"through": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup>
const ariaLabel = "Close"
</script>

<template>
<button :aria-label id="close-button">X</button>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
"<script setup>",
"const",
"ariaLabel",
"=",
"\"Close\"",
"</script>",
"<script",
"setup",
">",
"\n",
"const",
" ",
"ariaLabel",
" ",
"=",
" ",
"\"Close\"",
"\n",
"</script",
">",
"\n\n",
"<template",
">",
"\n ",
"<button",
":",
"aria-label",
"id",
"=",
"\"close-button\"",
">",
"X",
"</button",
">",
"\n",
"</template",
">",
"\n"
]
99 changes: 99 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand05-kebab/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[
{
"type": "VElement",
"text": "<template>\n <button :aria-label id=\"close-button\">X</button>\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<button :aria-label id=\"close-button\">X</button>",
"children": [
{
"type": "VStartTag",
"text": "<button :aria-label id=\"close-button\">",
"children": [
{
"type": "VAttribute",
"text": ":aria-label",
"children": [
{
"type": "VDirectiveKey",
"text": ":aria-label",
"children": [
{
"type": "VIdentifier",
"text": ":",
"children": []
},
{
"type": "VIdentifier",
"text": "aria-label",
"children": []
}
]
},
{
"type": "VExpressionContainer",
"text": "aria-label",
"children": [
{
"type": "Identifier",
"text": "aria-label",
"children": []
}
]
}
]
},
{
"type": "VAttribute",
"text": "id=\"close-button\"",
"children": [
{
"type": "VIdentifier",
"text": "id",
"children": []
},
{
"type": "VLiteral",
"text": "\"close-button\"",
"children": []
}
]
}
]
},
{
"type": "VText",
"text": "X",
"children": []
},
{
"type": "VEndTag",
"text": "</button>",
"children": []
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
1,078 changes: 1,078 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand06-invalid/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint": ">=8"
}
154 changes: 154 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand06-invalid/scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [
{
"name": "title",
"identifiers": [
{
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
}
],
"defs": [
{
"type": "Variable",
"node": {
"type": "VariableDeclarator",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 21
}
}
},
"name": "title"
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"init": true
},
{
"identifier": {
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"init": null,
"vueUsedInTemplate": true
}
]
}
],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"from": "module",
"resolved": {
"type": "Identifier",
"name": "title",
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
}
},
"init": true
}
],
"childScopes": [],
"through": []
}
],
"through": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup>
const title = 'title'
</script>

<template>
<button :[title] >X</button>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
"<script setup>",
"const",
"title",
"=",
"'title'",
"</script>",
"<script",
"setup",
">",
"\n",
"const",
" ",
"title",
" ",
"=",
" ",
"'title'",
"\n",
"</script",
">",
"\n\n",
"<template",
">",
"\n ",
"<button",
":",
"[",
"title",
"]",
">",
"X",
"</button",
">",
"\n",
"</template",
">",
"\n"
]
78 changes: 78 additions & 0 deletions test/fixtures/ast/v-bind-same-name-shorthand06-invalid/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[
{
"type": "VElement",
"text": "<template>\n <button :[title] >X</button>\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n ",
"children": []
},
{
"type": "VElement",
"text": "<button :[title] >X</button>",
"children": [
{
"type": "VStartTag",
"text": "<button :[title] >",
"children": [
{
"type": "VAttribute",
"text": ":[title]",
"children": [
{
"type": "VDirectiveKey",
"text": ":[title]",
"children": [
{
"type": "VIdentifier",
"text": ":",
"children": []
},
{
"type": "VExpressionContainer",
"text": "[title]",
"children": [
{
"type": "Identifier",
"text": "title",
"children": []
}
]
}
]
}
]
}
]
},
{
"type": "VText",
"text": "X",
"children": []
},
{
"type": "VEndTag",
"text": "</button>",
"children": []
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
58 changes: 58 additions & 0 deletions test/variables-references.js
Original file line number Diff line number Diff line change
@@ -358,3 +358,61 @@ describe("Variables of v-for and references of dynamic arguments", () => {
assert(vBindKeyReferences[0].variable === variables[0])
})
})

describe("Variables of v-for and references of v-bind same-name shorthand", () => {
const code = '<template><div v-for="x of xs" :x /></template>'
let variables = null
let vForReferences = null
let vBindReferences = null

before(() => {
const ast = parse(
code,
Object.assign({ filePath: "test.vue" }, PARSER_OPTIONS),
).ast
variables = ast.templateBody.children[0].variables
vForReferences =
ast.templateBody.children[0].startTag.attributes[0].value.references
vBindReferences =
ast.templateBody.children[0].startTag.attributes[1].value.references
})

it("should have relationship each other", () => {
assert(variables.length === 1)
assert(vForReferences.length === 1)
assert(vBindReferences.length === 1)
assert(variables[0].references.length === 1)
assert(variables[0].references[0] === vBindReferences[0])
assert(vForReferences[0].variable === null)
assert(vBindReferences[0].variable === variables[0])
})
})

describe("Variables of v-for and references of v-bind same-name shorthand with kebab-case", () => {
const code = '<template><div v-for="dataXx of xs" :data-xx /></template>'
let variables = null
let vForReferences = null
let vBindReferences = null

before(() => {
const ast = parse(
code,
Object.assign({ filePath: "test.vue" }, PARSER_OPTIONS),
).ast
variables = ast.templateBody.children[0].variables
vForReferences =
ast.templateBody.children[0].startTag.attributes[0].value.references
vBindReferences =
ast.templateBody.children[0].startTag.attributes[1].value.references
})

it("should have relationship each other", () => {
assert(variables.length === 1)
assert(vForReferences.length === 1)
assert(vBindReferences.length === 1)
assert(variables[0].references.length === 1)
assert(variables[0].references[0] === vBindReferences[0])
assert(vForReferences[0].variable === null)
assert(vBindReferences[0].variable === variables[0])
})
})