MCPcopy Index your code
hub / github.com/TypeScriptToLua/TypeScriptToLua / lineAndColumnOf

Function lineAndColumnOf

test/unit/printer/utils.ts:5–25  ·  view source on GitHub ↗
(text: string, pattern: string)

Source from the content-addressed store, hash-verified

3import { Position } from "source-map";
4
5export function lineAndColumnOf(text: string, pattern: string): Position {
6 const pos = text.indexOf(pattern);
7 if (pos === -1) {
8 return { line: -1, column: -1 };
9 }
10
11 const lineLengths = text.split("\n").map(s => s.length);
12
13 let totalPos = 0;
14 for (let line = 1; line <= lineLengths.length; line++) {
15 // Add + 1 for the removed \n
16 const lineLength = lineLengths[line - 1] + 1;
17 if (pos < totalPos + lineLength) {
18 return { line, column: pos - totalPos };
19 }
20
21 totalPos += lineLengths[line - 1] + 1;
22 }
23
24 return { line: -1, column: -1 };
25}

Callers 2

bundle.spec.tsFile · 0.90
sourcemaps.spec.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected