-
-
Notifications
You must be signed in to change notification settings - Fork 681
feat: Add vue/no-multiple-objects-in-class
rule
#1218
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 8 commits into
vuejs:master
from
tyankatsu0105:add-rule/no-multiple-object-in-class
Jun 26, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a94ab88
feat: Add
tyankatsu0105 ad508eb
feat: Add rule, test, and docs
tyankatsu0105 9c26cb0
chore: run update
tyankatsu0105 7d69151
chore: rename no-multiple-objects-in-class
tyankatsu0105 41b6cdf
chore: fix docs url
tyankatsu0105 ce8f812
docs: fix pattern
tyankatsu0105 8f695e6
fix: consider bind
tyankatsu0105 c437c64
chore: remove unused function
tyankatsu0105 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,37 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-multiple-objects-in-class | ||
description: disallow to pass multiple objects into array to class | ||
--- | ||
# vue/no-multiple-objects-in-class | ||
> disallow to pass multiple objects into array to class | ||
|
||
## :book: Rule Details | ||
|
||
This rule disallows to pass multiple objects into array to class. | ||
|
||
<eslint-code-block :rules="{'vue/no-multiple-objects-in-class': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<!-- ✓ GOOD --> | ||
<div :class="[{'foo': isFoo, 'bar': isBar}]" /> | ||
|
||
<!-- ✗ BAD --> | ||
<div :class="[{'foo': isFoo}, {'bar': isBar}]" /> | ||
</div> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-multiple-objects-in-class.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-multiple-objects-in-class.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,59 @@ | ||
/** | ||
* @author tyankatsu <https://github.com/tyankatsu0105> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const { defineTemplateBodyVisitor } = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* count ObjectExpression element | ||
* @param {VAttribute} node | ||
* @return {number} | ||
*/ | ||
function countObjectExpression(node) { | ||
return node.value.expression.elements.filter( | ||
(element) => element.type === 'ObjectExpression' | ||
).length | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow to pass multiple objects into array to class', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-multiple-objects-in-class.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
unexpected: 'Unexpected multiple objects. Merge objects.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
return defineTemplateBodyVisitor(context, { | ||
/** @param {VAttribute} node */ | ||
'VAttribute[directive=true][key.argument.name="class"][key.name.name="bind"][value.expression.type="ArrayExpression"]'( | ||
node | ||
) { | ||
if (countObjectExpression(node) > 1) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'unexpected' | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,48 @@ | ||
/** | ||
* @author tyankatsu <https://github.com/tyankatsu0105> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-multiple-objects-in-class') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015, sourceType: 'module' } | ||
}) | ||
|
||
ruleTester.run('no-multiple-objects-in-class', rule, { | ||
valid: [ | ||
`<template><div :class="[{'foo': isFoo}]" /></template>`, | ||
`<template><div :class="[{'foo': isFoo, 'bar': isBar}]" /></template>`, | ||
`<template><div v-foo:class="[{'foo': isFoo}, {'bar': isBar}]" /></template>` | ||
], | ||
invalid: [ | ||
{ | ||
code: `<template><div v-bind:class="[{'foo': isFoo}, {'bar': isBar}]" /></template>`, | ||
errors: [ | ||
{ | ||
message: 'Unexpected multiple objects. Merge objects.', | ||
type: 'VAttribute' | ||
} | ||
] | ||
}, | ||
{ | ||
code: `<template><div :class="[{'foo': isFoo}, {'bar': isBar}]" /></template>`, | ||
errors: [ | ||
{ | ||
message: 'Unexpected multiple objects. Merge objects.', | ||
type: 'VAttribute' | ||
} | ||
] | ||
} | ||
] | ||
}) |
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.
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.
This rule don't take into other than
v-bind:class
consideration.