-
-
Notifications
You must be signed in to change notification settings - Fork 700
feat: add vue/no-literals-in-template rule #3000
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
+773
−0
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9c5eb00
feat: add vue/no-literals-in-template rule
rzzf 73f038f
Add changeset
FloEdelmann 893f5c6
refactor: refine type
rzzf f590a95
chore: tweak message
rzzf ec4e393
docs: update description
rzzf 27ea5e6
test: add case
rzzf 0f2a52a
test: add cases
rzzf 2f0127c
test: update
rzzf a9085d4
chore: format
rzzf ba11964
Update tests/lib/rules/no-literals-in-template.js
rzzf bae2247
Update tests/lib/rules/no-literals-in-template.js
rzzf 391ed02
feat: ignore v-for and slot reference
rzzf 6c496dc
docs: update example
rzzf 78f7e68
Rename variable
FloEdelmann 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "eslint-plugin-vue": minor | ||
| --- | ||
|
|
||
| Added new `vue/no-literals-in-template` rule |
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,55 @@ | ||
| --- | ||
| pageClass: rule-details | ||
| sidebarDepth: 0 | ||
| title: vue/no-literals-in-template | ||
| description: disallow object, array, and function literals in template | ||
| --- | ||
|
|
||
| # vue/no-literals-in-template | ||
|
|
||
| > disallow object, array, and function literals in template | ||
|
|
||
| - :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge> | ||
|
|
||
| ## :book: Rule Details | ||
|
|
||
| This rule disallows object, array, and function literals in template `v-bind` directives. | ||
| These literals are created as new references on every rerender, which can cause the child component's watchers to trigger unnecessarily even when the object hasn't actually changed. | ||
|
|
||
| <eslint-code-block :rules="{'vue/no-literals-in-template': ['error']}"> | ||
|
|
||
| ```vue | ||
| <template> | ||
| <!-- ✓ GOOD --> | ||
| <div :arr="myArray" /> | ||
| <div :obj="myObject" /> | ||
| <div :callback="myFunction" /> | ||
| <div arr="[]" /> | ||
| <!-- class and style bindings are ignored --> | ||
| <div :class="{ active: isActive }" /> | ||
| <div :style="{ color: 'red' }" /> | ||
|
|
||
| <!-- ✗ BAD --> | ||
| <div :arr="[]" /> | ||
| <div :arr="[1, 2, 3]" /> | ||
| <div :obj="{}" /> | ||
| <div :obj="{ name: 'Tom', age: 123 }" /> | ||
| <div :callback="() => someFunction(someArgs)" /> | ||
| <div :callback="function() { return 1 }" /> | ||
| </template> | ||
| ``` | ||
|
|
||
| </eslint-code-block> | ||
|
|
||
| ## :wrench: Options | ||
|
|
||
| Nothing. | ||
|
|
||
| ## :books: Further Reading | ||
|
|
||
| - [vuejs/vue#4060](https://github.com/vuejs/vue/issues/4060) | ||
|
|
||
| ## :mag: Implementation | ||
|
|
||
| - [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-literals-in-template.js) | ||
| - [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-literals-in-template.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,62 @@ | ||
| /** | ||
| * @author rzzf | ||
| * See LICENSE file in root directory for full license. | ||
| */ | ||
| 'use strict' | ||
|
|
||
| const utils = require('../utils') | ||
|
|
||
| /** | ||
| * @type {Record<string, 'object' | 'array' | 'function' | 'arrow function' | undefined>} | ||
| */ | ||
| const EXPRESSION_TYPES = { | ||
| ObjectExpression: 'object', | ||
| ArrayExpression: 'array', | ||
| FunctionExpression: 'function', | ||
| ArrowFunctionExpression: 'arrow function' | ||
| } | ||
|
|
||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'disallow object, array, and function literals in template', | ||
| categories: undefined, | ||
| url: 'https://eslint.vuejs.org/rules/no-literals-in-template.html' | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| messages: { | ||
| unexpected: 'Unexpected {{type}} literal in template.' | ||
| } | ||
| }, | ||
| /** @param {RuleContext} context */ | ||
| create(context) { | ||
| return utils.defineTemplateBodyVisitor(context, { | ||
| /** | ||
| * @param {VDirective} node | ||
| */ | ||
| "VAttribute[directive=true][key.name.name='bind']"(node) { | ||
| const expression = node.value?.expression | ||
| if ( | ||
| !expression || | ||
| (node.key.argument && | ||
| node.key.argument.type === 'VIdentifier' && | ||
| (node.key.argument.name === 'class' || | ||
| node.key.argument.name === 'style')) | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| const type = EXPRESSION_TYPES[expression.type] | ||
| if (type) { | ||
| context.report({ | ||
| node: expression, | ||
| messageId: 'unexpected', | ||
| data: { type } | ||
| }) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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,173 @@ | ||
| /** | ||
| * @author rzzf | ||
| * See LICENSE file in root directory for full license. | ||
| */ | ||
| 'use strict' | ||
|
|
||
| const RuleTester = require('../../eslint-compat').RuleTester | ||
| const rule = require('../../../lib/rules/no-literals-in-template') | ||
|
|
||
| const tester = new RuleTester({ | ||
| languageOptions: { | ||
| parser: require('vue-eslint-parser'), | ||
| ecmaVersion: 2020, | ||
| sourceType: 'module' | ||
| } | ||
| }) | ||
|
|
||
| tester.run('no-literals-in-template', rule, { | ||
| valid: [ | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :arr="myArray"></div></template>' | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :obj="myObject"></div></template>' | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :callback="myFunction"></div></template>' | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div v-bind="myProps"></div></template>' | ||
| }, | ||
| // Excluded attributes | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :class="{ active: isActive }"></div></template>' | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: `<template><div :style="{ color: 'red' }"></div></template>` | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: `<template><div :class="['active', errorClass]"></div></template>` | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :style="[baseStyles, overridingStyles]"></div></template>' | ||
| } | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
rzzf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| invalid: [ | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :arr="[]"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected array literal in template.', | ||
| line: 1, | ||
| column: 22, | ||
| endLine: 1, | ||
| endColumn: 24 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :arr="[1,2,3]"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected array literal in template.', | ||
| line: 1, | ||
| column: 22, | ||
| endLine: 1, | ||
| endColumn: 29 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :obj="{}"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected object literal in template.', | ||
| line: 1, | ||
| column: 22, | ||
| endLine: 1, | ||
| endColumn: 24 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: `<template><div :obj="{name:'Tom', age: 123}"></div></template>`, | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected object literal in template.', | ||
| line: 1, | ||
| column: 22, | ||
| endLine: 1, | ||
| endColumn: 44 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :callback="() => someFunction(someArgs)"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected arrow function literal in template.', | ||
| line: 1, | ||
| column: 27, | ||
| endLine: 1, | ||
| endColumn: 55 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :callback="function() { return 1 }"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected function literal in template.', | ||
| line: 1, | ||
| column: 27, | ||
| endLine: 1, | ||
| endColumn: 50 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :arr="[...myArray]"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected array literal in template.', | ||
| line: 1, | ||
| column: 22, | ||
| endLine: 1, | ||
| endColumn: 34 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div :arr="{...myObject}"></div></template>', | ||
rzzf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| errors: [ | ||
| { | ||
| message: 'Unexpected object literal in template.', | ||
| line: 1, | ||
| column: 22, | ||
| endLine: 1, | ||
| endColumn: 35 | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| filename: 'test.vue', | ||
| code: '<template><div v-bind="{foo: 1}"></div></template>', | ||
| errors: [ | ||
| { | ||
| message: 'Unexpected object literal in template.', | ||
| line: 1, | ||
| column: 24, | ||
| endLine: 1, | ||
| endColumn: 32 | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
rzzf marked this conversation as resolved.
Show resolved
Hide resolved
rzzf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
Oops, something went wrong.
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.