tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

parse_imports.ts (1018B)


      1 /**
      2 * Parses all the paths of the typescript `import` statements from content
      3 * @param path the current path of the file
      4 * @param content the file content
      5 * @returns the list of import paths
      6 */
      7 export function parseImports(path: string, content: string): string[] {
      8  const out: string[] = [];
      9  const importRE = /^import\s[^'"]*(['"])([./\w]*)(\1);/gm;
     10  let importMatch: RegExpMatchArray | null;
     11  while ((importMatch = importRE.exec(content))) {
     12    const importPath = importMatch[2].replace(`'`, '').replace(`"`, '');
     13    out.push(joinPath(path, importPath));
     14  }
     15  return out;
     16 }
     17 
     18 function joinPath(a: string, b: string): string {
     19  const aParts = a.split('/');
     20  const bParts = b.split('/');
     21  aParts.pop(); // remove file
     22  let bStart = 0;
     23  while (aParts.length > 0) {
     24    switch (bParts[bStart]) {
     25      case '.':
     26        bStart++;
     27        continue;
     28      case '..':
     29        aParts.pop();
     30        bStart++;
     31        continue;
     32    }
     33    break;
     34  }
     35  return [...aParts, ...bParts.slice(bStart)].join('/');
     36 }