Skip to content

feat: add new v-bind-same-name-style rule #2374

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ For example:
| [vue/prefer-true-attribute-shorthand](./prefer-true-attribute-shorthand.md) | require shorthand form attribute when `v-bind` value is `true` | :bulb: | :hammer: |
| [vue/require-direct-export](./require-direct-export.md) | require the component to be directly exported | | :hammer: |
| [vue/require-emit-validator](./require-emit-validator.md) | require type definitions in emits | :bulb: | :hammer: |
| [vue/require-explicit-slots](./require-explicit-slots.md) | require slots to be explicitly defined | | :warning: |
| [vue/require-expose](./require-expose.md) | require declare public properties using `expose` | :bulb: | :hammer: |
| [vue/require-macro-variable-name](./require-macro-variable-name.md) | require a certain macro variable name | :bulb: | :hammer: |
| [vue/require-name-property](./require-name-property.md) | require a name property in Vue components | :bulb: | :hammer: |
Expand All @@ -278,6 +279,7 @@ For example:
| [vue/script-indent](./script-indent.md) | enforce consistent indentation in `<script>` | :wrench: | :lipstick: |
| [vue/sort-keys](./sort-keys.md) | enforce sort-keys in a manner that is compatible with order-in-components | | :hammer: |
| [vue/static-class-names-order](./static-class-names-order.md) | enforce static class names order | :wrench: | :hammer: |
| [vue/v-bind-same-name-style](./v-bind-same-name-style.md) | enforce `v-bind` same name directive style | :wrench: | :hammer: |
| [vue/v-for-delimiter-style](./v-for-delimiter-style.md) | enforce `v-for` directive's delimiter style | :wrench: | :lipstick: |
| [vue/v-if-else-key](./v-if-else-key.md) | require key attribute for conditionally rendered repeated components | :wrench: | :warning: |
| [vue/v-on-handler-style](./v-on-handler-style.md) | enforce writing style for handlers in `v-on` directives | :wrench: | :hammer: |
Expand Down
1 change: 0 additions & 1 deletion docs/rules/no-restricted-static-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ title: vue/no-restricted-static-attribute
description: disallow specific attribute
since: v7.0.0
---

# vue/no-restricted-static-attribute

> disallow specific attribute
Expand Down
1 change: 0 additions & 1 deletion docs/rules/no-restricted-v-bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ title: vue/no-restricted-v-bind
description: disallow specific argument in `v-bind`
since: v7.0.0
---

# vue/no-restricted-v-bind

> disallow specific argument in `v-bind`
Expand Down
8 changes: 6 additions & 2 deletions docs/rules/require-explicit-slots.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
pageClass: rule-details
sidebarDepth: 0
title: vue/require-explicit-slots
description: require slots to be explicitly defined with defineSlots
description: require slots to be explicitly defined
---

# vue/require-explicit-slots

