@@ -18,18 +18,20 @@ import { getKey, getRawKey } from "../util.js";
1818/**
1919 * @import { JSONRuleDefinition } from "../types.ts";
2020 * @import { MemberNode } from "@humanwhocodes/momoa";
21- *
2221 * @typedef {Object } SortOptions
23- * @property {boolean } caseSensitive
24- * @property {boolean } natural
25- * @property {number } minKeys
26- * @property {boolean } allowLineSeparatedGroups
27- *
22+ * @property {boolean } caseSensitive Whether key comparisons are case-sensitive.
23+ * @property {boolean } natural Whether to use natural sort order instead of purely alphanumeric.
24+ * @property {number } minKeys Minimum number of keys in an object before enforcing sorting.
25+ * @property {boolean } allowLineSeparatedGroups Whether a blank line between properties starts a new group that is independently sorted.
2826 * @typedef {"sortKeys" } SortKeysMessageIds
2927 * @typedef {"asc"|"desc" } SortDirection
3028 * @typedef {[SortDirection, SortOptions] } SortKeysRuleOptions
3129 * @typedef {JSONRuleDefinition<{ RuleOptions: SortKeysRuleOptions, MessageIds: SortKeysMessageIds }> } SortKeysRuleDefinition
3230 * @typedef {(a:string,b:string) => boolean } Comparator
31+ * @typedef {"ascending"|"descending" } DirectionName
32+ * @typedef {"alphanumeric"|"natural" } SortName
33+ * @typedef {"sensitive"|"insensitive" } Sensitivity
34+ * @typedef {Record<DirectionName, Record<SortName, Record<Sensitivity, Comparator>>> } ComparatorMap
3335 */
3436
3537//-----------------------------------------------------------------------------
@@ -38,39 +40,30 @@ import { getKey, getRawKey } from "../util.js";
3840
3941const hasNonWhitespace = / \S / u;
4042
43+ /** @type {ComparatorMap } */
4144const comparators = {
4245 ascending : {
4346 alphanumeric : {
44- /** @type {Comparator } */
4547 sensitive : ( a , b ) => a <= b ,
46-
47- /** @type {Comparator } */
4848 insensitive : ( a , b ) => a . toLowerCase ( ) <= b . toLowerCase ( ) ,
4949 } ,
5050 natural : {
51- /** @type {Comparator } */
5251 sensitive : ( a , b ) => naturalCompare ( a , b ) <= 0 ,
53-
54- /** @type {Comparator } */
5552 insensitive : ( a , b ) =>
5653 naturalCompare ( a . toLowerCase ( ) , b . toLowerCase ( ) ) <= 0 ,
5754 } ,
5855 } ,
5956 descending : {
6057 alphanumeric : {
61- /** @type {Comparator } */
6258 sensitive : ( a , b ) =>
6359 comparators . ascending . alphanumeric . sensitive ( b , a ) ,
6460
65- /** @type {Comparator } */
6661 insensitive : ( a , b ) =>
6762 comparators . ascending . alphanumeric . insensitive ( b , a ) ,
6863 } ,
6964 natural : {
70- /** @type {Comparator } */
7165 sensitive : ( a , b ) => comparators . ascending . natural . sensitive ( b , a ) ,
7266
73- /** @type {Comparator } */
7467 insensitive : ( a , b ) =>
7568 comparators . ascending . natural . insensitive ( b , a ) ,
7669 } ,
@@ -140,9 +133,13 @@ const rule = {
140133 { allowLineSeparatedGroups, caseSensitive, natural, minKeys } ,
141134 ] = context . options ;
142135
136+ /** @type {DirectionName } */
143137 const direction = directionShort === "asc" ? "ascending" : "descending" ;
138+ /** @type {SortName } */
144139 const sortName = natural ? "natural" : "alphanumeric" ;
140+ /** @type {Sensitivity } */
145141 const sensitivity = caseSensitive ? "sensitive" : "insensitive" ;
142+ /** @type {Comparator } */
146143 const isValidOrder = comparators [ direction ] [ sortName ] [ sensitivity ] ;
147144
148145 // Note that @humanwhocodes /momoa doesn't include comments in the object.members tree, so we can't just see if a member is preceded by a comment
@@ -161,7 +158,7 @@ const rule = {
161158 * Checks if two members are line-separated.
162159 * @param {MemberNode } prevMember The previous member.
163160 * @param {MemberNode } member The current member.
164- * @return {boolean }
161+ * @returns {boolean } True if the members are separated by at least one blank line (ignoring comment-only lines).
165162 */
166163 function isLineSeparated ( prevMember , member ) {
167164 // Note that there can be comments *inside* members, e.g. `{"foo: /* comment *\/ "bar"}`, but these are ignored when calculating line-separated groups
0 commit comments