From 6c0792a005009da91d4a41c18f0410574ddc5937 Mon Sep 17 00:00:00 2001 From: Armano Date: Mon, 26 Nov 2018 22:34:22 +0100 Subject: [PATCH 1/2] Add `ignoreWhenEmpty` options to multiline-html-element-content-newline and singleline-html-element-content-newline --- .../multiline-html-element-content-newline.md | 3 +++ .../singleline-html-element-content-newline.md | 3 +++ .../multiline-html-element-content-newline.js | 18 +++++++++++++++--- .../singleline-html-element-content-newline.js | 13 ++++++++++++- .../multiline-html-element-content-newline.js | 14 ++++++++++++++ .../singleline-html-element-content-newline.js | 10 ++++++++-- 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/docs/rules/multiline-html-element-content-newline.md b/docs/rules/multiline-html-element-content-newline.md index 1e131f7c8..937583dae 100644 --- a/docs/rules/multiline-html-element-content-newline.md +++ b/docs/rules/multiline-html-element-content-newline.md @@ -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` ... allows having contents in one line, when given 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 0ad75380d..80641fc27 100644 --- a/docs/rules/singleline-html-element-content-newline.md +++ b/docs/rules/singleline-html-element-content-newline.md @@ -45,6 +45,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"] }] } @@ -52,6 +53,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` ... allows having contents in one line, when given 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..8fca9b792 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,12 @@ module.exports = { } const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' } + if (ignoreWhenEmpty && node.children.length === 0) { + if (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..bb496b7eb 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,12 @@ module.exports = { } const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' } + if (ignoreWhenEmpty && node.children.length === 0) { + if (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: `