Skip to content

Add ignoreWhenEmpty options to singleline/multiline-html-element-content-newline #684

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 3 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions docs/rules/multiline-html-element-content-newline.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ This rule enforces a line break before and after the contents of a multiline ele
```json
{
"vue/multiline-html-element-content-newline": ["error", {
"ignoreWhenEmpty": true,
"ignores": ["pre", "textarea"]
}]
}
```

- `ignoreWhenEmpty` ... disables reporting when element has no content.
default `true`
- `ignores` ... the configuration for element names to ignore line breaks style.
default `["pre", "textarea"]`

Expand Down
3 changes: 3 additions & 0 deletions docs/rules/singleline-html-element-content-newline.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ This rule enforces a line break before and after the contents of a singleline el
{
"vue/singleline-html-element-content-newline": ["error", {
"ignoreWhenNoAttributes": true,
"ignoreWhenEmpty": true,
"ignores": ["pre", "textarea"]
}]
}
```

- `ignoreWhenNoAttributes` ... allows having contents in one line, when given element has no attributes.
default `true`
- `ignoreWhenEmpty` ... disables reporting when element has no content.
default `true`
- `ignores` ... the configuration for element names to ignore line breaks style.
default `["pre", "textarea"]`

Expand Down
20 changes: 17 additions & 3 deletions lib/rules/multiline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function isMultilineElement (element) {

function parseOptions (options) {
return Object.assign({
ignores: ['pre', 'textarea']
ignores: ['pre', 'textarea'],
ignoreWhenEmpty: true
}, options)
}

Expand Down Expand Up @@ -60,7 +61,10 @@ module.exports = {
schema: [{
type: 'object',
properties: {
'ignores': {
ignoreWhenEmpty: {
type: 'boolean'
},
ignores: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
Expand All @@ -76,7 +80,9 @@ module.exports = {
},

create (context) {
const ignores = parseOptions(context.options[0]).ignores
const options = parseOptions(context.options[0])
const ignores = options.ignores
const ignoreWhenEmpty = options.ignoreWhenEmpty
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
const sourceCode = context.getSourceCode()

Expand Down Expand Up @@ -108,6 +114,14 @@ module.exports = {
}

const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' }
if (
ignoreWhenEmpty &&
node.children.length === 0 &&
template.getFirstTokensBetween(node.startTag, node.endTag, getTokenOption).length === 0
) {
return
}

const contentFirst = template.getTokenAfter(node.startTag, getTokenOption)
const contentLast = template.getTokenBefore(node.endTag, getTokenOption)

Expand Down
15 changes: 14 additions & 1 deletion lib/rules/singleline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function isSinglelineElement (element) {
function parseOptions (options) {
return Object.assign({
ignores: ['pre', 'textarea'],
ignoreWhenNoAttributes: true
ignoreWhenNoAttributes: true,
ignoreWhenEmpty: true
}, options)
}

Expand Down Expand Up @@ -58,6 +59,9 @@ module.exports = {
ignoreWhenNoAttributes: {
type: 'boolean'
},
ignoreWhenEmpty: {
type: 'boolean'
},
ignores: {
type: 'array',
items: { type: 'string' },
Expand All @@ -77,6 +81,7 @@ module.exports = {
const options = parseOptions(context.options[0])
const ignores = options.ignores
const ignoreWhenNoAttributes = options.ignoreWhenNoAttributes
const ignoreWhenEmpty = options.ignoreWhenEmpty
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
const sourceCode = context.getSourceCode()

Expand Down Expand Up @@ -111,6 +116,14 @@ module.exports = {
}

const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' }
if (
ignoreWhenEmpty &&
node.children.length === 0 &&
template.getFirstTokensBetween(node.startTag, node.endTag, getTokenOption).length === 0
) {
return
}

const contentFirst = template.getTokenAfter(node.startTag, getTokenOption)
const contentLast = template.getTokenBefore(node.endTag, getTokenOption)

Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/multiline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ tester.run('multiline-html-element-content-newline', rule, {
`<template><div class="panel">content</div></template>`,
`<template><div class="panel"><div></div></div></template>`,
`<template><div class="panel"><!-- comment --></div></template>`,
`
<template>
<slot
name="panel"
></slot>
</template>
`,
`
<template>
<div
></div>
</template>
`,
`
<template>
<div class="panel">
Expand Down Expand Up @@ -525,6 +538,7 @@ content
</div>
</template>
`,
options: [{ ignoreWhenEmpty: false }],
errors: ['Expected 1 line break after opening tag (`<div>`), but no line breaks found.']
}
]
Expand Down
10 changes: 8 additions & 2 deletions tests/lib/rules/singleline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const tester = new RuleTester({

tester.run('singleline-html-element-content-newline', rule, {
valid: [
`
<template>
<slot name="panel"></slot>
</template>
`,
`
<template>
<div class="panel">
Expand Down Expand Up @@ -319,6 +324,7 @@ content
</div>
</template>
`,
options: [{ ignoreWhenEmpty: false }],
errors: [
'Expected 1 line break after opening tag (`<div>`), but no line breaks found.'
]
Expand Down Expand Up @@ -429,7 +435,7 @@ singleline element
<div></div>
</template>
`,
options: [{ ignoreWhenNoAttributes: false }],
options: [{ ignoreWhenEmpty: false, ignoreWhenNoAttributes: false }],
output: `
<template>
<div>
Expand All @@ -446,7 +452,7 @@ singleline element
<div> </div>
</template>
`,
options: [{ ignoreWhenNoAttributes: false }],
options: [{ ignoreWhenEmpty: false, ignoreWhenNoAttributes: false }],
output: `
<template>
<div>
Expand Down