-
-
Notifications
You must be signed in to change notification settings - Fork 699
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
Add "no-multi-spaces" rule (fixes #133) #138
Conversation
vue/no-extra-whitespaces
|
@mysticatea is there a way that we can have access to for now i had to parse entire This is going to be needed for |
vue/no-extra-whitespaces|
I think that this rule should be token-based. return {
Program(node) {
// TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not.
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
const tokens = tokenStore.getTokens(node.templateBody, {includeComments: true})
let prevToken = tokens.shift()
for (const token of tokens) {
const onSameLine = (prevToken.loc.end.line === token.loc.start.line)
const spaces = token.loc.start.column - prevToken.loc.end.column
if (onSameLine && spaces >= 2) {
// Report.
}
prevToken = token
}
}
} |
|
Spaces which don't change semantic are on outside of tokens, so we can check only extra spaces in this way. |
|
@mysticatea thank you for suggestion, i'm still learning how to work with eslint rules 😄 |
|
@mysticatea i took your sugestion and i applied it to my code also i added basic doc for it. i think we should change error message: |
|
i consider we can add parameter to for this but i'm not sure if this should be in this rule |
4185212 to
ce8f6d1
Compare
|
@armano2 I think better if this rule focuses on only the sequences of multiple spaces. In core rule case, no-multi-spaces is doing that, and for example, space-before-function-parentheses focuses on one or zero spaces which are followed by the parenthesis of function parameters. |
lib/rules/html-no-self-closing.js
Outdated
| return | ||
| } | ||
|
|
||
| // TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not. |
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.
This is inside of registerTemplateBodyVisitor, so context.parserServices.getTemplateBodyTokenStore should always exist.
lib/rules/no-multi-spaces.js
Outdated
|
|
||
| let prevToken = tokens.shift() | ||
| for (const token of tokens) { | ||
| if (sourceCode.isSpaceBetweenTokens(prevToken, token)) { |
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.
This method cannot be used here because this method has special behavior that it ignores /* */ style comments which are not comments in HTML context.
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.
ok
| type: 'HTMLIdentifier' | ||
| } | ||
| ] | ||
| } |
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.
I'd like to see tests for the expression of Vue.js directives and mustaches. Those are parsed by script parser (espree by default), so those have valid tokens. This rule can report on multiple spaces which are in the expressions.
For reference, this is the message of core |
lib/rules/no-multi-spaces.js
Outdated
| } | ||
| }, | ||
| message: 'Extra whitespace detected.', | ||
| fix: (fixer) => fixer.removeRange([prevToken.range[1] + requiredSpaces, prevToken.range[1] + spaces]) |
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.
This can be simpler a bit. It's "put a space between the previous token and the current token". Original spaces may be \t.
- fix: (fixer) => fixer.removeRange([prevToken.range[1] + requiredSpaces, prevToken.range[1] + spaces])
+ fix: (fixer) => fixer.replaceTextRange([prevToken.range[1], token.range[0]], ' ')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.
in this case:
fix: (fixer) => fixer.replaceTextRange([prevToken.range[1], prevToken.range[1] + spaces], ' '),
is going to be valid cus there can be \n\r symbold before space
|
@armano2 Please use |
|
@mysticatea thank you for suggestions, i applied most of them except that i changed a little fixer: fix: (fixer) => fixer.replaceTextRange(
[prevToken.range[1], prevToken.range[1] + spaces],
requiredSpaces ? ' ' : ''
) |
|
In core, |
|
@mysticatea than its ok now? |
|
I think better if this rule focuses on only the sequences of multiple spaces between tokens. Could you remove the following things?:
|
|
@mysticatea ok changes applied first time i didn't understand you |
|
also idk if this is bug in parser but i like |
bb20abd to
876fabf
Compare
876fabf to
014110b
Compare
|
LGTM, thank you for the great work, @armano2 !
It's intentional. Those kinds of tokens don't have variation, so their |
lib/rules/no-multi-spaces.js
Outdated
| case 'HTMLTagClose': return '>' | ||
| } | ||
|
|
||
| return token.value |
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 of HTMLAssociation 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
|
@mysticatea requested changes applied |
mysticatea
left a comment
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!
This PR implement rule proposed in #133