> require slots to be explicitly defined
Expand Down Expand Up @@ -66,3 +65,8 @@ defineComponent({
## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-explicit-slots.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-explicit-slots.js)
68 changes: 68 additions & 0 deletions docs/rules/v-bind-same-name-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/v-bind-same-name-style
description: enforce `v-bind` same name directive style
---
# vue/v-bind-same-name-style

> enforce `v-bind` same name directive style

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

This rule enforces `v-bind` same-name directive style which you should use always or never use shorthand form.

<eslint-code-block fix :rules="{'vue/v-bind-same-name-style': ['error', 'never']}">

```vue
<template>
<!-- ✓ GOOD -->
<div :foo="foo" />
<div v-bind:foo="foo" />

<!-- ✗ BAD -->
<div :foo />
<div v-bind:foo />
</template>
```

</eslint-code-block>

## :wrench: Options

Default is set to `never`.

```json
{
"vue/v-bind-same-name-style": ["error", "never" | "always"]
}
```

- `"never"` (default) ... requires never using shorthand.
- `"always"` ... requires using shorthand.

### `"always"`

<eslint-code-block fix :rules="{'vue/v-bind-same-name-style': ['error', 'always']}">

```vue
<template>
<!-- ✓ GOOD -->
<div :foo />
<div v-bind:foo />

<!-- ✗ BAD -->
<div :foo="foo" />
<div v-bind:foo="foo" />
</template>
```

</eslint-code-block>

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/v-bind-same-name-style.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/v-bind-same-name-style.js)
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ module.exports = {
'require-direct-export': require('./rules/require-direct-export'),
'require-emit-validator': require('./rules/require-emit-validator'),
'require-explicit-emits': require('./rules/require-explicit-emits'),
'require-explicit-slots': require('./rules/require-explicit-slots'),
'require-expose': require('./rules/require-expose'),
'require-macro-variable-name': require('./rules/require-macro-variable-name'),
'require-name-property': require('./rules/require-name-property'),
Expand All @@ -216,6 +217,7 @@ module.exports = {
'template-curly-spacing': require('./rules/template-curly-spacing'),
'this-in-template': require('./rules/this-in-template'),
'use-v-on-exact': require('./rules/use-v-on-exact'),
'v-bind-same-name-style': require('./rules/v-bind-same-name-style'),
'v-bind-style': require('./rules/v-bind-style'),
'v-for-delimiter-style': require('./rules/v-for-delimiter-style'),
'v-if-else-key': require('./rules/v-if-else-key'),
Expand Down
122 changes: 122 additions & 0 deletions lib/rules/v-bind-same-name-style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* @author waynzh
* See LICENSE file in root directory for full license.
*/
'use strict'

const utils = require('../utils')
const casing = require('../utils/casing')

/**
* @typedef { VDirectiveKey & { name: VIdentifier & { name: 'bind' }, argument: VExpressionContainer | VIdentifier } } VBindDirectiveKey
* @typedef { VDirective & { key: VBindDirectiveKey } } VBindDirective
*/

/**
* @param {VBindDirective} node
* @returns {string | null}
*/
function getAttributeName(node) {
// not support VExpressionContainer e.g. :[attribute]
if (node.key.argument.type === 'VIdentifier') {
return node.key.argument.rawName
}

return null
}

/**
* @param {VBindDirective} node
* @returns {string | null}
*/
function getValueName(node) {
if (node.value?.expression?.type === 'Identifier') {
return node.value.expression.name
}

return null
}

/**
* @param {VBindDirective} node
* @returns {boolean}
*/
function isSameName(node) {
const attrName = getAttributeName(node)
const valueName = getValueName(node)
return Boolean(
attrName &&
valueName &&
casing.camelCase(attrName) === casing.camelCase(valueName)
)
}

/**
* @param {VBindDirectiveKey} key
* @returns {number}
*/
function getCutStart(key) {
const modifiers = key.modifiers
return modifiers.length > 0
? modifiers[modifiers.length - 1].range[1]
: key.argument.range[1]
}

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce `v-bind` same name directive style',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/v-bind-same-name-style.html'
},
fixable: 'code',
schema: [{ enum: ['always', 'never'] }],
messages: {
expectedShorthand: 'Expected shorthand same name.',
unexpectedShorthand: 'Unexpected shorthand same name.'
}
},
/** @param {RuleContext} context */
create(context) {
const preferShorthand = context.options[0] === 'always'

return utils.defineTemplateBodyVisitor(context, {
/** @param {VBindDirective} node */
"VAttribute[directive=true][key.name.name='bind'][key.argument!=null]"(
node
) {
if (!isSameName(node)) return

const isShortHand = utils.isVBindSameNameShorthand(node)
if (isShortHand === preferShorthand) {
return
}

let messageId = 'unexpectedShorthand'
if (preferShorthand) {
messageId = 'expectedShorthand'
}

context.report({
node,
loc: node.loc,
messageId,
*fix(fixer) {
if (preferShorthand) {
/** @type {Range} */
const valueRange = [getCutStart(node.key), node.range[1]]

yield fixer.removeRange(valueRange)
} else if (node.key.argument.type === 'VIdentifier') {
yield fixer.insertTextAfter(
node,
`="${casing.camelCase(node.key.argument.rawName)}"`
)
}
}
})
}
})
}
}
Loading