]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/sort-keys.js
2 * @fileoverview Rule to require object keys to be sorted
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils"),
13 naturalCompare
= require("natural-compare");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
20 * Gets the property name of the given `Property` node.
22 * - If the property's key is an `Identifier` node, this returns the key's name
23 * whether it's a computed property or not.
24 * - If the property has a static name, this returns the static name.
25 * - Otherwise, this returns null.
26 * @param {ASTNode} node The `Property` node to get.
27 * @returns {string|null} The property name or null.
30 function getPropertyName(node
) {
31 const staticName
= astUtils
.getStaticPropertyName(node
);
33 if (staticName
!== null) {
37 return node
.key
.name
|| null;
41 * Functions which check that the given 2 names are in specific order.
43 * Postfix `I` is meant insensitive.
44 * Postfix `N` is meant natural.
47 const isValidOrders
= {
52 return a
.toLowerCase() <= b
.toLowerCase();
55 return naturalCompare(a
, b
) <= 0;
58 return naturalCompare(a
.toLowerCase(), b
.toLowerCase()) <= 0;
61 return isValidOrders
.asc(b
, a
);
64 return isValidOrders
.ascI(b
, a
);
67 return isValidOrders
.ascN(b
, a
);
70 return isValidOrders
.ascIN(b
, a
);
74 //------------------------------------------------------------------------------
76 //------------------------------------------------------------------------------
83 description
: "require object keys to be sorted",
85 url
: "https://eslint.org/docs/rules/sort-keys"
109 additionalProperties
: false
114 sortKeys
: "Expected object keys to be in {{natural}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'."
121 const order
= context
.options
[0] || "asc";
122 const options
= context
.options
[1];
123 const insensitive
= options
&& options
.caseSensitive
=== false;
124 const natural
= options
&& options
.natural
;
125 const minKeys
= options
&& options
.minKeys
;
126 const isValidOrder
= isValidOrders
[
127 order
+ (insensitive
? "I" : "") + (natural
? "N" : "")
130 // The stack to save the previous property's name for each object literals.
134 ObjectExpression(node
) {
138 numKeys
: node
.properties
.length
142 "ObjectExpression:exit"() {
146 SpreadElement(node
) {
147 if (node
.parent
.type
=== "ObjectExpression") {
148 stack
.prevName
= null;
153 if (node
.parent
.type
=== "ObjectPattern") {
157 const prevName
= stack
.prevName
;
158 const numKeys
= stack
.numKeys
;
159 const thisName
= getPropertyName(node
);
161 if (thisName
!== null) {
162 stack
.prevName
= thisName
;
165 if (prevName
=== null || thisName
=== null || numKeys
< minKeys
) {
169 if (!isValidOrder(prevName
, thisName
)) {
173 messageId
: "sortKeys",
178 insensitive
: insensitive
? "insensitive " : "",
179 natural
: natural
? "natural " : ""