Skip to content

Add mustache-curly-spacing rule. #153

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 2 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 67 additions & 0 deletions docs/rules/mustache-interpolation-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# enforce unified spacing in mustache interpolations. (mustache-interpolation-spacing)

- :wrench: The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

This rule aims to enforce unified spacing in mustache interpolations.

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

```html
<template>
<div>{{ text }}</div>
</template>
```

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

```html
<template>
<div>{{ text }}</div>
</template>
```

## :wrench: Options

Default spacing is set to `always`

```
'vue/mustache-interpolation-spacing': [2, 'always'|'never']
```

### `"always"` - Expect one space between expression and curly brackets.

:+1: Examples of **correct** code`:

```html
<template>
<div>{{ text }}</div>
</template>
```

:-1: Examples of **incorrect** code`:

```html
<template>
<div>{{text}}</div>
</template>
```

### `"never"` - Expect no spaces between expression and curly brackets.

:+1: Examples of **correct** code`:

```html
<template>
<div>{{text}}</div>
</template>
```

:-1: Examples of **incorrect** code`:

```html
<template>
<div>{{ text }}</div>
</template>
```
91 changes: 91 additions & 0 deletions lib/rules/mustache-interpolation-spacing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @fileoverview enforce unified spacing in mustache interpolations.
* @author Armano
*/
'use strict'

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

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

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

module.exports = {
meta: {
docs: {
description: 'enforce unified spacing in mustache interpolations.',
category: 'Stylistic Issues',
recommended: false
},
fixable: 'whitespace',
schema: [
{
enum: ['always', 'never']
}
]
},

create (context) {
const options = context.options[0]
const optSpaces = options !== 'never'
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------

function checkTokens (leftToken, rightToken) {
if (leftToken.loc.end.line === rightToken.loc.start.line) {
const spaces = rightToken.loc.start.column - leftToken.loc.end.column
const noSpacesFound = spaces === 0

if (optSpaces === noSpacesFound) {
context.report({
node: rightToken,
loc: {
start: leftToken.loc.end,
end: rightToken.loc.start
},
message: 'Found {{spaces}} whitespaces, {{type}} expected.',
data: {
spaces: spaces === 0 ? 'none' : spaces,
type: optSpaces ? '1' : 'none'
},
fix: (fixer) => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], optSpaces ? ' ' : '')
})
}
}
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

utils.registerTemplateBodyVisitor(context, {
'VExpressionContainer[expression!=null]' (node) {
const tokens = template.getTokens(node, {
includeComments: true,
filter: token => token.type !== 'HTMLWhitespace' // When there is only whitespace between ignore it
})

const startToken = tokens.shift()
if (!startToken || startToken.type !== 'VExpressionStart') return
const endToken = tokens.pop()
if (!endToken || endToken.type !== 'VExpressionEnd') return

if (tokens.length > 0) {
checkTokens(startToken, tokens[0])
checkTokens(tokens[tokens.length - 1], endToken)
} else {
checkTokens(startToken, endToken)
}
}
})

return { }
}
}
168 changes: 168 additions & 0 deletions tests/lib/rules/mustache-interpolation-spacing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* @fileoverview enforce unified spacing in mustache interpolations.
* @author Armano
*/
'use strict'

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

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

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

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

ruleTester.run('mustache-interpolation-spacing', rule, {

valid: [
{
filename: 'test.vue',
code: '<template></template>'
},
{
filename: 'test.vue',
code: '<template><div></div></template>'
},
{
filename: 'test.vue',
code: '<template> <div id=" "></div> </template>'
},
{
filename: 'test.vue',
code: '<template> <div :style=" " :class=" foo " v-if=foo ></div> </template>'
},
{
filename: 'test.vue',
code: '<template><div>{{ text }}</div></template>'
},
{
filename: 'test.vue',
code: '<template><div>{{ }}</div></template>'
},
{
filename: 'test.vue',
code: '<template><div>{{ }}</div></template>',
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div>{{}}</div></template>',
options: ['never']
},
{
filename: 'test.vue',
code: '<template><div>{{text}}</div></template>',
options: ['never']
},
{
filename: 'test.vue',
code: '<template><div>{{ text }}</div></template>',
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div>{{ }}</div></template>',
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div>{{ }}</div></template>',
options: ['never']
},
{
filename: 'test.vue',
code: '<template><div>{{ text }}</div></template>',
options: ['always']
}
],

invalid: [
{
filename: 'test.vue',
code: '<template><div>{{ text}}</div></template>',
output: '<template><div>{{ text }}</div></template>',
options: ['always'],
errors: [{
message: 'Found none whitespaces, 1 expected.',
type: 'VExpressionEnd'
}]
},
{
filename: 'test.vue',
code: '<template><div>{{text }}</div></template>',
output: '<template><div>{{ text }}</div></template>',
options: ['always'],
errors: [{
message: 'Found none whitespaces, 1 expected.',
type: 'Identifier'
}]
},
{
filename: 'test.vue',
code: '<template><div>{{ text}}</div></template>',
output: '<template><div>{{text}}</div></template>',
options: ['never'],
errors: [{
message: 'Found 1 whitespaces, none expected.',
type: 'Identifier'
}]
},
{
filename: 'test.vue',
code: '<template><div>{{text }}</div></template>',
output: '<template><div>{{text}}</div></template>',
options: ['never'],
errors: [{
message: 'Found 1 whitespaces, none expected.',
type: 'VExpressionEnd'
}]
},
{
filename: 'test.vue',
code: '<template><div>{{text}}</div></template>',
output: '<template><div>{{ text }}</div></template>',
options: ['always'],
errors: [{
message: 'Found none whitespaces, 1 expected.',
type: 'Identifier'
}, {
message: 'Found none whitespaces, 1 expected.',
type: 'VExpressionEnd'
}]
},
{
filename: 'test.vue',
code: '<template><div>{{ text }}</div></template>',
output: '<template><div>{{text}}</div></template>',
options: ['never'],
errors: [{
message: 'Found 1 whitespaces, none expected.',
type: 'Identifier'
}, {
message: 'Found 1 whitespaces, none expected.',
type: 'VExpressionEnd'
}]
},
{
filename: 'test.vue',
code: '<template><div>{{ text }}</div></template>',
output: '<template><div>{{text}}</div></template>',
options: ['never'],
errors: [{
message: 'Found 3 whitespaces, none expected.',
type: 'Identifier'
}, {
message: 'Found 3 whitespaces, none expected.',
type: 'VExpressionEnd'
}]
}
]
})