{"version":3,"file":"rule.declaration-order-violation.mjs","sources":["../../../../../src/rules/css/grouped-declarations/rule.declaration-order-violation.ts"],"sourcesContent":["import type { TSESTree } from \"@typescript-eslint/utils\";\nimport type { RuleContext } from \"@typescript-eslint/utils/ts-eslint\";\nimport type { Declaration } from \"postcss\";\n\nimport type { Options } from \"./_rule\";\nimport { MessageIds } from \"./_rule\";\nimport type { GroupOrder } from \"./config.order\";\nimport { defaultOrder } from \"./config.order\";\nimport type {\n  DeclarationGroup,\n  DeclarationScope,\n} from \"./util.extract-declaration-scopes\";\nimport {\n  findGroupOrderForProperty,\n  findPropertyIndexInGroupOrder,\n} from \"./util.find-group-order-for-property\";\nimport { getDeclarationPosition } from \"./util.get-declaration-position\";\n\n/**\n * Verifies all properties in a given scope and report any line that isn't in\n * the expected order.\n */\nexport function reportDeclarationOrderViolations(\n  context: RuleContext<MessageIds, Options>,\n  node: TSESTree.Node,\n  scope: DeclarationScope,\n) {\n  // In case groups are collapsed, then bail out from checking individual\n  // declarations entirely\n  const collapsedGroups =\n    scope.groups.length === 2\n      ? scope.groups[0]?.declarations.reduce(\n          (acc, decl) =>\n            decl.prop.startsWith(\"custom-prop__\") ||\n            decl.prop.startsWith(\"custom-js__\") ||\n            decl.prop.startsWith(\"--\") ||\n            acc,\n          false,\n        ) &&\n        new Set(\n          scope.groups[1]?.declarations.map((decl) =>\n            defaultOrder.findIndex(findGroupOrderForProperty(decl.prop)),\n          ),\n        ).size === scope.groups[1]?.declarations.length\n      : new Set(\n          scope.groups[0]?.declarations.map((decl) =>\n            defaultOrder.findIndex(findGroupOrderForProperty(decl.prop)),\n          ),\n        ).size === scope.groups[0]?.declarations.length;\n\n  if (collapsedGroups) {\n    return;\n  }\n\n  for (const declarationGroup of scope.groups) {\n    if (!declarationGroup || declarationGroup.comments.length > 0) {\n      continue;\n    }\n\n    // Extract the group ordering relevant for the current group, based on the\n    // first property in the group.\n    const relevantGroupOrder = defaultOrder.find(\n      findGroupOrderForProperty(declarationGroup.declarations[0]?.prop),\n    );\n\n    if (!relevantGroupOrder) {\n      continue;\n    }\n\n    const violations = getViolatingDeclarations(\n      relevantGroupOrder,\n      declarationGroup,\n    );\n\n    // Report ALL violations such that the developer may know exactly\n    // what properties aren't grouped properly\n    for (const violation of violations) {\n      const loc = getDeclarationPosition(node, violation);\n\n      if (!loc) {\n        continue;\n      }\n\n      const isInGroup =\n        typeof findPropertyIndexInGroupOrder(\n          relevantGroupOrder,\n          violation.prop,\n        ) !== \"undefined\";\n\n      context.report({\n        loc,\n        messageId: isInGroup\n          ? MessageIds.INVALID_DECLARATION_ORDER\n          : MessageIds.INVALID_GROUPING,\n        data: {\n          property: violation.prop,\n          order: relevantGroupOrder,\n        },\n      });\n    }\n  }\n}\n\nfunction getViolatingDeclarations(\n  order: GroupOrder,\n  group: DeclarationGroup,\n): Declaration[] {\n  if (group.comments.length > 0) {\n    return [];\n  }\n\n  const violations: Declaration[] = [];\n  let lastSeenIndex = -1;\n\n  for (const declaration of group.declarations) {\n    const indexOf =\n      findPropertyIndexInGroupOrder(order, declaration.prop) ?? -1;\n\n    if (declaration.value.includes(\"custom-prop__\")) {\n      continue;\n    }\n\n    // Ignore declarations that are correctly grouped, but invalidly sorted if\n    // they contain JS. We want these to be forcibly placed first.\n    if (indexOf < lastSeenIndex) {\n      violations.push(declaration);\n    }\n\n    lastSeenIndex = Math.max(lastSeenIndex, indexOf);\n  }\n\n  return violations;\n}\n"],"names":[],"mappings":";;;;;AAsBO,SAAS,gCAAA,CACd,OAAA,EACA,IAAA,EACA,KAAA,EACA;AAGA,EAAA,MAAM,eAAA,GACJ,MAAM,MAAA,CAAO,MAAA,KAAW,IACpB,KAAA,CAAM,MAAA,CAAO,CAAC,CAAA,EAAG,YAAA,CAAa,MAAA;AAAA,IAC5B,CAAC,GAAA,EAAK,IAAA,KACJ,IAAA,CAAK,IAAA,CAAK,WAAW,eAAe,CAAA,IACpC,IAAA,CAAK,IAAA,CAAK,WAAW,aAAa,CAAA,IAClC,K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