{"version":3,"file":"create-route-property-order.rule.cjs","names":[],"sources":["../../../../src/rules/create-route-property-order/create-route-property-order.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\n\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { detectTanstackRouterImports } from '../../utils/detect-router-imports'\nimport { sortDataByOrder } from './create-route-property-order.utils'\nimport {\n  createRouteFunctions,\n  createRouteFunctionsIndirect,\n  sortRules,\n} from './constants'\nimport type { CreateRouteFunction } from './constants'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nconst createRouteFunctionSet = new Set(createRouteFunctions)\nfunction isCreateRouteFunction(node: any): node is CreateRouteFunction {\n  return createRouteFunctionSet.has(node)\n}\n\nexport const name = 'create-route-property-order'\n\nexport const rule = createRule({\n  name,\n  meta: {\n    type: 'problem',\n    docs: {\n      description:\n        'Ensure correct order of inference sensitive properties for createRoute functions',\n      recommended: 'error',\n    },\n    messages: {\n      invalidOrder: 'Invalid order of properties for `{{function}}`.',\n    },\n    schema: [],\n    hasSuggestions: true,\n    fixable: 'code',\n  },\n  defaultOptions: [],\n\n  create: detectTanstackRouterImports((context) => {\n    return {\n      CallExpression(node) {\n        if (node.callee.type !== AST_NODE_TYPES.Identifier) {\n          return\n        }\n        const createRouteFunction = node.callee.name\n        if (!isCreateRouteFunction(createRouteFunction)) {\n          return\n        }\n        let args = node.arguments\n        if (createRouteFunctionsIndirect.includes(createRouteFunction as any)) {\n          if (node.parent.type === AST_NODE_TYPES.CallExpression) {\n            args = node.parent.arguments\n          } else {\n            return\n          }\n        }\n\n        const argument = args[0]\n        if (argument === undefined || argument.type !== 'ObjectExpression') {\n          return\n        }\n\n        const allProperties = argument.properties\n        // no need to sort if there is at max 1 property\n        if (allProperties.length < 2) {\n          return\n        }\n\n        const properties = allProperties.flatMap((p) => {\n          if (\n            p.type === AST_NODE_TYPES.Property &&\n            p.key.type === AST_NODE_TYPES.Identifier\n          ) {\n            return { name: p.key.name, property: p }\n          } else if (p.type === AST_NODE_TYPES.SpreadElement) {\n            if (p.argument.type === AST_NODE_TYPES.Identifier) {\n              return { name: p.argument.name, property: p }\n            } else {\n              throw new Error('Unsupported spread element')\n            }\n          }\n          return []\n        })\n\n        const sortedProperties = sortDataByOrder(properties, sortRules, 'name')\n        if (sortedProperties === null) {\n          return\n        }\n        context.report({\n          node: argument,\n          data: { function: node.callee.name },\n          messageId: 'invalidOrder',\n          fix(fixer) {\n            const sourceCode = context.sourceCode\n\n            const text = sortedProperties.reduce(\n              (sourceText, specifier, index) => {\n                let text = ''\n                if (index < allProperties.length - 1) {\n                  text = sourceCode\n                    .getText()\n                    .slice(\n                      allProperties[index]!.range[1],\n                      allProperties[index + 1]!.range[0],\n                    )\n                }\n                return (\n                  sourceText + sourceCode.getText(specifier.property) + text\n                )\n              },\n              '',\n            )\n            return fixer.replaceTextRange(\n              [allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],\n              text,\n            )\n          },\n        })\n      },\n    }\n  }),\n})\n"],"mappings":";;;;;;AAaA,IAAM,aAAa,yBAAA,YAAY,YAA2B,qBAAA,WAAW;AAErE,IAAM,yBAAyB,IAAI,IAAI,kBAAA,qBAAqB;AAC5D,SAAS,sBAAsB,MAAwC;AACrE,QAAO,uBAAuB,IAAI,KAAK;;AAGzC,IAAa,OAAO;AAEpB,IAAa,OAAO,WAAW;CAC7B;CACA,MAAM;EACJ,MAAM;EACN,MAAM;GACJ,aACE;GACF,aAAa;GACd;EACD,UAAU,EACR,cAAc,mDACf;EACD,QAAQ,EAAE;EACV,gBAAgB;EAChB,SAAS;EACV;CACD,gBAAgB,EAAE;CAElB,QAAQ,8BAAA,6BAA6B,YAAY;AAC/C,SAAO,EACL,eAAe,MAAM;AACnB,OAAI,KAAK,OAAO,SAAS,yBAAA,eAAe,WACtC;GAEF,MAAM,sBAAsB,KAAK,OAAO;AACxC,OAAI,CAAC,sBAAs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