{"version":3,"sources":["../../src/failures/normalizedTypeToString.ts"],"sourcesContent":["// Code based on DefinitelyTyped-tools implementation:\n// https://github.com/microsoft/DefinitelyTyped-tools/blob/42484ff245f6f18018de729f12c9a28436daa08a/packages/eslint-plugin/src/rules/expect.ts#L466\n\nimport type ts from \"typescript\";\n\nimport { TSModule } from \"../utils/programs.js\";\n\nexport function normalizedTypeToString(type: string, tsModule: TSModule) {\n\tconst sourceFile = tsModule.createSourceFile(\n\t\t\"foo.ts\",\n\t\t`declare var x: ${type};`,\n\t\ttsModule.ScriptTarget.Latest,\n\t);\n\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\tconst typeNode = (sourceFile.statements[0] as ts.VariableStatement)\n\t\t.declarationList.declarations[0].type!;\n\n\tconst printer = tsModule.createPrinter();\n\tfunction print(node: ts.Node) {\n\t\treturn printer.printNode(tsModule.EmitHint.Unspecified, node, sourceFile);\n\t}\n\n\t// TODO: pass undefined instead once all supported TS versions support it per:\n\t// https://github.com/microsoft/TypeScript/pull/52941\n\tconst context = tsModule.nullTransformationContext;\n\n\tfunction visit(node: ts.Node) {\n\t\tnode = tsModule.visitEachChild(node, visit, context);\n\n\t\tif (tsModule.isUnionTypeNode(node)) {\n\t\t\tconst types = node.types\n\t\t\t\t.map((t) => [t, print(t)] as const)\n\t\t\t\t.sort((a, b) => (a[1] < b[1] ? -1 : 1))\n\t\t\t\t.map((t) => t[0]);\n\t\t\treturn tsModule.factory.updateUnionTypeNode(\n\t\t\t\tnode,\n\t\t\t\ttsModule.factory.createNodeArray(types),\n\t\t\t);\n\t\t}\n\n\t\tif (\n\t\t\ttsModule.isTypeOperatorNode(node) &&\n\t\t\tnode.operator === tsModule.SyntaxKind.ReadonlyKeyword &&\n\t\t\ttsModule.isArrayTypeNode(node.type)\n\t\t) {\n\t\t\t// It's possible that this would conflict with a library which defines their own type with this name,\n\t\t\t// but that's unlikely (and was not previously handled in a prior revision of type string normalization).\n\t\t\treturn tsModule.factory.createTypeReferenceNode(\"ReadonlyArray\", [\n\t\t\t\tskipTypeParentheses(node.type.elementType),\n\t\t\t]);\n\t\t}\n\n\t\treturn node;\n\t}\n\n\tconst visited = visit(typeNode);\n\treturn print(visited);\n\n\tfunction skipTypeParentheses(node: ts.TypeNode): ts.TypeNode {\n\t\twhile (tsModule.isParenthesizedTypeNode(node)) {\n\t\t\tnode = node.type;\n\t\t}\n\t\treturn node;\n\t}\n}\n"],"mappings":";AAOO,SAAS,uBAAuB,MAAc,UAAoB;AACxE,QAAM,aAAa,SAAS;AAAA,IAC3B;AAAA,IACA,kBAAkB,IAAI;AAAA,IACtB,SAAS,aAAa;AAAA,EACvB;AAEA,QAAM,WAAY,WAAW,WAAW,CAAC,EACvC,gBAAgB,aAAa,CAAC,EAAE;AAElC,QAAM,UAAU,SAAS,cAAc;AACvC,WAAS,MAAM,MAAe;AAC7B,WAAO,QAAQ,UAAU,SAAS,SAAS,aAAa,MAAM,UAAU;AAAA,EACzE;AAIA,QAAM,UAAU,SAAS;AAEzB,WAAS,MAAM,MAAe;AAC7B,WAAO,SAAS,eAAe,MAAM,OAAO,OAAO;AAEnD,QAAI,SAAS,gBAAgB,IAAI,GAAG;AACnC,YAAM,QAAQ,KAAK,MACjB,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAU,EACjC,KAAK,CAAC,GAAG,MAAO,EAAE,CAAC,IAAI,EAAE,CAAC,IAAI,KAAK,CAAE,EACrC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;AACjB,aAAO,SAAS,QAAQ;AAAA,QACvB;AAAA,QACA,SAAS,QAAQ,gBAAgB,KAAK;AAAA,MACvC;AAAA,IACD;AAEA,QACC,SAAS,mBAAmB,IAAI,KAChC,KAAK,aAAa,SAAS,WAAW,mBACtC,SAAS,gBAAgB,KAAK,IAAI,GACjC;AAGD,aAAO,SAAS,QAAQ,wBAAwB,iBAAiB;AAAA,QAChE,oBAAoB,KAAK,KAAK,WAAW;AAAA,MAC1C,CAAC;AAAA,IACF;AAEA,WAAO;AAAA,EACR;AAEA,QAAM,UAAU,MAAM,QAAQ;AAC9B,SAAO,MAAM,OAAO;AAEpB,WAAS,oBAAoB,MAAgC;AAC5D,WAAO,SAAS,wBAAwB,IAAI,GAAG;AAC9C,aAAO,KAAK;AAAA,IACb;AACA,WAAO;AAAA,EACR;AACD;","names":[]}