-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add "no-multi-spaces" rule (fixes #133) #138
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
mysticatea
merged 2 commits into
vuejs:master
from
armano2:no-extra-space-between-attributes
Aug 14, 2017
Merged
Changes from 1 commit
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This rule warns about the usage of extra whitespaces between attributes (no-multi-spaces) | ||
|
||
The `--fix` option on the command line can automatically fix some of the problems reported by this rule. | ||
|
||
This rule aims to remove multiple spaces in a row between attributes witch are not used for indentation. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```html | ||
<template> | ||
<div class="foo" :style="foo" | ||
:foo="bar" > | ||
</div> | ||
</template> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```html | ||
<template> | ||
<div class="foo" | ||
:style="foo"> | ||
</div> | ||
</template> | ||
``` | ||
|
||
### Options | ||
|
||
Nothing |
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,74 @@ | ||
/** | ||
* @fileoverview This rule warns about the usage of extra whitespaces between attributes | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'This rule warns about the usage of extra whitespaces between attributes', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
}, | ||
fixable: 'whitespace', // or "code" or "whitespace" | ||
schema: [] | ||
}, | ||
|
||
/** | ||
* @param {RuleContext} context - The rule context. | ||
* @returns {Object} AST event handlers. | ||
*/ | ||
create (context) { | ||
function formatValue (token) { | ||
switch (token.type) { | ||
case 'HTMLSelfClosingTagClose': return '/>' | ||
case 'HTMLTagClose': return '>' | ||
} | ||
|
||
return token.value | ||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return { | ||
Program (node) { | ||
if (context.parserServices.getTemplateBodyTokenStore == null) { | ||
context.report({ | ||
loc: { line: 1, column: 0 }, | ||
message: 'Use the latest vue-eslint-parser. See also https://github.com/vuejs/eslint-plugin-vue#what-is-the-use-the-latest-vue-eslint-parser-error.' | ||
}) | ||
return | ||
} | ||
const tokenStore = context.parserServices.getTemplateBodyTokenStore() | ||
const tokens = tokenStore.getTokens(node.templateBody, { includeComments: true }) | ||
|
||
let prevToken = tokens.shift() | ||
for (const token of tokens) { | ||
const spaces = token.range[0] - prevToken.range[1] | ||
if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line) { | ||
context.report({ | ||
node: token, | ||
loc: { | ||
start: prevToken.loc.end, | ||
end: token.loc.start | ||
}, | ||
message: "Multiple spaces found before '{{displayValue}}'.", | ||
fix: (fixer) => fixer.replaceTextRange([prevToken.range[1], token.range[0]], ' '), | ||
data: { | ||
displayValue: formatValue(token) | ||
} | ||
}) | ||
} | ||
prevToken = token | ||
} | ||
} | ||
} | ||
} | ||
} |
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,177 @@ | ||
/** | ||
* @fileoverview This rule warns about the usage of extra whitespaces between attributes | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-multi-spaces') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
ruleTester.run('no-multi-spaces', rule, { | ||
valid: [ | ||
'', | ||
'<template></template>', | ||
'<template><div /></template>', | ||
'<template><div class="foo"></div></template>', | ||
'<template><div class=" foo " style=" foo "></div></template>', | ||
'<template><div class="foo" @click="bar"></div></template>', | ||
'<template><div class="foo"\n :style="foo"></div></template>', | ||
'<template><div class="foo"\n\t\t\t:style="foo"></div></template>', | ||
'<template><div class="foo"\n :style="foo"\n ></div></template>', | ||
'<template><div class="foo"\n :style="foo" /></template>', | ||
'<template><div class="foo"\n :style="foo"\n /></template>', | ||
'<template><div>{{ test }}</div></template>', | ||
'<template><div>{{test}}</div></template>', | ||
'<template><div>{{test}}<!-- fooo --></div></template>', | ||
'<template><div>{{test}} <!-- fooo --></div></template>', | ||
'<template><div v-for="i in b">{{ i }}</div></template>', | ||
'<template><div v-for=" i in b ">{{ i }}</div></template>', | ||
'<template><div :test="` `"> {{ a }} </div></template>', | ||
'<template><div :test="` `"> \n {{ a }} </div></template>' | ||
], | ||
invalid: [ | ||
{ | ||
code: '<template><div /></template>', | ||
output: '<template><div /></template>', | ||
errors: [{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
}] | ||
}, | ||
{ | ||
code: '<template><div class="foo" /></template>', | ||
output: '<template><div class="foo" /></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before 'class'.", | ||
type: 'HTMLIdentifier' | ||
}, | ||
{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><div\t\tclass="foo"\t\t/></template>', | ||
output: '<template><div class="foo" /></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before 'class'.", | ||
type: 'HTMLIdentifier' | ||
}, | ||
{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><div :class="foo" /></template>', | ||
output: '<template><div :class="foo" /></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before ':class'.", | ||
type: 'HTMLIdentifier' | ||
}, | ||
{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><div :foo="" class="foo" /></template>', | ||
output: '<template><div :foo="" class="foo" /></template>', | ||
errors: [{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
}] | ||
}, | ||
{ | ||
code: '<template><div foo="" class="foo" /></template>', | ||
output: '<template><div foo="" class="foo" /></template>', | ||
errors: [{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
}] | ||
}, | ||
{ | ||
code: '<template><foo v-foo="" class="foo" /></template>', | ||
output: '<template><foo v-foo="" class="foo" /></template>', | ||
errors: [{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
}] | ||
}, | ||
{ | ||
code: '<template><foo v-foo="" \n class="foo" /></template>', | ||
output: '<template><foo v-foo="" \n class="foo" /></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before '/>'.", | ||
type: 'HTMLSelfClosingTagClose' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><div>{{ test }}</div></template>', | ||
output: '<template><div>{{ test }}</div></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before 'test'.", | ||
type: 'Identifier' | ||
}, | ||
{ | ||
message: "Multiple spaces found before '}}'.", | ||
type: 'VExpressionEnd' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><div ></div></template>', | ||
output: '<template><div ></div></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before '>'.", | ||
type: 'HTMLTagClose' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><div v-for=" i in b ">{{ test }}</div></template>', | ||
output: '<template><div v-for=" i in b ">{{ test }}</div></template>', | ||
errors: [ | ||
{ | ||
message: "Multiple spaces found before 'i'.", | ||
type: 'Identifier' | ||
}, | ||
{ | ||
message: "Multiple spaces found before 'in'.", | ||
type: 'Keyword' | ||
}, | ||
{ | ||
message: "Multiple spaces found before 'b'.", | ||
type: 'Identifier' | ||
}, | ||
{ | ||
message: "Multiple spaces found before '\"'.", | ||
type: 'Punctuator' | ||
} | ||
] | ||
} | ||
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'd like to see tests for the expression of Vue.js directives and mustaches. Those are parsed by script parser ( |
||
] | ||
}) |
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.
Ah, also the
value
property ofHTMLAssociation
tokens is empty.I think
context.getSourceCode().getText(token)
is good for this purpose.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.
thank you i learned few new useful thinks while building this rule