-
-
Notifications
You must be signed in to change notification settings - Fork 681
[New] Add vue/multiline-html-element-content-newline
rule
#551
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
michalsnik
merged 3 commits into
master
from
add-rule/multiline-html-element-content-newline
Aug 13, 2018
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# require a line break before and after the contents of a multiline element (vue/multiline-html-element-content-newline) | ||
|
||
- :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. | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces a line break before and after the contents of a multiline element. | ||
|
||
|
||
:-1: Examples of **incorrect** code: | ||
|
||
```html | ||
<div>multiline | ||
content</div> | ||
|
||
<div | ||
attr | ||
>multiline start tag</div> | ||
|
||
<tr><td>multiline</td> | ||
<td>children</td></tr> | ||
|
||
<div><!-- multiline | ||
comment --></div> | ||
|
||
<div | ||
></div> | ||
``` | ||
|
||
:+1: Examples of **correct** code: | ||
|
||
```html | ||
<div> | ||
multiline | ||
content | ||
</div> | ||
|
||
<div | ||
attr | ||
> | ||
multiline start tag | ||
</div> | ||
|
||
<tr> | ||
<td>multiline</td> | ||
<td>children</td> | ||
</tr> | ||
|
||
<div> | ||
<!-- multiline | ||
comment --> | ||
</div> | ||
|
||
<div | ||
> | ||
</div> | ||
|
||
<div attr>singleline element</div> | ||
``` | ||
|
||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/multiline-html-element-content-newline": ["error", { | ||
"ignoreNames": ["pre", "textarea"] | ||
}] | ||
} | ||
``` | ||
|
||
- `ignoreNames` ... the configuration for element names to ignore line breaks style. | ||
default `["pre", "textarea"]` | ||
|
||
|
||
:+1: Examples of **correct** code: | ||
|
||
```html | ||
/* eslint vue/multiline-html-element-content-newline: ["error", { "ignoreNames": ["VueComponent", "pre", "textarea"]}] */ | ||
|
||
<VueComponent>multiline | ||
content</VueComponent> | ||
|
||
<VueComponent><span | ||
class="bold">For example,</span> | ||
Defines the Vue component that accepts preformatted text.</VueComponent> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
function isMultilineElement (element) { | ||
return element.loc.start.line < element.endTag.loc.start.line | ||
} | ||
|
||
function parseOptions (options) { | ||
return Object.assign({ | ||
'ignoreNames': ['pre', 'textarea'] | ||
}, options) | ||
} | ||
|
||
function getPhrase (lineBreaks) { | ||
switch (lineBreaks) { | ||
case 0: return 'no' | ||
default: return `${lineBreaks}` | ||
} | ||
} | ||
/** | ||
* Check whether the given element is empty or not. | ||
* This ignores whitespaces, doesn't ignore comments. | ||
* @param {VElement} node The element node to check. | ||
* @param {SourceCode} sourceCode The source code object of the current context. | ||
* @returns {boolean} `true` if the element is empty. | ||
*/ | ||
function isEmpty (node, sourceCode) { | ||
const start = node.startTag.range[1] | ||
const end = node.endTag.range[0] | ||
return sourceCode.text.slice(start, end).trim() === '' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'require a line break before and after the contents of a multiline element', | ||
category: undefined, | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.2/docs/rules/multiline-html-element-content-newline.md' | ||
}, | ||
fixable: 'whitespace', | ||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
'ignoreNames': { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
uniqueItems: true, | ||
additionalItems: false | ||
} | ||
}, | ||
additionalProperties: false | ||
}], | ||
messages: { | ||
unexpectedAfterClosingBracket: `Expected 1 line break after closing bracket of the "{{name}}" element, but {{actual}} line breaks found.`, | ||
unexpectedBeforeOpeningBracket: `Expected 1 line break before opening bracket of the "{{name}}" element, but {{actual}} line breaks found.` | ||
} | ||
}, | ||
|
||
create (context) { | ||
const ignoreNames = parseOptions(context.options[0]).ignoreNames | ||
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() | ||
const sourceCode = context.getSourceCode() | ||
|
||
let inIgnoreElement | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
'VElement' (node) { | ||
if (inIgnoreElement) { | ||
return | ||
} | ||
if (ignoreNames.indexOf(node.name) >= 0) { | ||
// ignore element name | ||
inIgnoreElement = node | ||
return | ||
} | ||
if (node.startTag.selfClosing || !node.endTag) { | ||
// self closing | ||
return | ||
} | ||
|
||
if (!isMultilineElement(node)) { | ||
return | ||
} | ||
|
||
const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' } | ||
const contentFirst = template.getTokenAfter(node.startTag, getTokenOption) | ||
const contentLast = template.getTokenBefore(node.endTag, getTokenOption) | ||
|
||
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) { | ||
context.report({ | ||
node: template.getLastToken(node.startTag), | ||
loc: { | ||
start: node.startTag.loc.end, | ||
end: contentFirst.loc.start | ||
}, | ||
messageId: 'unexpectedAfterClosingBracket', | ||
data: { | ||
name: node.name, | ||
actual: getPhrase(beforeLineBreaks) | ||
}, | ||
fix (fixer) { | ||
const range = [node.startTag.range[1], contentFirst.range[0]] | ||
return fixer.replaceTextRange(range, '\n') | ||
} | ||
}) | ||
} | ||
|
||
if (isEmpty(node, sourceCode)) { | ||
return | ||
} | ||
|
||
if (afterLineBreaks !== 1) { | ||
context.report({ | ||
node: template.getFirstToken(node.endTag), | ||
loc: { | ||
start: contentLast.loc.end, | ||
end: node.endTag.loc.start | ||
}, | ||
messageId: 'unexpectedBeforeOpeningBracket', | ||
data: { | ||
name: node.name, | ||
actual: getPhrase(afterLineBreaks) | ||
}, | ||
fix (fixer) { | ||
const range = [contentLast.range[1], node.endTag.range[0]] | ||
return fixer.replaceTextRange(range, '\n') | ||
} | ||
}) | ||
} | ||
}, | ||
'VElement:exit' (node) { | ||
if (inIgnoreElement === node) { | ||
inIgnoreElement = null | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Perhaps we should stick to the convention and use simple
ignore
property? I don't have strong preference thoughThere 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 for review!
Name
was unnecessary.Since the
indent
rule is namedignores
, we also name itignores
.