1 | export default function getLocation ( source, charIndex ) {
|
2 | const lines = source.split( '\n' );
|
3 | const len = lines.length;
|
4 |
|
5 | let lineStart = 0;
|
6 | let i;
|
7 |
|
8 | for ( i = 0; i < len; i += 1 ) {
|
9 | const line = lines[i];
|
10 | const lineEnd = lineStart + line.length + 1; // +1 for newline
|
11 |
|
12 | if ( lineEnd > charIndex ) {
|
13 | return { line: i + 1, column: charIndex - lineStart };
|
14 | }
|
15 |
|
16 | lineStart = lineEnd;
|
17 | }
|
18 |
|
19 | throw new Error( 'Could not determine location of character' );
|
20 | }
|