-
-
Notifications
You must be signed in to change notification settings - Fork 680
Add prefer-true-attribute-shorthand
rule
#1796
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
ota-meshi
merged 7 commits into
vuejs:master
from
g-plane:prefer-true-attribute-shorthand
Feb 12, 2022
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ed8113
Add `prefer-true-attribute-shorthand` rule
g-plane 62119d5
fix typo
g-plane dbfe89c
accept suggestions
g-plane 83e4311
accept option `"always"` or `"never"`
g-plane ca41132
ignore native HTML elements
g-plane 2188af9
provide suggestions instead of auto fix
g-plane df75aff
meta update
g-plane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). | ||
|
||
## :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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} | ||
] | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>` | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.