Skip to content

All capped keys should be at the top. #300 #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/rules/sort-destructure-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const NODE_TYPE_SORT_ORDER = {
ExperimentalRestProperty: 99,

/* Property keys */
ALL_CAPS_KEY: 0,
Identifier: 1,
Literal: 1,
TemplateLiteral: 2,
Expand All @@ -43,9 +44,13 @@ const NODE_TYPE_SORT_ORDER = {
const SORT_ORDER_DEFAULT = 98;

function getSortOrder(node) {
return isComputedProperty(node)
? NODE_TYPE_SORT_ORDER.ComputedProperty
: NODE_TYPE_SORT_ORDER[node.type] || SORT_ORDER_DEFAULT;
if (isAllCaps(getNodeName(node))) return NODE_TYPE_SORT_ORDER.ALL_CAPS_KEY;
if (isComputedProperty(node)) return NODE_TYPE_SORT_ORDER.ComputedProperty;
return NODE_TYPE_SORT_ORDER[node.type] || SORT_ORDER_DEFAULT;
}

function isAllCaps(key) {
return /^[A-Z_]+$/.test(key);
}

/**
Expand Down Expand Up @@ -140,13 +145,28 @@ function createSorter(caseSensitive) {

return (a, b) => {
// When we have different node "types"

const nodeResult = getSortOrder(a) - getSortOrder(b);
if (nodeResult !== 0) return nodeResult;

// When the keys have different "types"
const keyResult = getSortOrder(a.key) - getSortOrder(b.key);

if (keyResult !== 0) return keyResult;

// Handle ALL_CAPS special cases
const isAAllCaps = getSortOrder(a) === 0;
const isBAllCaps = getSortOrder(b) === 0;

if (isAAllCaps && isBAllCaps) {
// Both ALL_CAPS: compare directly
return getNodeName(a).localeCompare(getNodeName(b));
}

if (isAAllCaps || isBAllCaps) {
// If either is ALL_CAPS but not both: keep original order
return 0;
}
return naturalCompare(sortName(getNodeName(a)), sortName(getNodeName(b)));
};
}
Expand All @@ -159,6 +179,7 @@ function createFix({ context, fixer, node, sorter }) {
const sourceText = sourceCode.getText();

const sorted = node.properties.concat().sort(sorter);
console.log({ sorted });

const newText = sorted
.map((child, i) => {
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/sort-destructure-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ function testsWithParser(parser, parserOptions = {}) {
],
});

testsFor("capitalized variables", {
valid: ["const { LAST_NAME, NAME_FIRST, Motto, a, b } = someObject;"],
invalid: [
{
code: "const { NAME_FIRST, Motto, LAST_NAME, b, a } = someObject;",
errors: just("Motto", "LAST_NAME"),
output: "const { LAST_NAME, NAME_FIRST, Motto, a, b } = someObject;",
},
],
});

describe("options", () => {
testsFor("caseSensitive", {
valid: [
Expand Down