{"version":3,"sources":["../../src/assertions/parseTwoslashAssertion.ts"],"sourcesContent":["import { SyntaxError, TwoSlashAssertion } from \"./types.js\";\n\nexport function parseTwoslashAssertion(\n\tcomment: string,\n\tcommentIndex: number,\n\tcommentLine: number,\n\tsourceText: string,\n\tlineStarts: readonly number[],\n): null | SyntaxError | TwoSlashAssertion {\n\tconst matchTwoslash = /^( *)\\^\\?(.*)$/.exec(comment) as\n\t\t| [never, string, string]\n\t\t| null;\n\tif (!matchTwoslash) {\n\t\treturn null;\n\t}\n\n\tconst whitespace = matchTwoslash[1];\n\tconst rawPayload = matchTwoslash[2];\n\tif (rawPayload.length && !rawPayload.startsWith(\" \")) {\n\t\t// This is an error: there must be a space after the ^?\n\t\treturn {\n\t\t\tline: commentLine - 1,\n\t\t\ttype: \"InvalidTwoslash\",\n\t\t};\n\t}\n\n\tlet expected = rawPayload.slice(1); // strip leading space, or leave it as \"\".\n\tif (commentLine === 1) {\n\t\t// This will become an attachment error later.\n\t\treturn {\n\t\t\tassertionType: \"twoslash\",\n\t\t\texpected,\n\t\t\texpectedPrefix: \"\",\n\t\t\texpectedRange: [-1, -1],\n\t\t\tinsertSpace: false,\n\t\t\tposition: -1,\n\t\t};\n\t}\n\n\t// The position of interest is wherever the \"^\" (caret) is, but on the previous line.\n\tconst caretIndex = commentIndex + whitespace.length + 2; // 2 = length of \"//\"\n\tconst position =\n\t\tcaretIndex - (lineStarts[commentLine - 1] - lineStarts[commentLine - 2]);\n\n\tconst expectedRange: [number, number] = [\n\t\tcommentIndex + whitespace.length + 5,\n\t\tcommentLine < lineStarts.length\n\t\t\t? lineStarts[commentLine] - 1\n\t\t\t: sourceText.length,\n\t];\n\t// Peak ahead to the next lines to see if the expected type continues\n\tconst expectedPrefix =\n\t\tsourceText.slice(\n\t\t\tlineStarts[commentLine - 1],\n\t\t\tcommentIndex + 2 + whitespace.length,\n\t\t) + \"   \";\n\tfor (let nextLine = commentLine; nextLine < lineStarts.length; nextLine++) {\n\t\tconst thisLineEnd =\n\t\t\tnextLine + 1 < lineStarts.length\n\t\t\t\t? lineStarts[nextLine + 1] - 1\n\t\t\t\t: sourceText.length;\n\t\tconst lineText = sourceText.slice(lineStarts[nextLine], thisLineEnd + 1);\n\t\tif (lineText.startsWith(expectedPrefix)) {\n\t\t\tif (nextLine === commentLine) {\n\t\t\t\texpected += \"\\n\";\n\t\t\t}\n\n\t\t\texpected += lineText.slice(expectedPrefix.length);\n\t\t\texpectedRange[1] = thisLineEnd;\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tlet insertSpace = false;\n\tif (expectedRange[0] > expectedRange[1]) {\n\t\t// this happens if the line ends with \"^?\" and nothing else\n\t\texpectedRange[0] = expectedRange[1];\n\t\tinsertSpace = true;\n\t}\n\n\treturn {\n\t\tassertionType: \"twoslash\",\n\t\texpected,\n\t\texpectedPrefix,\n\t\texpectedRange,\n\t\tinsertSpace,\n\t\tposition,\n\t};\n}\n"],"mappings":";AAEO,SAAS,uBACf,SACA,cACA,aACA,YACA,YACyC;AACzC,QAAM,gBAAgB,iBAAiB,KAAK,OAAO;AAGnD,MAAI,CAAC,eAAe;AACnB,WAAO;AAAA,EACR;AAEA,QAAM,aAAa,cAAc,CAAC;AAClC,QAAM,aAAa,cAAc,CAAC;AAClC,MAAI,WAAW,UAAU,CAAC,WAAW,WAAW,GAAG,GAAG;AAErD,WAAO;AAAA,MACN,MAAM,cAAc;AAAA,MACpB,MAAM;AAAA,IACP;AAAA,EACD;AAEA,MAAI,WAAW,WAAW,MAAM,CAAC;AACjC,MAAI,gBAAgB,GAAG;AAEtB,WAAO;AAAA,MACN,eAAe;AAAA,MACf;AAAA,MACA,gBAAgB;AAAA,MAChB,eAAe,CAAC,IAAI,EAAE;AAAA,MACtB,aAAa;AAAA,MACb,UAAU;AAAA,IACX;AAAA,EACD;AAGA,QAAM,aAAa,eAAe,WAAW,SAAS;AACtD,QAAM,WACL,cAAc,WAAW,cAAc,CAAC,IAAI,WAAW,cAAc,CAA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