Skip to content

Commit

Permalink
⭐️New: Add vue/keyword-spacing rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Feb 1, 2019
1 parent a8b2ca0 commit 7a9730b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -148,6 +148,7 @@ For example:
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
| [vue/eqeqeq](./eqeqeq.md) | require the use of `===` and `!==` | :wrench: |
| [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |
| [vue/keyword-spacing](./keyword-spacing.md) | enforce consistent spacing before and after keywords | :wrench: |
| [vue/match-component-file-name](./match-component-file-name.md) | require component name property to match its file name | |
| [vue/no-restricted-syntax](./no-restricted-syntax.md) | disallow specified syntax | |
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |
Expand Down
23 changes: 23 additions & 0 deletions docs/rules/keyword-spacing.md
@@ -0,0 +1,23 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/keyword-spacing
description: enforce consistent spacing before and after keywords
---
# vue/keyword-spacing
> enforce consistent spacing before and after keywords
- :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 is the same rule as core [keyword-spacing] rule but it applies to the expressions in `<template>`.

## :books: Further reading

- [keyword-spacing]

[keyword-spacing]: https://eslint.org/docs/rules/keyword-spacing

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/keyword-spacing.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/keyword-spacing.js)
1 change: 1 addition & 0 deletions lib/configs/no-layout-rules.js
Expand Up @@ -16,6 +16,7 @@ module.exports = {
'vue/html-quotes': 'off',
'vue/html-self-closing': 'off',
'vue/key-spacing': 'off',
'vue/keyword-spacing': 'off',
'vue/max-attributes-per-line': 'off',
'vue/multiline-html-element-content-newline': 'off',
'vue/mustache-interpolation-spacing': 'off',
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -26,6 +26,7 @@ module.exports = {
'html-self-closing': require('./rules/html-self-closing'),
'jsx-uses-vars': require('./rules/jsx-uses-vars'),
'key-spacing': require('./rules/key-spacing'),
'keyword-spacing': require('./rules/keyword-spacing'),
'match-component-file-name': require('./rules/match-component-file-name'),
'max-attributes-per-line': require('./rules/max-attributes-per-line'),
'multiline-html-element-content-newline': require('./rules/multiline-html-element-content-newline'),
Expand Down
9 changes: 9 additions & 0 deletions lib/rules/keyword-spacing.js
@@ -0,0 +1,9 @@
/**
* @author Yosuke Ota
*/
'use strict'

const { wrapCoreRule } = require('../utils')

// eslint-disable-next-line
module.exports = wrapCoreRule(require('eslint/lib/rules/keyword-spacing'))
142 changes: 142 additions & 0 deletions tests/lib/rules/keyword-spacing.js
@@ -0,0 +1,142 @@
/**
* @author Yosuke Ota
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/keyword-spacing')

const tester = new RuleTester({
parser: 'vue-eslint-parser',
parserOptions: { ecmaVersion: 2015 }
})

tester.run('keyword-spacing', rule, {
valid: [
`<template>
<div @event="
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
" />
</template>`,
{
code:
`<template>
<div @event="
if(foo) {
//...
}else if(bar) {
//...
}else{
//...
}
" />
</template>`,
options: [{ before: false, after: false }]
}
],
invalid: [
{
code:
`<template>
<div @event="
if(foo) {
//...
}else if(bar) {
//...
}else{
//...
}
" />
</template>`,
output:
`<template>
<div @event="
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
" />
</template>`,
errors: [
{
message: 'Expected space(s) after "if".',
line: 3
},
{
message: 'Expected space(s) before "else".',
line: 5
},
{
message: 'Expected space(s) after "if".',
line: 5
},
{
message: 'Expected space(s) before "else".',
line: 7
},
{
message: 'Expected space(s) after "else".',
line: 7
}
]
},
{
code:
`<template>
<div @event="
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
" />
</template>`,
options: [{ before: false, after: false }],
output:
`<template>
<div @event="
if(foo) {
//...
}else if(bar) {
//...
}else{
//...
}
" />
</template>`,
errors: [
{
message: 'Unexpected space(s) after "if".',
line: 3
},
{
message: 'Unexpected space(s) before "else".',
line: 5
},
{
message: 'Unexpected space(s) after "if".',
line: 5
},
{
message: 'Unexpected space(s) before "else".',
line: 7
},
{
message: 'Unexpected space(s) after "else".',
line: 7
}
]
}
]
})

0 comments on commit 7a9730b

Please sign in to comment.