|
| 1 | +import { invariant } from '../jsutils/invariant.js'; |
| 2 | + |
1 | 3 | import type { Location } from './ast.js'; |
2 | 4 | import type { SourceLocation } from './location.js'; |
3 | 5 | import { getLocation } from './location.js'; |
@@ -35,35 +37,53 @@ export function printSourceLocation( |
35 | 37 | const locationLine = lines[lineIndex]; |
36 | 38 |
|
37 | 39 | // Special case for minified documents |
38 | | - if (locationLine.length > 120) { |
| 40 | + if (locationLine !== undefined && locationLine.length > 120) { |
39 | 41 | const subLineIndex = Math.floor(columnNum / 80); |
40 | 42 | const subLineColumnNum = columnNum % 80; |
41 | 43 | const subLines: Array<string> = []; |
42 | 44 | for (let i = 0; i < locationLine.length; i += 80) { |
43 | 45 | subLines.push(locationLine.slice(i, i + 80)); |
44 | 46 | } |
45 | 47 |
|
| 48 | + const firstSubLine = subLines[0]; |
| 49 | + const nextSubLines = subLines.slice(1, subLineIndex + 1); |
| 50 | + const nextSubLine = subLines[subLineIndex + 1]; |
| 51 | + |
| 52 | + invariant(firstSubLine !== undefined); |
| 53 | + // invariant(nextSubLine !== undefined); |
| 54 | + |
46 | 55 | return ( |
47 | 56 | locationStr + |
48 | 57 | printPrefixedLines([ |
49 | | - [`${lineNum} |`, subLines[0]], |
50 | | - ...subLines |
51 | | - .slice(1, subLineIndex + 1) |
52 | | - .map((subLine) => ['|', subLine] as const), |
| 58 | + [`${lineNum} |`, firstSubLine], |
| 59 | + ...nextSubLines.map<[string, string]>((subLine) => ['|', subLine]), |
53 | 60 | ['|', '^'.padStart(subLineColumnNum)], |
54 | | - ['|', subLines[subLineIndex + 1]], |
| 61 | + // TODO: This assertion can be removed if the above invariant is comment in. |
| 62 | + ['|', nextSubLine as string], |
55 | 63 | ]) |
56 | 64 | ); |
57 | 65 | } |
58 | 66 |
|
| 67 | + const previousLine = lines[lineIndex - 1]; |
| 68 | + const nextLine = lines[lineIndex + 1]; |
| 69 | + |
| 70 | + // TODO: With the way the types are set up, we should be able to |
| 71 | + // comment these in, but doing so breaks tests. |
| 72 | + // |
| 73 | + // invariant(previousLine !== undefined); |
| 74 | + // invariant(nextLine !== undefined); |
| 75 | + invariant(locationLine !== undefined); |
| 76 | + |
59 | 77 | return ( |
60 | 78 | locationStr + |
61 | 79 | printPrefixedLines([ |
62 | 80 | // Lines specified like this: ["prefix", "string"], |
63 | | - [`${lineNum - 1} |`, lines[lineIndex - 1]], |
| 81 | + // TODO: This assertion can be removed if the above invariant is comment in. |
| 82 | + [`${lineNum - 1} |`, previousLine as string], |
64 | 83 | [`${lineNum} |`, locationLine], |
65 | 84 | ['|', '^'.padStart(columnNum)], |
66 | | - [`${lineNum + 1} |`, lines[lineIndex + 1]], |
| 85 | + // TODO: This assertion can be removed if the above invariant is comment in. |
| 86 | + [`${lineNum + 1} |`, nextLine as string], |
67 | 87 | ]) |
68 | 88 | ); |
69 | 89 | } |
|
0 commit comments