Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 137 additions & 3 deletions packages/angular/build/src/utils/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@

/**
* Removes `//# sourceMappingURL=` comments safely from the given JavaScript code,
* ignoring any occurrences that are inside string literals, template literals, or block comments.
* ignoring any occurrences that are inside string literals, template literals, regex literals,
* or block comments.
*
* For raw Uint8Array / Buffer inputs, it optimizes performance by inspecting trailing byte sequences
* to slice the buffer directly without full string decoding.
Expand Down Expand Up @@ -93,7 +94,60 @@
return stripped === text ? code : Buffer.from(stripped, 'utf-8');
}

const REGEX_PRECEDING_KEYWORDS = new Set([
'await',
'case',
'delete',
'do',
'else',
'in',
'instanceof',
'new',
'of',
'return',
'throw',
'typeof',
'void',
'yield',
]);
Comment thread
alan-agius4 marked this conversation as resolved.
Resolved

const REGEX_PRECEDING_PUNCTUATION = new Set([
'(',
',',
'=',
':',
'[',
'!',
'&',
'|',
'?',
'{',
'}',
';',
'+',
'-',
'*',
'%',
'~',
'^',
'<',
'>',
'/',
]);

function isIdentifierChar(char: string): boolean {
const code = char.charCodeAt(0);

return (
(code >= 97 && code <= 122) || // a-z
(code >= 65 && code <= 90) || // A-Z
(code >= 48 && code <= 57) || // 0-9
code === 95 || // _
code === 36 // $
);
Comment thread
alan-agius4 marked this conversation as resolved.
Resolved
}

function removeSourceMappingURLFromString(code: string): string {

Check failure on line 150 in packages/angular/build/src/utils/source-map.ts

View workflow job for this annotation

GitHub Actions / lint

Function 'removeSourceMappingURLFromString' has too many lines (242). Maximum allowed is 200
if (!code.includes(SOURCEMAP_COMMENT_PREFIX)) {
return code;
}
Expand All @@ -106,8 +160,15 @@
// Stack to track template literal state and curly brace depth for nested interpolations.
const stack: { type: 'template'; braceDepth: number }[] = [];
let currentState:
'normal' | 'string_double' | 'string_single' | 'template' | 'comment_block' | 'comment_line' =
'normal';
| 'normal'
| 'string_double'
| 'string_single'
| 'template'
| 'regex'
| 'comment_block'
| 'comment_line' = 'normal';
let regexAllowed = true;
let inRegexCharClass = false;

while (i < len) {
const char = code[i];
Expand Down Expand Up @@ -156,17 +217,20 @@
}
if (char === '"') {
currentState = 'string_double';
regexAllowed = false;
i++;
continue;
}
if (char === "'") {
currentState = 'string_single';
regexAllowed = false;
i++;
continue;
}
if (char === '`') {
currentState = 'template';
stack.push({ type: 'template', braceDepth: 0 });
regexAllowed = false;
i++;
continue;
}
Expand All @@ -175,6 +239,7 @@
if (top) {
top.braceDepth++;
}
regexAllowed = true;
i++;
continue;
}
Expand All @@ -186,14 +251,53 @@
// Exiting a template literal interpolation ${ ... }
stack.pop();
currentState = 'template';
regexAllowed = false;
i++;
continue;
}
}
regexAllowed = true;
i++;
continue;
}
if (char === '/') {
Comment thread
alan-agius4 marked this conversation as resolved.
Resolved
if (regexAllowed) {
currentState = 'regex';
inRegexCharClass = false;
regexAllowed = false;
} else {
regexAllowed = true;
}
i++;
continue;
}
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
i++;
continue;
}
if (isIdentifierChar(char)) {
const start = i;
i++;
while (i < len && isIdentifierChar(code[i])) {
i++;
}
const word = code.slice(start, i);
let prevIdx = start - 1;
while (
prevIdx >= 0 &&
(code[prevIdx] === ' ' ||
code[prevIdx] === '\t' ||
code[prevIdx] === '\n' ||
code[prevIdx] === '\r')
) {
prevIdx--;
}
regexAllowed =
(prevIdx < 0 || code[prevIdx] !== '.') && REGEX_PRECEDING_KEYWORDS.has(word);
continue;
}

regexAllowed = REGEX_PRECEDING_PUNCTUATION.has(char);
i++;
} else if (currentState === 'string_double') {
if (char === '\\') {
Expand All @@ -202,6 +306,7 @@
}
if (char === '"') {
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'string_single') {
Expand All @@ -211,6 +316,7 @@
}
if (char === "'") {
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'template') {
Expand All @@ -222,12 +328,40 @@
// Entering template literal interpolation context
currentState = 'normal';
stack.push({ type: 'template', braceDepth: 0 });
regexAllowed = true;
i += 2;
continue;
}
if (char === '`') {
stack.pop();
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'regex') {
if (char === '\\') {
if (nextChar === '\n' || nextChar === '\r') {
currentState = 'normal';
inRegexCharClass = false;
regexAllowed = true;
}
i += 2;
continue;
}
if (char === '\n' || char === '\r') {
currentState = 'normal';
inRegexCharClass = false;
regexAllowed = true;
i++;
continue;
}
if (char === '[') {
inRegexCharClass = true;
} else if (char === ']' && inRegexCharClass) {
inRegexCharClass = false;
} else if (char === '/' && !inRegexCharClass) {
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'comment_block') {
Expand Down
53 changes: 53 additions & 0 deletions packages/angular/build/src/utils/source-map_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,64 @@ describe('removeSourceMappingURL', () => {
expect(removeSourceMappingURL(code)).toBe(code);
});

it('should not corrupt strings following a regex literal containing a double quote', () => {
const code = 'const pattern = /"/; const text = "//# sourceMappingURL=not-a-comment";';
expect(removeSourceMappingURL(code)).toBe(code);
});

it('should not corrupt strings following a regex literal containing single quotes or backticks', () => {
const singleQuoteCode =
"const pattern = /'/; const text = '//# sourceMappingURL=not-a-comment';";
expect(removeSourceMappingURL(singleQuoteCode)).toBe(singleQuoteCode);

const backtickCode = 'const pattern = /`/; const text = `//# sourceMappingURL=not-a-comment`;';
expect(removeSourceMappingURL(backtickCode)).toBe(backtickCode);
});

it('should handle regex character classes containing slashes and quotes', () => {
const code =
'const pattern = /[/"\'`]/; const text = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe(
'const pattern = /[/"\'`]/; const text = "//# sourceMappingURL=not-a-comment";\n',
);

const charClassComment = 'const re = /[//# sourceMappingURL=foo.js.map]/;';
expect(removeSourceMappingURL(charClassComment)).toBe(charClassComment);
});

it('should handle escaped slashes, brackets, and quotes inside regex literals', () => {
const code =
'const pattern = /\\/"\\[/; const text = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe(
'const pattern = /\\/"\\[/; const text = "//# sourceMappingURL=not-a-comment";\n',
);

const escapedBracketInClass =
'const pattern = /[\\]/"\']/; const text = "//# sourceMappingURL=not-a-comment";';
expect(removeSourceMappingURL(escapedBracketInClass)).toBe(escapedBracketInClass);
});

it('should handle regex literals after keywords', () => {
const code =
'function test() { return /"/; }\nconst text = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe(
'function test() { return /"/; }\nconst text = "//# sourceMappingURL=not-a-comment";\n',
);
});

it('should not affect normal division operators', () => {
const code = 'const ratio = 10 / 2;\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe('const ratio = 10 / 2;\n');
});

it('should handle division expressions followed by strings and comments', () => {
const code =
'const x = a / b; const y = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe(
'const x = a / b; const y = "//# sourceMappingURL=not-a-comment";\n',
);
});

it('should only remove exact sourceMappingURL prefix comments', () => {
const code = '// # sourceMappingURL=main.js.map\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe('// # sourceMappingURL=main.js.map\n');
Expand Down
Loading