Skip to content

Commit aeacf95

Browse files
committed
sourcemap-tools: add doc about regexp in DebugIdGenerator
1 parent c2887d8 commit aeacf95

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tools/sourcemap-tools/src/DebugIdGenerator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ export const SOURCE_DEBUG_ID_VARIABLE = '_btDebugIds';
22
export const SOURCE_DEBUG_ID_COMMENT = 'debugId';
33
export const SOURCEMAP_DEBUG_ID_KEY = 'debugId';
44

5+
/**
6+
* Matches leading and trailing semicolons, e.g. in `;;foo;bar;;`
7+
*/
8+
const MATCH_SEMICOLONS_REGEX = /^;+|;+$/;
9+
510
export class DebugIdGenerator {
611
public generateSourceSnippet(uuid: string) {
712
return `;!function(){try{var k="${SOURCE_DEBUG_ID_VARIABLE}",u="undefined",v="${uuid}",a=function(x){try{x[k]=x[k]||{};x[k][n]=v}catch{}},n=(new Error).stack;n&&(u!=typeof window?a(window):u);n&&(u!=typeof global?a(global):u);n&&(u!=typeof self?a(self):u);n&&(u!=typeof globalThis?a(globalThis):u)}catch{}}();`;
@@ -15,7 +20,7 @@ export class DebugIdGenerator {
1520
const replaceAll = () => source.replace(oldDebugId, newDebugId);
1621

1722
// Try to replace more safely first
18-
const oldSourceSnippet = this.generateSourceSnippet(oldDebugId).replace(/^;+|;+$/g, '');
23+
const oldSourceSnippet = this.generateSourceSnippet(oldDebugId).replace(MATCH_SEMICOLONS_REGEX, '');
1924
if (source.indexOf(oldSourceSnippet) !== -1) {
2025
source = source.replace(oldSourceSnippet, this.generateSourceSnippet(newDebugId));
2126
} else {
@@ -33,7 +38,7 @@ export class DebugIdGenerator {
3338
}
3439

3540
public hasCodeSnippet(source: string, debugId: string) {
36-
const sourceSnippet = this.generateSourceSnippet(debugId).replace(/^;+|;+$/g, '');
41+
const sourceSnippet = this.generateSourceSnippet(debugId).replace(MATCH_SEMICOLONS_REGEX, '');
3742
return source.includes(sourceSnippet);
3843
}
3944

0 commit comments

Comments
 (0)