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 vue/no-v-text
rule
#1605
Add vue/no-v-text
rule
#1605
Changes from 2 commits
411e6c8
4cd6ec6
6238491
5a9b764
bc030be
5c535ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-v-text | ||
description: disallow use of v-text | ||
since: v7.17.0 | ||
--- | ||
# vue/no-v-text | ||
|
||
> disallow use of v-text | ||
|
||
- :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. | ||
|
||
- However, when using selfClose to element with v-text like `<div v-text="foobar" />`, it can't be fixed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The header part is automatically generated, so the autofix sentence may not come to the bottom in the future. Could you list it in another place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
## :book: Rule Details | ||
|
||
This rule reports all uses of `v-text` directive. | ||
|
||
|
||
<eslint-code-block fix :rules="{'vue/no-v-text': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<div>{{ foobar }}</div> | ||
|
||
<!-- ✗ BAD --> | ||
<div v-text="foobar"></div> | ||
<!-- Reported. However, Not fixable --> | ||
<div v-text="foobar" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in eslint-plugin-vue v7.17.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-v-text.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-v-text.js) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @author tyankatsu <https://github.com/tyankatsu0105> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* | ||
* @param {VElement} node | ||
* @returns {{node: VAttribute | VDirective, value: string}} | ||
*/ | ||
const getVText = (node) => { | ||
const vText = node.startTag.attributes.find( | ||
(attribute) => | ||
attribute.key.type === 'VDirectiveKey' && | ||
attribute.key.name.name === 'text' | ||
) | ||
|
||
if (!vText) return | ||
if (!vText.value) return | ||
if (vText.value.type !== 'VExpressionContainer') return | ||
if (!vText.value.expression) return | ||
if (vText.value.expression.type !== 'Identifier') return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why limit it to an Identifier? I think all expressions should be reported. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's my misunderstood. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed. |
||
|
||
const vTextValue = vText.value.expression.name | ||
|
||
return { | ||
node: vText, | ||
value: vTextValue | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow use of v-text', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-v-text.html' | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
/** @param {VElement} node */ | ||
"VElement:has(VAttribute[directive=true][key.name.name='text'])"(node) { | ||
const vText = getVText(node) | ||
if (!vText) return | ||
|
||
context.report({ | ||
node: vText.node, | ||
loc: vText.loc, | ||
message: "Don't use 'v-text'.", | ||
fix(fixable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think autofix is very difficult. For example, converting the following expression to <div v-text="'</div>'"></div> ↓ <div >{{'</div>'}}</div> So if you want to implement autofix, we need to limit autofix by the content of the expression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I make sense of what there are some points that I have to think about. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done
tyankatsu0105 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (node.startTag.selfClosing) return | ||
|
||
return [ | ||
fixable.remove(vText.node), | ||
fixable.insertTextAfterRange( | ||
node.startTag.range, | ||
`{{${vText.value}}}` | ||
) | ||
] | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @author tyankatsu <https://github.com/tyankatsu0105> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-v-text') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
ruleTester.run('no-v-text', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div>{{foobar}}</div></template>' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-text="foobar"></div></template>', | ||
output: '<template><div >{{foobar}}</div></template>', | ||
errors: ["Don't use 'v-text'."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-text="foobar" /></template>', | ||
output: '<template><div v-text="foobar" /></template>', | ||
errors: ["Don't use 'v-text'."] | ||
} | ||
] | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"since" is set automatically at release. So for now remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
6238491