forked from vuejs/eslint-plugin-vue
/
sort-keys.js
219 lines (197 loc) · 5.56 KB
/
sort-keys.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @fileoverview enforce sort-keys in a manner that is compatible with order-in-components
* @author Loren Klingman
* Original ESLint sort-keys by Toru Nagashima
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const naturalCompare = require('natural-compare')
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Gets the property name of the given `Property` node.
*
* - If the property's key is an `Identifier` node, this returns the key's name
* whether it's a computed property or not.
* - If the property has a static name, this returns the static name.
* - Otherwise, this returns null.
* @param {ASTNode} node The `Property` node to get.
* @returns {string|null} The property name or null.
* @private
*/
function getPropertyName (node) {
const staticName = utils.getStaticPropertyName(node)
if (staticName !== null) {
return staticName
}
return node.key.name || null
}
/**
* Functions which check that the given 2 names are in specific order.
*
* Postfix `I` is meant insensitive.
* Postfix `N` is meant natual.
* @private
*/
const isValidOrders = {
asc (a, b) {
return a <= b
},
ascI (a, b) {
return a.toLowerCase() <= b.toLowerCase()
},
ascN (a, b) {
return naturalCompare(a, b) <= 0
},
ascIN (a, b) {
return naturalCompare(a.toLowerCase(), b.toLowerCase()) <= 0
},
desc (a, b) {
return isValidOrders.asc(b, a)
},
descI (a, b) {
return isValidOrders.ascI(b, a)
},
descN (a, b) {
return isValidOrders.ascN(b, a)
},
descIN (a, b) {
return isValidOrders.ascIN(b, a)
}
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce sort-keys in a manner that is compatible with order-in-components',
category: null,
recommended: false,
url: 'https://eslint.vuejs.org/rules/sort-keys.html'
},
fixable: null,
schema: [
{
enum: ['asc', 'desc']
},
{
type: 'object',
properties: {
caseSensitive: {
type: 'boolean',
default: true
},
natural: {
type: 'boolean',
default: false
},
minKeys: {
type: 'integer',
minimum: 2,
default: 2
},
runOutsideVue: {
type: 'boolean',
default: true
}
},
additionalProperties: false
}
]
},
create (context) {
// Parse options.
const order = context.options[0] || 'asc'
const options = context.options[1]
const insensitive = options && options.caseSensitive === false
const natual = options && options.natural
const minKeys = options && options.minKeys
const isValidOrder = isValidOrders[
order + (insensitive ? 'I' : '') + (natual ? 'N' : '')
]
// The stack to save the previous property's name for each object literals.
let stack = null
let errors = []
const reportErrors = (isVue) => {
if (isVue) {
errors = errors.filter((error) => error.hasUpper)
}
errors.forEach((error) => context.report(error))
errors = []
}
const sortTests = {
ObjectExpression (node) {
if (!stack) {
reportErrors(false)
}
stack = {
upper: stack,
prevName: null,
numKeys: node.properties.length
}
},
'ObjectExpression:exit' () {
stack = stack.upper
},
SpreadElement (node) {
if (node.parent.type === 'ObjectExpression') {
stack.prevName = null
}
},
'Program:exit' () {
reportErrors(false)
},
Property (node) {
if (node.parent.type === 'ObjectPattern') {
return
}
const prevName = stack.prevName
const numKeys = stack.numKeys
const thisName = getPropertyName(node)
if (thisName !== null) {
stack.prevName = thisName
}
if (prevName === null || thisName === null || numKeys < minKeys) {
return
}
if (!isValidOrder(prevName, thisName)) {
errors.push({
node,
hasUpper: !!stack.upper,
loc: node.key.loc,
message: "Expected object keys to be in {{natual}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'.",
data: {
thisName,
prevName,
order,
insensitive: insensitive ? 'insensitive ' : '',
natual: natual ? 'natural ' : ''
}
})
}
}
}
const execOnVue = utils.executeOnVue(context, (obj) => {
reportErrors(true)
})
const result = { ...sortTests }
Object.keys(execOnVue).forEach((key) => {
// Ensure we call both the callback from sortTests and execOnVue if they both use the same key
if (Object.prototype.hasOwnProperty.call(sortTests, key)) {
result[key] = (node) => {
sortTests[key](node)
execOnVue[key](node)
}
} else {
result[key] = execOnVue[key]
}
})
return result
}
}