Skip to content

New: Add vue/single-attribute-single-line rule #1431

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/rules/single-attribute-single-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/single-attribute-single-line
description: enforce component opening tags with a single attribute to be on a single line
since: v7.5.0
---
# vue/single-attribute-single-line

> enforce component opening tags with a single attribute to be on a single line

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

Ensures that component opening tags that only have one attribute are inline to improve readability.

## :book: Rule Details

This rule aims to enforce component opening tags with a single attribute to be place on a single line.
It checks all the elements in a template and verifies that when the number of attributes for a component is 1, that it is inlined on the opening tag.

<eslint-code-block fix :rules="{'vue/single-attribute-single-line': ['error']}">

```vue
<template>
<!-- ✓ GOOD -->
<MyComponent lorem="1"/>
<MyComponent lorem="1" />
<MyComponent lorem="1"></MyComponent>
<MyComponent lorem="1">
<p>content</p>
</MyComponent>

<!-- ✗ BAD -->
<MyComponent
lorem="1"
/>
<MyComponent
lorem="1"
></MyComponent>
<MyComponent
lorem="1"
>
<p>content</p>
</MyComponent>
</template>
```

</eslint-code-block>

## :wrench: Options

```json
{
"vue/single-attribute-single-line": ["error"]
}
```

</eslint-code-block>

## :rocket: Version

This rule was introduced in eslint-plugin-vue v7.5.0

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/single-attribute-single-line.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/single-attribute-single-line.js)
78 changes: 78 additions & 0 deletions lib/rules/single-attribute-single-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @fileoverview Enforce component opening tags with a single attribute to be on a single line
* @author Jackson Gross
*/
'use strict'

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const utils = require('../utils')

module.exports = {
meta: {
type: 'layout',
docs: {
description:
'enforce component opening tags with a single attribute to be on a single line',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/single-attribute-single-line.html'
},
fixable: 'whitespace', // or "code" or "whitespace"
schema: []
},
/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
const template =
context.parserServices.getTemplateBodyTokenStore &&
context.parserServices.getTemplateBodyTokenStore()

return utils.defineTemplateBodyVisitor(context, {
VStartTag(node) {
const closingTag = node.range[1] - 1
const numberOfAttributes = node.attributes.length

if (numberOfAttributes !== 1) return

if (!utils.isSingleLine(node)) {
// Find the closest token before the current prop
// that is not a white space
const prevToken = /** @type {Token} */ (template.getTokenBefore(
node.attributes[0],
{
filter: (token) => token.type !== 'HTMLWhitespace'
}
))

const startOfAttribute = node.attributes[0].range[0]
const endOfAttribute = node.attributes[0].range[1]

/** @type {Range} */
const rangeBetweenAttributeAndStartTag = [
prevToken.range[1],
startOfAttribute
]

/** @type {Range} */
const rangeBetweenAttributeAndClosingTag = [
endOfAttribute,
closingTag
]

context.report({
node,
message: "'{{name}}' should be on a single line.",
data: { name: sourceCode.getText(node.attributes[0].key) },
fix(fixer) {
return [
fixer.replaceTextRange(rangeBetweenAttributeAndClosingTag, ''),
fixer.replaceTextRange(rangeBetweenAttributeAndStartTag, ' ')
]
}
})
}
}
})
}
}
155 changes: 155 additions & 0 deletions tests/lib/rules/single-attribute-single-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* @fileoverview Enforce component opening tags with a single attribute to be on a single line
* @author Jackson Gross
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/single-attribute-single-line')

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: { ecmaVersion: 2015 }
})

ruleTester.run('single-attribute-single-line', rule, {
valid: [
{
code: `<template><component></component></template>`
},
{
code: `<template><component /></template>`
},
{
code: `<template>
<component></component>
</template>`
},
{
code: `<template>
<component />
</template>`
},
{
code: `<template><component name="John Doe"></component></template>`
},
{
code: `<template><component name="John Doe"/></template>`
},
{
code: `<template>
<component name="John Doe"></component>
</template>`
},
{
code: `<template>
<component name="John Doe" />
</template>`
},
{
code: `<template>
<component name="John Doe">
<p>Some content</p>
</component>
</template>`
},
{
code: `<template>
<component
name="John Doe"
age="12"
>
<p>Some content</p>
</component>
</template>`
}
],

invalid: [
{
code: `<template>
<component
name="John Doe"
></component>
</template>`,
output: `<template>
<component name="John Doe"></component>
</template>`,
errors: ["'name' should be on a single line."]
},
{
code: `<template>
<component
:name="user.name"
></component>
</template>`,
output: `<template>
<component :name="user.name"></component>
</template>`,
errors: ["':name' should be on a single line."]
},
{
code: `<template>
<component
v-bind="user"
></component>
</template>`,
output: `<template>
<component v-bind="user"></component>
</template>`,
errors: ["'v-bind' should be on a single line."]
},
{
code: `<template>
<component
@buy="buyProduct"
></component>
</template>`,
output: `<template>
<component @buy="buyProduct"></component>
</template>`,
errors: ["'@buy' should be on a single line."]
},
{
code: `<template>
<component
@click.stop
></component>
</template>`,
output: `<template>
<component @click.stop></component>
</template>`,
errors: ["'@click.stop' should be on a single line."]
},
{
code: `<template>
<component
v-if="something"
></component>
</template>`,
output: `<template>
<component v-if="something"></component>
</template>`,
errors: ["'v-if' should be on a single line."]
},
{
code: `<template>
<component
v-bind:name="user.name"
></component>
</template>`,
output: `<template>
<component v-bind:name="user.name"></component>
</template>`,
errors: ["'v-bind:name' should be on a single line."]
}
]
})