Skip to content

⭐️New: Add vue/no-spaces-around-equal-signs-in-attribute #542

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
merged 4 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
| | Rule ID | Description |
|:---|:--------|:------------|
| :wrench: | [vue/component-name-in-template-casing](./docs/rules/component-name-in-template-casing.md) | enforce specific casing for the component naming style in template |
| :wrench: | [vue/no-spaces-around-equal-signs-in-attribute](./docs/rules/no-spaces-around-equal-signs-in-attribute.md) | disallow spaces around equal signs in attribute |
| :wrench: | [vue/script-indent](./docs/rules/script-indent.md) | enforce consistent indentation in `<script>` |

### Deprecated
Expand Down
25 changes: 25 additions & 0 deletions docs/rules/no-spaces-around-equal-signs-in-attribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# disallow spaces around equal signs in attribute (vue/no-spaces-around-equal-signs-in-attribute)

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

This rule disallow spaces around equal signs in attribute.

HTML5 allows spaces around equal signs. But space-less is easier to read, and groups entities better together.

## :book: Rule Details

:-1: Examples of **incorrect** code for this rule:

```html
<div class = "item">
```

:+1: Examples of **correct** code for this rule:

```html
<div class="item">
```

## Further Reading

* [HTML5 Style Guide - W3Schools *Spaces and Equal Signs*](https://www.w3schools.com/html/html5_syntax.asp)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
'no-reserved-keys': require('./rules/no-reserved-keys'),
'no-shared-component-data': require('./rules/no-shared-component-data'),
'no-side-effects-in-computed-properties': require('./rules/no-side-effects-in-computed-properties'),
'no-spaces-around-equal-signs-in-attribute': require('./rules/no-spaces-around-equal-signs-in-attribute'),
'no-template-key': require('./rules/no-template-key'),
'no-template-shadow': require('./rules/no-template-shadow'),
'no-textarea-mustache': require('./rules/no-textarea-mustache'),
Expand Down
54 changes: 54 additions & 0 deletions lib/rules/no-spaces-around-equal-signs-in-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @author Yosuke Ota
* issue https://github.com/vuejs/eslint-plugin-vue/issues/460
*/
'use strict'

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

const utils = require('../utils')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'disallow spaces around equal signs in attribute',
category: undefined,
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.1/docs/rules/no-spaces-around-equal-signs-in-attribute.md'
},
fixable: 'whitespace',
schema: []
},

create (context) {
const sourceCode = context.getSourceCode()
return utils.defineTemplateBodyVisitor(context, {
'VAttribute' (node) {
if (!node.value) {
return
}
const range = [node.key.range[1], node.value.range[0]]
const eqText = sourceCode.text.slice(range[0], range[1])
const expect = eqText.trim()

if (eqText !== expect) {
context.report({
node: node.key,
loc: {
start: node.key.loc.end,
end: node.value.loc.start
},
message: 'Unexpected spaces found around equal signs.',
data: {},
fix: fixer => fixer.replaceTextRange(range, expect)
})
}
}
})
}
}
128 changes: 128 additions & 0 deletions tests/lib/rules/no-spaces-around-equal-signs-in-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* @author Yosuke Ota
*/
'use strict'

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

const rule = require('../../../lib/rules/no-spaces-around-equal-signs-in-attribute')
const RuleTester = require('eslint').RuleTester

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

const tester = new RuleTester({
parser: 'vue-eslint-parser'
})

tester.run('no-spaces-around-equal-signs-in-attribute', rule, {
valid: [
'<template><div attr="value" /></template>',
'<template><div attr="" /></template>',
'<template><div attr=\'value\' /></template>',
'<template><div attr=value /></template>',
'<template><div attr /></template>',
'<template><div/></template>',
`<template>
<div
is="header"
v-for="item in items"
v-if="!visible"
v-once
id="uniqueID"
ref="header"
v-model="headerData"
myProp="prop"
@click="functionCall"
v-text="textContent">
</div>
</template>`
],
invalid: [
{
code: '<template><div attr = "value" /></template>',
output: '<template><div attr="value" /></template>',
errors: ['Unexpected spaces found around equal signs.']
},
{
code: '<template><div attr = "" /></template>',
output: '<template><div attr="" /></template>',
errors: ['Unexpected spaces found around equal signs.']
},
{
code: '<template><div attr = \'value\' /></template>',
output: '<template><div attr=\'value\' /></template>',
errors: ['Unexpected spaces found around equal signs.']
},
{
code: '<template><div attr = value /></template>',
output: '<template><div attr=value /></template>',
errors: ['Unexpected spaces found around equal signs.']
},

{
code: '<template><div attr \t\n = \t\n "value" /></template>',
output: '<template><div attr="value" /></template>',
errors: ['Unexpected spaces found around equal signs.']
},
{
code: '<template><div attr ="value" /></template>',
output: '<template><div attr="value" /></template>',
errors: ['Unexpected spaces found around equal signs.']
},
{
code: '<template><div attr= "value" /></template>',
output: '<template><div attr="value" /></template>',
errors: ['Unexpected spaces found around equal signs.']
},

{
code:
`<template>
<div
is = "header"
v-for = "item in items"
v-if = "!visible"
v-once
id = "uniqueID"
ref = "header"
v-model = "headerData"
myProp = "prop"
@click = "functionCall"
v-text = "textContent">
</div>
</template>`,

output:

Copy link
Member

Choose a reason for hiding this comment

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

Redundant empty line

`<template>
<div
is="header"
v-for="item in items"
v-if="!visible"
v-once
id="uniqueID"
ref="header"
v-model="headerData"
myProp="prop"
@click="functionCall"
v-text="textContent">
</div>
</template>`,
errors: [
'Unexpected spaces found around equal signs.',
Copy link
Member

Choose a reason for hiding this comment

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

I see that you nicely report proper location in the rule. Perhaps it would be good to also make sure in tests that these reported locations are ok?

'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.',
'Unexpected spaces found around equal signs.'
]
}
]
})