-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathmax-attributes-per-line.js
190 lines (166 loc) · 5.4 KB
/
max-attributes-per-line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* @fileoverview Define the number of attributes allows per line
* @author Filipa Lacerda
*/
'use strict'
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const utils = require('../utils')
module.exports = {
meta: {
docs: {
description: 'enforce the maximum number of attributes per line',
category: 'strongly-recommended',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/max-attributes-per-line.md'
},
fixable: 'whitespace', // or "code" or "whitespace"
schema: [
{
type: 'object',
properties: {
singleline: {
anyOf: [
{
type: 'number',
minimum: 1
},
{
type: 'object',
properties: {
max: {
type: 'number',
minimum: 1
}
},
additionalProperties: false
}
]
},
multiline: {
anyOf: [
{
type: 'number',
minimum: 1
},
{
type: 'object',
properties: {
max: {
type: 'number',
minimum: 1
},
allowFirstLine: {
type: 'boolean'
}
},
additionalProperties: false
}
]
}
}
}
]
},
create: function (context) {
const configuration = parseOptions(context.options[0])
const multilineMaximum = configuration.multiline
const singlelinemMaximum = configuration.singleline
const canHaveFirstLine = configuration.allowFirstLine
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
return utils.defineTemplateBodyVisitor(context, {
'VStartTag' (node) {
const numberOfAttributes = node.attributes.length
if (!numberOfAttributes) return
if (utils.isSingleLine(node) && numberOfAttributes > singlelinemMaximum) {
showErrors(node.attributes.slice(singlelinemMaximum))
}
if (!utils.isSingleLine(node)) {
if (!canHaveFirstLine && node.attributes[0].loc.start.line === node.loc.start.line) {
showErrors([node.attributes[0]])
}
groupAttrsByLine(node.attributes)
.filter(attrs => attrs.length > multilineMaximum)
.forEach(attrs => showErrors(attrs.splice(multilineMaximum)))
}
}
})
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
function parseOptions (options) {
const defaults = {
singleline: 1,
multiline: 1,
allowFirstLine: false
}
if (options) {
if (typeof options.singleline === 'number') {
defaults.singleline = options.singleline
} else if (options.singleline && options.singleline.max) {
defaults.singleline = options.singleline.max
}
if (options.multiline) {
if (typeof options.multiline === 'number') {
defaults.multiline = options.multiline
} else if (typeof options.multiline === 'object') {
if (options.multiline.max) {
defaults.multiline = options.multiline.max
}
if (options.multiline.allowFirstLine) {
defaults.allowFirstLine = options.multiline.allowFirstLine
}
}
}
}
return defaults
}
function getPropData (prop) {
let propType = 'Attribute'
let propName = prop.key.name
if (utils.isBindingAttribute(prop)) {
propType = 'Binding'
propName = prop.key.raw.argument
} else if (utils.isEventAttribute(prop)) {
propType = 'Event'
propName = prop.key.raw.argument
} else if (utils.isDirectiveAttribute(prop)) {
propType = 'Directive'
}
return { propType, propName }
}
function showErrors (attributes) {
attributes.forEach((prop, i) => {
const fix = (fixer) => {
if (i !== 0) return null
// Find the closest token before the current prop
// that is not a white space
const prevToken = template.getTokenBefore(prop, {
filter: (token) => token.type !== 'HTMLWhitespace'
})
const range = [prevToken.range[1], prop.range[0]]
return fixer.replaceTextRange(range, '\n')
}
context.report({
node: prop,
loc: prop.loc,
message: '{{propType}} "{{propName}}" should be on a new line.',
data: getPropData(prop),
fix
})
})
}
function groupAttrsByLine (attributes) {
const propsPerLine = [[attributes[0]]]
attributes.reduce((previous, current) => {
if (previous.loc.end.line === current.loc.start.line) {
propsPerLine[propsPerLine.length - 1].push(current)
} else {
propsPerLine.push([current])
}
return current
})
return propsPerLine
}
}
}