-
-
Notifications
You must be signed in to change notification settings - Fork 681
⭐️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
michalsnik
merged 4 commits into
vuejs:master
from
ota-meshi:dev/no-spaces-around-equal-signs
Aug 13, 2018
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,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) |
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,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
128
tests/lib/rules/no-spaces-around-equal-signs-in-attribute.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
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: | ||
|
||
`<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.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.' | ||
] | ||
} | ||
] | ||
}) |
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.
Redundant empty line