Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update: Add allowEmptyLines option to vue/multiline-html-element-content-newline rule #759

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 4 commits into from
Jan 26, 2019
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
35 changes: 34 additions & 1 deletion docs/rules/multiline-html-element-content-newline.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ This rule enforces a line break before and after the contents of a multiline ele
{
"vue/multiline-html-element-content-newline": ["error", {
"ignoreWhenEmpty": true,
"ignores": ["pre", "textarea"]
"ignores": ["pre", "textarea"],
"allowEmptyLines": false
}]
}
```
Expand All @@ -87,6 +88,8 @@ This rule enforces a line break before and after the contents of a multiline ele
default `true`
- `ignores` ... the configuration for element names to ignore line breaks style.
default `["pre", "textarea"]`
- `allowEmptyLines` ... if `true`, it allows empty lines around content. If you want to disallow multiple empty lines, use [no-multiple-empty-lines] in combination.
default `false`

### `"ignores": ["VueComponent", "pre", "textarea"]`

Expand All @@ -109,6 +112,36 @@ This rule enforces a line break before and after the contents of a multiline ele

</eslint-code-block>

### `"allowEmptyLines": true`

<eslint-code-block fix :rules="{'vue/multiline-html-element-content-newline': ['error', { allowEmptyLines: true }]}">

```vue
<template>
<!-- ✓ GOOD -->
<div>
content
</div>
<div>

content

</div>

<!-- ✗ BAD -->
<div>content
content</div>
</template>
```

</eslint-code-block>

## :books: Further reading

- [no-multiple-empty-lines]

[no-multiple-empty-lines]: https://eslint.org/docs/rules/no-multiple-empty-lines

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/multiline-html-element-content-newline.js)
Expand Down
19 changes: 16 additions & 3 deletions lib/rules/multiline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function isMultilineElement (element) {
function parseOptions (options) {
return Object.assign({
ignores: ['pre', 'textarea'],
ignoreWhenEmpty: true
ignoreWhenEmpty: true,
allowEmptyLines: false
}, options)
}

Expand Down Expand Up @@ -69,6 +70,9 @@ module.exports = {
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
},
allowEmptyLines: {
type: 'boolean'
}
},
additionalProperties: false
Expand All @@ -83,6 +87,7 @@ module.exports = {
const options = parseOptions(context.options[0])
const ignores = options.ignores
const ignoreWhenEmpty = options.ignoreWhenEmpty
const allowEmptyLines = options.allowEmptyLines
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
const sourceCode = context.getSourceCode()

Expand All @@ -94,6 +99,14 @@ module.exports = {
ignores.includes(casing.kebabCase(node.rawName))
}

function isInvalidLineBreaks (lineBreaks) {
if (allowEmptyLines) {
return lineBreaks === 0
} else {
return lineBreaks !== 1
}
}

return utils.defineTemplateBodyVisitor(context, {
'VElement' (node) {
if (inIgnoreElement) {
Expand Down Expand Up @@ -127,7 +140,7 @@ module.exports = {

const beforeLineBreaks = contentFirst.loc.start.line - node.startTag.loc.end.line
const afterLineBreaks = node.endTag.loc.start.line - contentLast.loc.end.line
if (beforeLineBreaks !== 1) {
if (isInvalidLineBreaks(beforeLineBreaks)) {
context.report({
node: template.getLastToken(node.startTag),
loc: {
Expand All @@ -150,7 +163,7 @@ module.exports = {
return
}

if (afterLineBreaks !== 1) {
if (isInvalidLineBreaks(afterLineBreaks)) {
context.report({
node: template.getFirstToken(node.endTag),
loc: {
Expand Down
96 changes: 96 additions & 0 deletions tests/lib/rules/multiline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ tester.run('multiline-html-element-content-newline', rule, {
class="panel">
</div>
</template>`,
// allowEmptyLines
{
code: `
<template>
<div
class="panel">

</div>
</template>`,
options: [{ allowEmptyLines: true, ignoreWhenEmpty: false }]
},
{
code: `
<template>
<div
class="panel">

contents

</div>
</template>`,
options: [{ allowEmptyLines: true }]
},
{
code: `
<template>
<div
class="panel">


contents


</div>
</template>`,
options: [{ allowEmptyLines: true }]
},
// self closing
`
<template>
Expand Down Expand Up @@ -439,6 +476,65 @@ content
'Expected 1 line break before closing tag (`</div>`), but 2 line breaks found.'
]
},
// allowEmptyLines
{
code: `
<template>
<div
class="panel">

</div>
<div
class="panel"></div>
</template>`,
options: [{ allowEmptyLines: true, ignoreWhenEmpty: false }],
output: `
<template>
<div
class="panel">

</div>
<div
class="panel">
</div>
</template>`,
errors: [
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.'
]
},
{
code: `
<template>
<div>

content
content

</div>
<div>content
content</div>
</template>
`,
options: [{ allowEmptyLines: true }],
output: `
<template>
<div>

content
content

</div>
<div>
content
content
</div>
</template>
`,
errors: [
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.',
'Expected 1 line break before closing tag (`</div>`), but no line breaks found.'
]
},
// mustache
{
code: `
Expand Down