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

Function splitTwoslashCodeInfoFiles

scripts/parser-tests/typescript/index.js:150–177  ·  view source on GitHub ↗

* Split a code string into multiple files based on twoslash `//` comments. * @param {string} code * @param {string} defaultFileName * @param {string} root * @returns {Array<[string, string[]]>}

(code, defaultFileName, root = "")

Source from the content-addressed store, hash-verified

148 * @returns {Array<[string, string[]]>}
149 */
150function splitTwoslashCodeInfoFiles(code, defaultFileName, root = "") {
151 const lines = code.split(/\r\n?|\n/);
152
153 let nameForFile = code.includes(`@filename: ${defaultFileName}`)
154 ? "global.ts"
155 : defaultFileName;
156 let currentFileContent = [];
157 /**
158 * @type {Array<[string, string[]]>}
159 */
160 const fileMap = [];
161
162 for (const line of lines) {
163 const newFileName = BracketedFileRegExp.test(line)
164 ? // @ts-expect-error checked above
165 line.match(BracketedFileRegExp)[1]
166 : (line.match(AtFileRegExp)?.[1] ?? false);
167 if (newFileName) {
168 fileMap.push([root + nameForFile, currentFileContent]);
169 nameForFile = newFileName;
170 currentFileContent = [];
171 } else {
172 currentFileContent.push(line);
173 }
174 }
175 fileMap.push([root + nameForFile, currentFileContent]);
176 return fileMap;
177}
178
179runner.run().catch(err => {
180 console.error(err);

Callers 1

toFilesFunction · 0.85

Calls 3

matchMethod · 0.80
testMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected