From e4bb81cd0d2821abe7d69b3d685bbd8a14569df8 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 25 Mar 2021 00:23:58 -0400 Subject: [PATCH 1/3] feat(max-attributes-per-line): allow max attribues 0 for singleline --- lib/rules/max-attributes-per-line.js | 9 ++++++--- tests/lib/rules/max-attributes-per-line.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index e379df47f..696fb9a3d 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -26,14 +26,14 @@ module.exports = { anyOf: [ { type: 'number', - minimum: 1 + minimum: 0 }, { type: 'object', properties: { max: { type: 'number', - minimum: 1 + minimum: 0 } }, additionalProperties: false @@ -121,7 +121,10 @@ module.exports = { if (options) { if (typeof options.singleline === 'number') { defaults.singleline = options.singleline - } else if (options.singleline && options.singleline.max) { + } else if ( + options.singleline && + typeof options.singleline.max === 'number' + ) { defaults.singleline = options.singleline.max } diff --git a/tests/lib/rules/max-attributes-per-line.js b/tests/lib/rules/max-attributes-per-line.js index fa7210027..6356f4557 100644 --- a/tests/lib/rules/max-attributes-per-line.js +++ b/tests/lib/rules/max-attributes-per-line.js @@ -299,6 +299,19 @@ petname="Snoopy" extra="foo"> line: 3 } ] + }, + { + code: ``, + options: [{ singleline: { max: 0 } }], + output: ``, + errors: [ + { + message: "'name' should be on a new line.", + type: 'VAttribute', + line: 1 + } + ] } ] }) From 541804d822e86d12cfae24fba3b45778c1247a08 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 25 Mar 2021 15:29:05 -0400 Subject: [PATCH 2/3] feat(max-attributes-per-line): allowFirstLine option --- lib/rules/max-attributes-per-line.js | 52 ++++++++++++++-------- tests/lib/rules/max-attributes-per-line.js | 2 +- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 696fb9a3d..d50a78d6d 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -26,14 +26,17 @@ module.exports = { anyOf: [ { type: 'number', - minimum: 0 + minimum: 1 }, { type: 'object', properties: { max: { type: 'number', - minimum: 0 + minimum: 1 + }, + allowFirstLine: { + type: 'boolean' } }, additionalProperties: false @@ -72,7 +75,8 @@ module.exports = { const configuration = parseOptions(context.options[0]) const multilineMaximum = configuration.multiline const singlelinemMaximum = configuration.singleline - const canHaveFirstLine = configuration.allowFirstLine + const canHaveSinglelineFirstLine = configuration.singlelineAllowFirstLine + const canHaveMultilineFirstLine = configuration.multilineAllowFirstLine const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() @@ -83,16 +87,22 @@ module.exports = { if (!numberOfAttributes) return - if ( - utils.isSingleLine(node) && - numberOfAttributes > singlelinemMaximum - ) { - showErrors(node.attributes.slice(singlelinemMaximum)) + if (utils.isSingleLine(node)) { + if ( + !canHaveSinglelineFirstLine && + node.attributes[0].loc.start.line === node.loc.start.line + ) { + showErrors([node.attributes[0]]) + } + + if (numberOfAttributes > singlelinemMaximum) { + showErrors(node.attributes.slice(singlelinemMaximum)) + } } if (!utils.isSingleLine(node)) { if ( - !canHaveFirstLine && + !canHaveMultilineFirstLine && node.attributes[0].loc.start.line === node.loc.start.line ) { showErrors([node.attributes[0]]) @@ -114,30 +124,36 @@ module.exports = { function parseOptions(options) { const defaults = { singleline: 1, + singlelineAllowFirstLine: true, multiline: 1, - allowFirstLine: false + multilineAllowFirstLine: false } if (options) { if (typeof options.singleline === 'number') { defaults.singleline = options.singleline - } else if ( - options.singleline && - typeof options.singleline.max === 'number' - ) { - defaults.singleline = options.singleline.max + } else if (typeof options.singleline === 'object') { + if (typeof options.singleline.max === 'number') { + defaults.singleline = options.singleline.max + } + + if (typeof options.singleline.allowFirstLine === 'boolean') { + defaults.singlelineAllowFirstLine = + options.singleline.allowFirstLine + } } if (options.multiline) { if (typeof options.multiline === 'number') { defaults.multiline = options.multiline } else if (typeof options.multiline === 'object') { - if (options.multiline.max) { + if (typeof options.multiline.max === 'number') { defaults.multiline = options.multiline.max } - if (options.multiline.allowFirstLine) { - defaults.allowFirstLine = options.multiline.allowFirstLine + if (typeof options.multiline.allowFirstLine === 'boolean') { + defaults.multilineAllowFirstLine = + options.multiline.allowFirstLine } } } diff --git a/tests/lib/rules/max-attributes-per-line.js b/tests/lib/rules/max-attributes-per-line.js index 6356f4557..aedc6d6d8 100644 --- a/tests/lib/rules/max-attributes-per-line.js +++ b/tests/lib/rules/max-attributes-per-line.js @@ -302,7 +302,7 @@ petname="Snoopy" extra="foo"> }, { code: ``, - options: [{ singleline: { max: 0 } }], + options: [{ singleline: { allowFirstLine: false } }], output: ``, errors: [ From c3a85329af7436122332e77457eae8ff3f3b722a Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 25 Mar 2021 15:40:11 -0400 Subject: [PATCH 3/3] docs(max-attributes-per-line): updated options and added example --- docs/rules/max-attributes-per-line.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/rules/max-attributes-per-line.md b/docs/rules/max-attributes-per-line.md index 69bd77949..dac9d2d72 100644 --- a/docs/rules/max-attributes-per-line.md +++ b/docs/rules/max-attributes-per-line.md @@ -57,7 +57,10 @@ There is a configurable number of attributes that are acceptable in one-line cas ```json { "vue/max-attributes-per-line": ["error", { - "singleline": 1, + "singleline": { + "max": 1, + "allowFirstLine": true + }, "multiline": { "max": 1, "allowFirstLine": false @@ -66,7 +69,8 @@ There is a configurable number of attributes that are acceptable in one-line cas } ``` -- `singleline` (`number`) ... The number of maximum attributes per line when the opening tag is in a single line. Default is `1`. +- `singleline.max` (`number`) ... The number of maximum attributes per line when the opening tag is in a single line. Default is `1`. +- `singleline.allowFirstLine` (`boolean`) ... If `true`, it allows attributes on the same line as that tag name. Default is `true`. - `multiline.max` (`number`) ... The max number of attributes per line when the opening tag is in multiple lines. Default is `1`. This can be `{ multiline: 1 }` instead of `{ multiline: { max: 1 }}` if you don't configure `allowFirstLine` property. - `multiline.allowFirstLine` (`boolean`) ... If `true`, it allows attributes on the same line as that tag name. Default is `false`. @@ -86,6 +90,24 @@ There is a configurable number of attributes that are acceptable in one-line cas +### `"singleline": 1, "allowFirstLine": false` + + + +```vue + +``` + + + ### `"multiline": 2`