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 vue/no-v-text rule #1605

Merged
merged 6 commits into from Aug 15, 2021
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 @@ -324,6 +324,7 @@ For example:
| [vue/no-use-computed-property-like-method](./no-use-computed-property-like-method.md) | disallow use computed property like method | |
| [vue/no-useless-mustaches](./no-useless-mustaches.md) | disallow unnecessary mustache interpolations | :wrench: |
| [vue/no-useless-v-bind](./no-useless-v-bind.md) | disallow unnecessary `v-bind` directives | :wrench: |
| [vue/no-v-text](./no-v-text.md) | disallow use of v-text | :wrench: |
| [vue/padding-line-between-blocks](./padding-line-between-blocks.md) | require or disallow padding lines between blocks | :wrench: |
| [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 | |
Expand Down
48 changes: 48 additions & 0 deletions docs/rules/no-v-text.md
@@ -0,0 +1,48 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-v-text
description: disallow use of v-text
since: v7.17.0
Copy link
Member

@ota-meshi ota-meshi Aug 13, 2021

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
6238491

---
# 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.
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
6238491


## :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)
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -132,6 +132,7 @@ module.exports = {
'no-v-for-template-key': require('./rules/no-v-for-template-key'),
'no-v-html': require('./rules/no-v-html'),
'no-v-model-argument': require('./rules/no-v-model-argument'),
'no-v-text': require('./rules/no-v-text'),
'no-watch-after-await': require('./rules/no-watch-after-await'),
'object-curly-newline': require('./rules/object-curly-newline'),
'object-curly-spacing': require('./rules/object-curly-spacing'),
Expand Down
76 changes: 76 additions & 0 deletions lib/rules/no-v-text.js
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my misunderstood.
I don't know how to use v-text very much.
I'm going to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can use utils.getDirective!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Copy link
Member

@ota-meshi ota-meshi Aug 13, 2021

Choose a reason for hiding this comment

The 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 {{}} will result in a parse error in Vue2.

<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.
Or I think it's good not to implement autofix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Yeah. I agree with your mention point.
I will remove the fix function from this rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
bc030be

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}}}`
)
]
}
})
}
})
}
}
52 changes: 52 additions & 0 deletions tests/lib/rules/no-v-text.js
@@ -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'."]
}
]
})