Skip to content

Commit

Permalink
Rewrite findSourceMappingURLComments
Browse files Browse the repository at this point in the history
Better readability and hopefully more performant
  • Loading branch information
Yannay Livneh committed Mar 11, 2021
1 parent 7f3845a commit 67d2ed9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,26 @@ const MISSING_EXPORT_SHIM_DESCRIPTION: ExportDescription = {
};

function findSourceMappingURLComments(ast: acorn.Node, code: string): [number, number][] {
let lastStmtEnd = 0;
const ranges = [];
const ret: [number, number][] = [];

for (const stmt of (ast as GenericEsTreeNode).body) {
if (lastStmtEnd != stmt.start) {
ranges.push([lastStmtEnd, stmt.start]);
const addCommentsPos = (start: number, end: number): void => {
if (start == end) {
return;
}
lastStmtEnd = stmt.end;
}
if (lastStmtEnd != code.length) {
ranges.push([lastStmtEnd, code.length]);
}

const ret: [number, number][] = [];
for (const [start, end] of ranges) {
let sourcemappingUrlMatch;
while (sourcemappingUrlMatch = SOURCEMAPPING_URL_COMMENT_RE.exec(code.slice(start, end))) {
const interStatmentCode = code.slice(start, end);
while (sourcemappingUrlMatch = SOURCEMAPPING_URL_COMMENT_RE.exec(interStatmentCode)) {
ret.push([start + sourcemappingUrlMatch.index, start + SOURCEMAPPING_URL_COMMENT_RE.lastIndex]);
}
};

let prevStmtEnd = 0;
for (const stmt of (ast as GenericEsTreeNode).body) {
addCommentsPos(prevStmtEnd, stmt.start);
prevStmtEnd = stmt.end;
}
addCommentsPos(prevStmtEnd, code.length);

return ret;
}
Expand Down

0 comments on commit 67d2ed9

Please sign in to comment.