MCPcopy Create free account
hub / github.com/babel/babel / readStringContents

Function readStringContents

packages/babel-helper-string-parser/src/index.ts:49–120  ·  view source on GitHub ↗
(
  type: "single" | "double" | "template",
  input: string,
  pos: number,
  lineStart: number,
  curLine: number,
  errors: StringContentsErrorHandlers,
)

Source from the content-addressed store, hash-verified

47};
48
49export function readStringContents(
50 type: "single" | "double" | "template",
51 input: string,
52 pos: number,
53 lineStart: number,
54 curLine: number,
55 errors: StringContentsErrorHandlers,
56) {
57 const initialPos = pos;
58 const initialLineStart = lineStart;
59 const initialCurLine = curLine;
60
61 let out = "";
62 let firstInvalidLoc = null;
63 let chunkStart = pos;
64 const { length } = input;
65 for (;;) {
66 if (pos >= length) {
67 errors.unterminated(initialPos, initialLineStart, initialCurLine);
68 out += input.slice(chunkStart, pos);
69 break;
70 }
71 const ch = input.charCodeAt(pos);
72 if (isStringEnd(type, ch, input, pos)) {
73 out += input.slice(chunkStart, pos);
74 break;
75 }
76 if (ch === charCodes.backslash) {
77 out += input.slice(chunkStart, pos);
78 const res = readEscapedChar(
79 input,
80 pos,
81 lineStart,
82 curLine,
83 type === "template",
84 errors,
85 );
86 if (res.ch === null && !firstInvalidLoc) {
87 firstInvalidLoc = { pos, lineStart, curLine };
88 } else {
89 out += res.ch;
90 }
91 ({ pos, lineStart, curLine } = res);
92 chunkStart = pos;
93 } else if (
94 ch === charCodes.lineSeparator ||
95 ch === charCodes.paragraphSeparator
96 ) {
97 ++pos;
98 ++curLine;
99 lineStart = pos;
100 } else if (ch === charCodes.lineFeed || ch === charCodes.carriageReturn) {
101 if (type === "template") {
102 out += input.slice(chunkStart, pos) + "\n";
103 ++pos;
104 if (
105 ch === charCodes.carriageReturn &&
106 input.charCodeAt(pos) === charCodes.lineFeed

Callers 3

readStringFunction · 0.90
readTemplateTokenFunction · 0.90
core.tsFile · 0.90

Calls 2

isStringEndFunction · 0.85
readEscapedCharFunction · 0.85

Tested by

no test coverage detected