diff --git a/docs/rules/multiline-html-element-content-newline.md b/docs/rules/multiline-html-element-content-newline.md index a1fd44dec..1e9357a78 100644 --- a/docs/rules/multiline-html-element-content-newline.md +++ b/docs/rules/multiline-html-element-content-newline.md @@ -74,12 +74,15 @@ This rule enforces a line break before and after the contents of a multiline ele ```json { - "vue/multiline-html-element-content-newline": ["error", { - "ignores": ["pre", "textarea"] - }] + "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"]` diff --git a/docs/rules/singleline-html-element-content-newline.md b/docs/rules/singleline-html-element-content-newline.md index dcb6de9a8..560e0e167 100644 --- a/docs/rules/singleline-html-element-content-newline.md +++ b/docs/rules/singleline-html-element-content-newline.md @@ -52,6 +52,7 @@ 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"] }] } @@ -59,6 +60,8 @@ This rule enforces a line break before and after the contents of a singleline el - `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"]` diff --git a/lib/rules/multiline-html-element-content-newline.js b/lib/rules/multiline-html-element-content-newline.js index 55bb1939b..c57e5d005 100644 --- a/lib/rules/multiline-html-element-content-newline.js +++ b/lib/rules/multiline-html-element-content-newline.js @@ -21,7 +21,8 @@ function isMultilineElement (element) { function parseOptions (options) { return Object.assign({ - ignores: ['pre', 'textarea'] + ignores: ['pre', 'textarea'], + ignoreWhenEmpty: true }, options) } @@ -60,7 +61,10 @@ module.exports = { schema: [{ type: 'object', properties: { - 'ignores': { + ignoreWhenEmpty: { + type: 'boolean' + }, + ignores: { type: 'array', items: { type: 'string' }, uniqueItems: true, @@ -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() @@ -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) diff --git a/lib/rules/singleline-html-element-content-newline.js b/lib/rules/singleline-html-element-content-newline.js index ae7e3e1b4..babf4f6d8 100644 --- a/lib/rules/singleline-html-element-content-newline.js +++ b/lib/rules/singleline-html-element-content-newline.js @@ -22,7 +22,8 @@ function isSinglelineElement (element) { function parseOptions (options) { return Object.assign({ ignores: ['pre', 'textarea'], - ignoreWhenNoAttributes: true + ignoreWhenNoAttributes: true, + ignoreWhenEmpty: true }, options) } @@ -58,6 +59,9 @@ module.exports = { ignoreWhenNoAttributes: { type: 'boolean' }, + ignoreWhenEmpty: { + type: 'boolean' + }, ignores: { type: 'array', items: { type: 'string' }, @@ -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() @@ -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) diff --git a/tests/lib/rules/multiline-html-element-content-newline.js b/tests/lib/rules/multiline-html-element-content-newline.js index e653218e9..b1eaa64c4 100644 --- a/tests/lib/rules/multiline-html-element-content-newline.js +++ b/tests/lib/rules/multiline-html-element-content-newline.js @@ -26,6 +26,19 @@ tester.run('multiline-html-element-content-newline', rule, { ``, ``, ``, + ` + + `, + ` + + `, ` `, + options: [{ ignoreWhenEmpty: false }], errors: ['Expected 1 line break after opening tag (`
`), but no line breaks found.'] } ] diff --git a/tests/lib/rules/singleline-html-element-content-newline.js b/tests/lib/rules/singleline-html-element-content-newline.js index a5c9bce03..679e48682 100644 --- a/tests/lib/rules/singleline-html-element-content-newline.js +++ b/tests/lib/rules/singleline-html-element-content-newline.js @@ -23,6 +23,11 @@ const tester = new RuleTester({ tester.run('singleline-html-element-content-newline', rule, { valid: [ + ` + + `, ` `, + options: [{ ignoreWhenEmpty: false }], errors: [ 'Expected 1 line break after opening tag (`
`), but no line breaks found.' ] @@ -429,7 +435,7 @@ singleline element
`, - options: [{ ignoreWhenNoAttributes: false }], + options: [{ ignoreWhenEmpty: false, ignoreWhenNoAttributes: false }], output: ` `, - options: [{ ignoreWhenNoAttributes: false }], + options: [{ ignoreWhenEmpty: false, ignoreWhenNoAttributes: false }], output: `