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

feat: Add vue/no-multiple-objects-in-class rule #1218

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -285,6 +285,7 @@ For example:
| [vue/no-bare-strings-in-template](./no-bare-strings-in-template.md) | disallow the use of bare strings in `<template>` | |
| [vue/no-boolean-default](./no-boolean-default.md) | disallow boolean defaults | :wrench: |
| [vue/no-duplicate-attr-inheritance](./no-duplicate-attr-inheritance.md) | enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"` | |
| [vue/no-multiple-objects-in-class](./no-multiple-objects-in-class.md) | disallow to pass multiple objects into array to class | |
| [vue/no-potential-component-option-typo](./no-potential-component-option-typo.md) | disallow a potential typo in your component property | |
| [vue/no-reserved-component-names](./no-reserved-component-names.md) | disallow the use of reserved names in component definitions | |
| [vue/no-restricted-static-attribute](./no-restricted-static-attribute.md) | disallow specific attribute | |
Expand Down
37 changes: 37 additions & 0 deletions docs/rules/no-multiple-objects-in-class.md
@@ -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)
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -73,6 +73,7 @@ module.exports = {
'no-irregular-whitespace': require('./rules/no-irregular-whitespace'),
'no-lifecycle-after-await': require('./rules/no-lifecycle-after-await'),
'no-multi-spaces': require('./rules/no-multi-spaces'),
'no-multiple-objects-in-class': require('./rules/no-multiple-objects-in-class'),
'no-multiple-slot-args': require('./rules/no-multiple-slot-args'),
'no-multiple-template-root': require('./rules/no-multiple-template-root'),
'no-mutating-props': require('./rules/no-mutating-props'),
Expand Down
59 changes: 59 additions & 0 deletions lib/rules/no-multiple-objects-in-class.js
@@ -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'
})
}
}
})
}
}
48 changes: 48 additions & 0 deletions tests/lib/rules/no-multiple-objects-in-class.js
@@ -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>`
Copy link
Contributor Author

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 thanv-bind:class consideration.

],
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'
}
]
}
]
})