Skip to content

Commit 8bdb2a9

Browse files
feat(max-attributes-per-line): singleline.allowFirstLine option (#1465)
* feat(max-attributes-per-line): allow max attribues 0 for singleline * feat(max-attributes-per-line): allowFirstLine option * docs(max-attributes-per-line): updated options and added example
1 parent 62f577d commit 8bdb2a9

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

docs/rules/max-attributes-per-line.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ There is a configurable number of attributes that are acceptable in one-line cas
5757
```json
5858
{
5959
"vue/max-attributes-per-line": ["error", {
60-
"singleline": 1,
60+
"singleline": {
61+
"max": 1,
62+
"allowFirstLine": true
63+
},
6164
"multiline": {
6265
"max": 1,
6366
"allowFirstLine": false
@@ -66,7 +69,8 @@ There is a configurable number of attributes that are acceptable in one-line cas
6669
}
6770
```
6871

69-
- `singleline` (`number`) ... The number of maximum attributes per line when the opening tag is in a single line. Default is `1`.
72+
- `singleline.max` (`number`) ... The number of maximum attributes per line when the opening tag is in a single line. Default is `1`.
73+
- `singleline.allowFirstLine` (`boolean`) ... If `true`, it allows attributes on the same line as that tag name. Default is `true`.
7074
- `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.
7175
- `multiline.allowFirstLine` (`boolean`) ... If `true`, it allows attributes on the same line as that tag name. Default is `false`.
7276

@@ -86,6 +90,24 @@ There is a configurable number of attributes that are acceptable in one-line cas
8690

8791
</eslint-code-block>
8892

93+
### `"singleline": 1, "allowFirstLine": false`
94+
95+
<eslint-code-block fix :rules="{'vue/max-attributes-per-line': ['error', {singleline: { allowFirstLine: false }}]}">
96+
97+
```vue
98+
<template>
99+
<!-- ✓ GOOD -->
100+
<MyComponent
101+
lorem="1"
102+
/>
103+
104+
<!-- ✗ BAD -->
105+
<MyComponent lorem="1" />
106+
</template>
107+
```
108+
109+
</eslint-code-block>
110+
89111
### `"multiline": 2`
90112

91113
<eslint-code-block fix :rules="{'vue/max-attributes-per-line': ['error', {multiline: 2}]}">

lib/rules/max-attributes-per-line.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module.exports = {
3434
max: {
3535
type: 'number',
3636
minimum: 1
37+
},
38+
allowFirstLine: {
39+
type: 'boolean'
3740
}
3841
},
3942
additionalProperties: false
@@ -72,7 +75,8 @@ module.exports = {
7275
const configuration = parseOptions(context.options[0])
7376
const multilineMaximum = configuration.multiline
7477
const singlelinemMaximum = configuration.singleline
75-
const canHaveFirstLine = configuration.allowFirstLine
78+
const canHaveSinglelineFirstLine = configuration.singlelineAllowFirstLine
79+
const canHaveMultilineFirstLine = configuration.multilineAllowFirstLine
7680
const template =
7781
context.parserServices.getTemplateBodyTokenStore &&
7882
context.parserServices.getTemplateBodyTokenStore()
@@ -83,16 +87,22 @@ module.exports = {
8387

8488
if (!numberOfAttributes) return
8589

86-
if (
87-
utils.isSingleLine(node) &&
88-
numberOfAttributes > singlelinemMaximum
89-
) {
90-
showErrors(node.attributes.slice(singlelinemMaximum))
90+
if (utils.isSingleLine(node)) {
91+
if (
92+
!canHaveSinglelineFirstLine &&
93+
node.attributes[0].loc.start.line === node.loc.start.line
94+
) {
95+
showErrors([node.attributes[0]])
96+
}
97+
98+
if (numberOfAttributes > singlelinemMaximum) {
99+
showErrors(node.attributes.slice(singlelinemMaximum))
100+
}
91101
}
92102

93103
if (!utils.isSingleLine(node)) {
94104
if (
95-
!canHaveFirstLine &&
105+
!canHaveMultilineFirstLine &&
96106
node.attributes[0].loc.start.line === node.loc.start.line
97107
) {
98108
showErrors([node.attributes[0]])
@@ -114,27 +124,36 @@ module.exports = {
114124
function parseOptions(options) {
115125
const defaults = {
116126
singleline: 1,
127+
singlelineAllowFirstLine: true,
117128
multiline: 1,
118-
allowFirstLine: false
129+
multilineAllowFirstLine: false
119130
}
120131

121132
if (options) {
122133
if (typeof options.singleline === 'number') {
123134
defaults.singleline = options.singleline
124-
} else if (options.singleline && options.singleline.max) {
125-
defaults.singleline = options.singleline.max
135+
} else if (typeof options.singleline === 'object') {
136+
if (typeof options.singleline.max === 'number') {
137+
defaults.singleline = options.singleline.max
138+
}
139+
140+
if (typeof options.singleline.allowFirstLine === 'boolean') {
141+
defaults.singlelineAllowFirstLine =
142+
options.singleline.allowFirstLine
143+
}
126144
}
127145

128146
if (options.multiline) {
129147
if (typeof options.multiline === 'number') {
130148
defaults.multiline = options.multiline
131149
} else if (typeof options.multiline === 'object') {
132-
if (options.multiline.max) {
150+
if (typeof options.multiline.max === 'number') {
133151
defaults.multiline = options.multiline.max
134152
}
135153

136-
if (options.multiline.allowFirstLine) {
137-
defaults.allowFirstLine = options.multiline.allowFirstLine
154+
if (typeof options.multiline.allowFirstLine === 'boolean') {
155+
defaults.multilineAllowFirstLine =
156+
options.multiline.allowFirstLine
138157
}
139158
}
140159
}

tests/lib/rules/max-attributes-per-line.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ petname="Snoopy" extra="foo">
299299
line: 3
300300
}
301301
]
302+
},
303+
{
304+
code: `<template><component name="John Doe"></component></template>`,
305+
options: [{ singleline: { allowFirstLine: false } }],
306+
output: `<template><component
307+
name="John Doe"></component></template>`,
308+
errors: [
309+
{
310+
message: "'name' should be on a new line.",
311+
type: 'VAttribute',
312+
line: 1
313+
}
314+
]
302315
}
303316
]
304317
})

0 commit comments

Comments
 (0)