eslint-plugin-absolute 0.11.7 → 0.11.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +46 -49
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1343,6 +1343,20 @@ var sortKeysFixable = createRule({
1343
1343
  }
1344
1344
  return prevEnd;
1345
1345
  };
1346
+ const compareProps = (left, right) => {
1347
+ if (variablesBeforeFunctions) {
1348
+ const leftIsFunc = isFunctionProperty(left);
1349
+ const rightIsFunc = isFunctionProperty(right);
1350
+ if (leftIsFunc !== rightIsFunc) {
1351
+ return leftIsFunc ? 1 : SORT_BEFORE;
1352
+ }
1353
+ }
1354
+ let res = compareKeys(getPropertyKeyName(left), getPropertyKeyName(right));
1355
+ if (order === "desc") {
1356
+ res = -res;
1357
+ }
1358
+ return res;
1359
+ };
1346
1360
  const buildSortedText = (fixableProps, rangeStart) => {
1347
1361
  const chunks = [];
1348
1362
  for (let idx = 0;idx < fixableProps.length; idx++) {
@@ -1357,22 +1371,7 @@ var sortKeysFixable = createRule({
1357
1371
  const text = sourceCode.text.slice(chunkStart, fullEnd);
1358
1372
  chunks.push({ prop, text });
1359
1373
  }
1360
- const sorted = chunks.slice().sort((left, right) => {
1361
- if (variablesBeforeFunctions) {
1362
- const leftIsFunc = isFunctionProperty(left.prop);
1363
- const rightIsFunc = isFunctionProperty(right.prop);
1364
- if (leftIsFunc !== rightIsFunc) {
1365
- return leftIsFunc ? 1 : SORT_BEFORE;
1366
- }
1367
- }
1368
- const leftKey = getPropertyKeyName(left.prop);
1369
- const rightKey = getPropertyKeyName(right.prop);
1370
- let res = compareKeys(leftKey, rightKey);
1371
- if (order === "desc") {
1372
- res = -res;
1373
- }
1374
- return res;
1375
- });
1374
+ const sorted = chunks.slice().sort((left, right) => compareProps(left.prop, right.prop));
1376
1375
  const firstPropLine = fixableProps[0].loc.start.line;
1377
1376
  const lastPropLine = fixableProps[fixableProps.length - 1].loc.start.line;
1378
1377
  const isMultiline = firstPropLine !== lastPropLine;
@@ -1396,26 +1395,34 @@ ${indent}`;
1396
1395
  return separator + stripped;
1397
1396
  }).join("");
1398
1397
  };
1399
- const getFixableProps = (node) => node.properties.filter((prop) => prop.type === "Property" && !prop.computed && (prop.key.type === "Identifier" || prop.key.type === "Literal"));
1400
1398
  const checkObjectExpression = (node) => {
1401
1399
  if (node.properties.length < minKeys) {
1402
1400
  return;
1403
1401
  }
1402
+ const segments = [];
1403
+ let currentSegment = [];
1404
+ for (const prop of node.properties) {
1405
+ if (prop.type === "Property") {
1406
+ currentSegment.push(prop);
1407
+ continue;
1408
+ }
1409
+ if (currentSegment.length > 0) {
1410
+ segments.push(currentSegment);
1411
+ currentSegment = [];
1412
+ }
1413
+ }
1414
+ if (currentSegment.length > 0) {
1415
+ segments.push(currentSegment);
1416
+ }
1404
1417
  let autoFixable = true;
1405
1418
  const keys = node.properties.map((prop) => {
1406
- let keyName = null;
1407
- let isFunc = false;
1408
1419
  if (prop.type !== "Property") {
1409
- autoFixable = false;
1410
- return {
1411
- isFunction: isFunc,
1412
- keyName,
1413
- node: prop
1414
- };
1420
+ return { isFunction: false, keyName: null, node: prop };
1415
1421
  }
1416
1422
  if (prop.computed) {
1417
1423
  autoFixable = false;
1418
1424
  }
1425
+ let keyName = null;
1419
1426
  if (prop.key.type === "Identifier") {
1420
1427
  keyName = prop.key.name;
1421
1428
  } else if (prop.key.type === "Literal") {
@@ -1424,11 +1431,8 @@ ${indent}`;
1424
1431
  } else {
1425
1432
  autoFixable = false;
1426
1433
  }
1427
- if (isFunctionProperty(prop)) {
1428
- isFunc = true;
1429
- }
1430
1434
  return {
1431
- isFunction: isFunc,
1435
+ isFunction: isFunctionProperty(prop),
1432
1436
  keyName,
1433
1437
  node: prop
1434
1438
  };
@@ -1436,30 +1440,23 @@ ${indent}`;
1436
1440
  if (hasDuplicatePropertyNames(node.properties)) {
1437
1441
  autoFixable = false;
1438
1442
  }
1439
- if (autoFixable) {
1440
- const impureCount = keys.filter((key) => key.node.type === "Property" && !isPureRuntimeExpression(key.node.value, getStableLocalsForNode(key.node))).length;
1441
- autoFixable = impureCount <= 1;
1442
- }
1443
+ const isSegmentFixable = (segment) => segment.filter((prop) => !isPureRuntimeExpression(prop.value, getStableLocalsForNode(prop))).length <= 1;
1444
+ const isSegmentSorted = (segment) => segment.every((prop, idx) => idx === 0 || compareProps(segment[idx - 1], prop) <= 0);
1443
1445
  let fixProvided = false;
1444
1446
  const createReportWithFix = (curr, shouldFix) => {
1445
1447
  context.report({
1446
1448
  fix: shouldFix ? (fixer) => {
1447
- const fixableProps = getFixableProps(node);
1448
- if (fixableProps.length < minKeys) {
1449
- return null;
1450
- }
1451
- const [firstProp] = fixableProps;
1452
- const lastProp = fixableProps[fixableProps.length - 1];
1453
- if (!firstProp || !lastProp) {
1454
- return null;
1455
- }
1456
- const firstLeading = getLeadingComments(firstProp, null);
1457
- const [firstLeadingComment] = firstLeading;
1458
- const rangeStart = firstLeadingComment ? firstLeadingComment.range[0] : firstProp.range[0];
1459
- const lastTrailing = getTrailingComments(lastProp, null);
1460
- const rangeEnd = lastTrailing.length > 0 ? lastTrailing[lastTrailing.length - 1].range[1] : lastProp.range[1];
1461
- const sortedText = buildSortedText(fixableProps, rangeStart);
1462
- return fixer.replaceTextRange([rangeStart, rangeEnd], sortedText);
1449
+ const fixes = segments.filter((segment) => segment.length >= minKeys && isSegmentFixable(segment) && !isSegmentSorted(segment)).map((segment) => {
1450
+ const [firstProp] = segment;
1451
+ const lastProp = segment[segment.length - 1];
1452
+ const firstLeading = getLeadingComments(firstProp, null);
1453
+ const [firstLeadingComment] = firstLeading;
1454
+ const rangeStart = firstLeadingComment ? firstLeadingComment.range[0] : firstProp.range[0];
1455
+ const lastTrailing = getTrailingComments(lastProp, null);
1456
+ const rangeEnd = lastTrailing.length > 0 ? lastTrailing[lastTrailing.length - 1].range[1] : lastProp.range[1];
1457
+ return fixer.replaceTextRange([rangeStart, rangeEnd], buildSortedText(segment, rangeStart));
1458
+ });
1459
+ return fixes.length > 0 ? fixes : null;
1463
1460
  } : null,
1464
1461
  messageId: "unsorted",
1465
1462
  node: curr.node.type === "Property" ? curr.node.key : curr.node
package/package.json CHANGED
@@ -41,5 +41,5 @@
41
41
  "typecheck": "bun run tsc --noEmit"
42
42
  },
43
43
  "type": "module",
44
- "version": "0.11.7"
44
+ "version": "0.11.8"
45
45
  }