Skip to content
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

Add prefer-true-attribute-shorthand rule #1796

Merged
merged 7 commits into from Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -353,6 +353,7 @@ For example:
| [vue/no-v-text](./no-v-text.md) | disallow use of v-text | |
| [vue/padding-line-between-blocks](./padding-line-between-blocks.md) | require or disallow padding lines between blocks | :wrench: |
| [vue/prefer-separate-static-class](./prefer-separate-static-class.md) | require static class names in template to be in a separate `class` attribute | :wrench: |
| [vue/prefer-true-attribute-shorthand](./prefer-true-attribute-shorthand.md) | require shorthand form attribute when `v-bind` value is `true`. | :bulb: |
| [vue/require-direct-export](./require-direct-export.md) | require the component to be directly exported | |
| [vue/require-emit-validator](./require-emit-validator.md) | require type definitions in emits | :bulb: |
| [vue/require-expose](./require-expose.md) | require declare public properties using `expose` | :bulb: |
Expand Down
68 changes: 68 additions & 0 deletions docs/rules/prefer-true-attribute-shorthand.md
@@ -0,0 +1,68 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/prefer-true-attribute-shorthand
description: require shorthand form attribute when `v-bind` value is `true`.
---
# vue/prefer-true-attribute-shorthand

> require shorthand form attribute when `v-bind` value is `true`.

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).

## :book: Rule Details

`v-bind` attribute with `true` value usually can be written in shorthand form. This can reduce verbosity.

<eslint-code-block :rules="{'vue/prefer-true-attribute-shorthand': ['error']}">

```vue
<template>
<!-- ✗ BAD -->
<MyComponent v-bind:show="true" />
<MyComponent :show="true" />

<!-- ✓ GOOD -->
<MyComponent show />
<MyComponent another-prop="true" />
</template>
```

</eslint-code-block>

However, those two representations are not always equivalent.
This case will be occurred if the definition of a prop includes `String`:

```vue
<template>
<pre>{{ prop }}</pre>
</template>

<script>
export default {
name: 'MyComponent',
props: {
prop: [String, Boolean]
}
}
</script>
```

```vue
<template>
<MyComponent prop />
<MyComponent :prop="true"/>
</template>
```

Those two calls will introduce different render result. See [this demo](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCBNeUNvbXBvbmVudCBmcm9tICcuL015Q29tcG9uZW50LnZ1ZSdcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxNeUNvbXBvbmVudCBhIGIvPlxuICA8TXlDb21wb25lbnQgOmE9XCJ0cnVlXCIgOmI9XCJ0cnVlXCIvPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59IiwiTXlDb21wb25lbnQudnVlIjoiPHNjcmlwdD5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgcHJvcHM6IHtcbiAgICBhOiBCb29sZWFuLFxuICAgIGI6IFtTdHJpbmcsIEJvb2xlYW5dXG4gIH1cbn1cbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxwcmU+XG5hOiB7e2F9fVxuYjoge3tifX1cbiAgPC9wcmU+XG48L3RlbXBsYXRlPiJ9).
g-plane marked this conversation as resolved.
Show resolved Hide resolved

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/prefer-true-attribute-shorthand.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/prefer-true-attribute-shorthand.js)
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -159,6 +159,7 @@ module.exports = {
'padding-line-between-blocks': require('./rules/padding-line-between-blocks'),
'prefer-separate-static-class': require('./rules/prefer-separate-static-class'),
'prefer-template': require('./rules/prefer-template'),
'prefer-true-attribute-shorthand': require('./rules/prefer-true-attribute-shorthand'),
'prop-name-casing': require('./rules/prop-name-casing'),
'quote-props': require('./rules/quote-props'),
'require-component-is': require('./rules/require-component-is'),
Expand Down
69 changes: 69 additions & 0 deletions lib/rules/prefer-true-attribute-shorthand.js
@@ -0,0 +1,69 @@
/**
* @author Pig Fang
* See LICENSE file in root directory for full license.
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

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

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
type: 'problem',
g-plane marked this conversation as resolved.
Show resolved Hide resolved
docs: {
description:
'require shorthand form attribute when `v-bind` value is `true`.',
g-plane marked this conversation as resolved.
Show resolved Hide resolved
categories: undefined,
url: 'https://eslint.vuejs.org/rules/prefer-true-attribute-shorthand.html'
},
fixable: null,
hasSuggestions: true,
schema: [],
messages: {
report:
"Boolean prop with 'true' value can be written in shorthand form.",
fix: 'Rewrite this prop into shorthand form.'
}
},
/** @param {RuleContext} context */
create(context) {
// ...

return utils.defineTemplateBodyVisitor(context, {
g-plane marked this conversation as resolved.
Show resolved Hide resolved
/** @param {VDirective} node */
"VAttribute[directive=true][key.name.name='bind']"(node) {
const { argument } = node.key
if (
!argument ||
!node.value ||
!node.value.expression ||
node.value.expression.type !== 'Literal' ||
node.value.expression.value !== true
) {
return
}

context.report({
node,
messageId: 'report',
suggest: [
{
messageId: 'fix',
fix: (fixer) => {
const sourceCode = context.getSourceCode()
return fixer.replaceText(node, sourceCode.getText(argument))
}
}
]
})
}
})
}
}
133 changes: 133 additions & 0 deletions tests/lib/rules/prefer-true-attribute-shorthand.js
@@ -0,0 +1,133 @@
/**
* @author Pig Fang
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/prefer-true-attribute-shorthand')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('prefer-true-attribute-shorthand', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<MyComp v-if="true" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp v-bind="true" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp v-loading="true" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp show="true" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp v-bind:show="value" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp :show="value" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp v-bind:show="false" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<MyComp :show="false" />
</template>
`
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<MyComp v-bind:show="true" />
</template>`,
errors: [
{
messageId: 'report',
line: 3,
column: 17,
suggestions: [
{
messageId: 'fix',
output: `
<template>
<MyComp show />
</template>`
}
]
}
]
},
{
filename: 'test.vue',
code: `
<template>
<MyComp :show="true" />
</template>`,
errors: [
{
messageId: 'report',
line: 3,
column: 17,
suggestions: [
{
messageId: 'fix',
output: `
<template>
<MyComp show />
</template>`
}
]
}
]
}
]
})