forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathNamesResolver.ts
More file actions
849 lines (743 loc) · 30.6 KB
/
Copy pathNamesResolver.ts
File metadata and controls
849 lines (743 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as Common from '../../core/common/common.js';
import * as Platform from '../../core/platform/platform.js';
import * as SDK from '../../core/sdk/sdk.js';
import * as Protocol from '../../generated/protocol.js';
import * as Bindings from '../bindings/bindings.js';
import * as Formatter from '../formatter/formatter.js';
import * as TextUtils from '../text_utils/text_utils.js';
import type * as Workspace from '../workspace/workspace.js';
import {scopeTreeForScript} from './ScopeTreeCache.js';
interface CachedScopeMap {
sourceMap: SDK.SourceMap.SourceMap|undefined;
mappingPromise: Promise<{variableMapping: Map<string, string>, thisMapping: string|null}>;
}
const scopeToCachedIdentifiersMap = new WeakMap<Formatter.FormatterWorkerPool.ScopeTreeNode, CachedScopeMap>();
const cachedMapByCallFrame = new WeakMap<SDK.DebuggerModel.CallFrame, Map<string, string|null>>();
export async function getTextFor(contentProvider: TextUtils.ContentProvider.ContentProvider):
Promise<TextUtils.Text.Text|null> {
const contentData = await contentProvider.requestContentData();
if (TextUtils.ContentData.ContentData.isError(contentData) || !contentData.isTextContent) {
return null;
}
return contentData.textObj;
}
export class IdentifierPositions {
name: string;
positions: Array<{lineNumber: number, columnNumber: number}>;
constructor(name: string, positions: Array<{lineNumber: number, columnNumber: number}> = []) {
this.name = name;
this.positions = positions;
}
addPosition(lineNumber: number, columnNumber: number): void {
this.positions.push({lineNumber, columnNumber});
}
}
const computeScopeTree = async function(script: SDK.Script.Script): Promise<{
scopeTree:
Formatter.FormatterWorkerPool.ScopeTreeNode, text: TextUtils.Text.Text,
}|null> {
if (!script.sourceMapURL) {
return null;
}
const text = await getTextFor(script);
if (!text) {
return null;
}
const scopeTree = await scopeTreeForScript(script);
if (!scopeTree) {
return null;
}
return {scopeTree, text};
};
/**
* @returns the scope chain from outer-most to inner-most scope where the inner-most
* scope either contains or matches the "needle".
*/
const findScopeChain = function(
scopeTree: Formatter.FormatterWorkerPool.ScopeTreeNode,
scopeNeedle: {start: number, end: number}): Formatter.FormatterWorkerPool.ScopeTreeNode[] {
if (!contains(scopeTree, scopeNeedle)) {
return [];
}
// Find the corresponding scope in the scope tree.
let containingScope = scopeTree;
const scopeChain = [scopeTree];
while (true) {
let childFound = false;
for (const child of containingScope.children) {
if (contains(child, scopeNeedle)) {
// We found a nested containing scope, continue with search there.
scopeChain.push(child);
containingScope = child;
childFound = true;
break;
}
// Sanity check: |scope| should not straddle any of the scopes in the tree. That is:
// Either |scope| is disjoint from |child| or |child| must be inside |scope|.
// (Or the |scope| is inside |child|, but that case is covered above.)
if (!disjoint(scopeNeedle, child) && !contains(scopeNeedle, child)) {
console.error('Wrong nesting of scopes');
return [];
}
}
if (!childFound) {
// We found the deepest scope in the tree that contains our scope chain entry.
break;
}
}
return scopeChain;
function contains(scope: {start: number, end: number}, candidate: {start: number, end: number}): boolean {
return (scope.start <= candidate.start) && (scope.end >= candidate.end);
}
function disjoint(scope: {start: number, end: number}, other: {start: number, end: number}): boolean {
return (scope.end <= other.start) || (other.end <= scope.start);
}
};
export async function findScopeChainForDebuggerScope(scope: SDK.DebuggerModel.ScopeChainEntry):
Promise<Formatter.FormatterWorkerPool.ScopeTreeNode[]> {
const startLocation = scope.range()?.start;
const endLocation = scope.range()?.end;
if (!startLocation || !endLocation) {
return [];
}
const script = startLocation.script();
if (!script) {
return [];
}
const scopeTreeAndText = await computeScopeTree(script);
if (!scopeTreeAndText) {
return [];
}
const {scopeTree, text} = scopeTreeAndText;
// Compute the offset within the scope tree coordinate space.
const scopeOffsets = {
start: text.offsetFromPosition(startLocation.lineNumber, startLocation.columnNumber),
end: text.offsetFromPosition(endLocation.lineNumber, endLocation.columnNumber),
};
return findScopeChain(scopeTree, scopeOffsets);
}
export const scopeIdentifiers = async function(
script: SDK.Script.Script, scope: Formatter.FormatterWorkerPool.ScopeTreeNode,
ancestorScopes: Formatter.FormatterWorkerPool.ScopeTreeNode[]): Promise<{
freeVariables:
IdentifierPositions[], boundVariables: IdentifierPositions[],
}|null> {
const text = await getTextFor(script);
if (!text) {
return null;
}
// Now we have containing scope. Collect all the scope variables.
const boundVariables = [];
const cursor = new TextUtils.TextCursor.TextCursor(text.lineEndings());
for (const variable of scope.variables) {
// Skip the fixed-kind variable (i.e., 'this' or 'arguments') if we only found their "definition"
// without any uses.
if (variable.kind === Formatter.FormatterWorkerPool.DefinitionKind.FIXED && variable.offsets.length <= 1) {
continue;
}
const identifier = new IdentifierPositions(variable.name);
for (const offset of variable.offsets) {
cursor.resetTo(offset);
identifier.addPosition(cursor.lineNumber(), cursor.columnNumber());
}
boundVariables.push(identifier);
}
// Compute free variables by collecting all the ancestor variables that are used in |containingScope|.
const freeVariables = [];
for (const ancestor of ancestorScopes) {
for (const ancestorVariable of ancestor.variables) {
let identifier = null;
for (const offset of ancestorVariable.offsets) {
if (offset >= scope.start && offset < scope.end) {
if (!identifier) {
identifier = new IdentifierPositions(ancestorVariable.name);
}
cursor.resetTo(offset);
identifier.addPosition(cursor.lineNumber(), cursor.columnNumber());
}
}
if (identifier) {
freeVariables.push(identifier);
}
}
}
return {boundVariables, freeVariables};
};
const identifierAndPunctuationRegExp = /^\s*([A-Za-z_$][A-Za-z_$0-9]*)\s*([.;,=]?)\s*$/;
const enum Punctuation {
NONE = 'none',
COMMA = 'comma',
DOT = 'dot',
SEMICOLON = 'semicolon',
EQUALS = 'equals',
}
const resolveDebuggerScope = async(scope: SDK.DebuggerModel.ScopeChainEntry):
Promise<{variableMapping: Map<string, string>, thisMapping: string | null}> => {
if (!Common.Settings.Settings.instance().moduleSetting('js-source-maps-enabled').get()) {
return {variableMapping: new Map(), thisMapping: null};
}
const script = scope.callFrame().script;
const scopeChain = await findScopeChainForDebuggerScope(scope);
return await resolveScope(script, scopeChain);
};
const resolveScope = async(script: SDK.Script.Script, scopeChain: Formatter.FormatterWorkerPool.ScopeTreeNode[]):
Promise<{variableMapping: Map<string, string>, thisMapping: string | null}> => {
const parsedScope = scopeChain[scopeChain.length - 1];
if (!parsedScope) {
return {variableMapping: new Map<string, string>(), thisMapping: null};
}
let cachedScopeMap = scopeToCachedIdentifiersMap.get(parsedScope);
const sourceMap = script.sourceMap();
if (!cachedScopeMap || cachedScopeMap.sourceMap !== sourceMap) {
const identifiersPromise =
(async () => {
const variableMapping = new Map<string, string>();
let thisMapping = null;
if (!sourceMap) {
return {variableMapping, thisMapping};
}
// Extract as much as possible from SourceMap and resolve
// missing identifier names from SourceMap ranges.
const promises: Array<Promise<void>> = [];
const resolveEntry = (id: IdentifierPositions, handler: (sourceName: string) => void): void => {
// First see if we have a source map entry with a name for the identifier.
for (const position of id.positions) {
const entry = sourceMap.findEntry(position.lineNumber, position.columnNumber);
if (entry?.name) {
handler(entry.name);
return;
}
}
// If there is no entry with the name field, try to infer the name from the source positions.
async function resolvePosition(): Promise<void> {
if (!sourceMap) {
return;
}
// Let us find the first non-empty mapping of |id| and return that. Ideally, we would
// try to compute all the mappings and only use the mapping if all the non-empty
// mappings agree. However, that can be expensive for identifiers with many uses,
// so we iterate sequentially, stopping at the first non-empty mapping.
for (const position of id.positions) {
const sourceName = await resolveSourceName(script, sourceMap, id.name, position);
if (sourceName) {
handler(sourceName);
return;
}
}
}
promises.push(resolvePosition());
};
const parsedVariables = await scopeIdentifiers(script, parsedScope, scopeChain.slice(0, -1));
if (!parsedVariables) {
return {variableMapping, thisMapping};
}
for (const id of parsedVariables.boundVariables) {
resolveEntry(id, sourceName => {
// Let use ignore 'this' mappings - those are handled separately.
if (sourceName !== 'this') {
variableMapping.set(id.name, sourceName);
}
});
}
for (const id of parsedVariables.freeVariables) {
resolveEntry(id, sourceName => {
if (sourceName === 'this') {
thisMapping = id.name;
}
});
}
await Promise.all(promises).then(getScopeResolvedForTest());
return {variableMapping, thisMapping};
})();
cachedScopeMap = {sourceMap, mappingPromise: identifiersPromise};
scopeToCachedIdentifiersMap.set(parsedScope, {sourceMap, mappingPromise: identifiersPromise});
}
return await cachedScopeMap.mappingPromise;
async function resolveSourceName(
script: SDK.Script.Script, sourceMap: SDK.SourceMap.SourceMap, name: string,
position: {lineNumber: number, columnNumber: number}): Promise<string|null> {
const ranges = sourceMap.findEntryRanges(position.lineNumber, position.columnNumber);
if (!ranges) {
return null;
}
// Extract the underlying text from the compiled code's range and make sure that
// it starts with the identifier |name|.
const uiSourceCode =
Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().uiSourceCodeForSourceMapSourceURL(
script.debuggerModel, ranges.sourceURL, script.isContentScript());
if (!uiSourceCode) {
return null;
}
const compiledText = await getTextFor(script);
if (!compiledText) {
return null;
}
const compiledToken = compiledText.extract(ranges.range);
const parsedCompiledToken = extractIdentifier(compiledToken);
if (!parsedCompiledToken) {
return null;
}
const {name: compiledName, punctuation: compiledPunctuation} = parsedCompiledToken;
if (compiledName !== name) {
return null;
}
// Extract the mapped name from the source code range and ensure that the punctuation
// matches the one from the compiled code.
const sourceText = await getTextFor(uiSourceCode);
if (!sourceText) {
return null;
}
const sourceToken = sourceText.extract(ranges.sourceRange);
const parsedSourceToken = extractIdentifier(sourceToken);
if (!parsedSourceToken) {
return null;
}
const {name: sourceName, punctuation: sourcePunctuation} = parsedSourceToken;
// Accept the source name if it is followed by the same punctuation.
if (compiledPunctuation === sourcePunctuation) {
return sourceName;
}
// Let us also allow semicolons into commas since that it is a common transformation.
if (compiledPunctuation === Punctuation.COMMA && sourcePunctuation === Punctuation.SEMICOLON) {
return sourceName;
}
return null;
function extractIdentifier(token: string): {name: string, punctuation: Punctuation}|null {
const match = token.match(identifierAndPunctuationRegExp);
if (!match) {
return null;
}
const name = match[1];
let punctuation: Punctuation|null = null;
switch (match[2]) {
case '.':
punctuation = Punctuation.DOT;
break;
case ',':
punctuation = Punctuation.COMMA;
break;
case ';':
punctuation = Punctuation.SEMICOLON;
break;
case '=':
punctuation = Punctuation.EQUALS;
break;
case '':
punctuation = Punctuation.NONE;
break;
default:
console.error(`Name token parsing error: unexpected token "${match[2]}"`);
return null;
}
return {name, punctuation};
}
}
};
export const resolveScopeChain =
async function(callFrame: SDK.DebuggerModel.CallFrame): Promise<SDK.DebuggerModel.ScopeChainEntry[]> {
const {pluginManager} = Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance();
let scopeChain: SDK.DebuggerModel.ScopeChainEntry[]|null|undefined = await pluginManager.resolveScopeChain(callFrame);
if (scopeChain) {
return scopeChain;
}
scopeChain = callFrame.script.sourceMap()?.resolveScopeChain(callFrame);
if (scopeChain) {
return scopeChain;
}
if (callFrame.script.isWasm()) {
return callFrame.scopeChain();
}
const thisObject = await resolveThisObject(callFrame);
return callFrame.scopeChain().map(scope => new ScopeWithSourceMappedVariables(scope, thisObject));
};
/**
* @returns A mapping from original name -> compiled name. If the orignal name is unavailable (e.g. because the compiled name was
* shadowed) we set it to `null`.
*/
export const allVariablesInCallFrame =
async(callFrame: SDK.DebuggerModel.CallFrame): Promise<Map<string, string|null>> => {
if (!Common.Settings.Settings.instance().moduleSetting('js-source-maps-enabled').get()) {
return new Map<string, string|null>();
}
const cachedMap = cachedMapByCallFrame.get(callFrame);
if (cachedMap) {
return cachedMap;
}
const scopeChain = callFrame.scopeChain();
const nameMappings = await Promise.all(scopeChain.map(resolveDebuggerScope));
const reverseMapping = new Map<string, string|null>();
const compiledNames = new Set<string>();
for (const {variableMapping} of nameMappings) {
for (const [compiledName, originalName] of variableMapping) {
if (!originalName) {
continue;
}
if (!reverseMapping.has(originalName)) {
// An inner scope might have shadowed {compiledName}. Mark it as "unavailable" in that case.
const compiledNameOrNull = compiledNames.has(compiledName) ? null : compiledName;
reverseMapping.set(originalName, compiledNameOrNull);
}
compiledNames.add(compiledName);
}
}
cachedMapByCallFrame.set(callFrame, reverseMapping);
return reverseMapping;
};
/**
* @returns A mapping from original name -> compiled name. If the orignal name is unavailable (e.g. because the compiled name was
* shadowed) we set it to `null`.
*/
export const allVariablesAtPosition =
async(location: SDK.DebuggerModel.Location): Promise<Map<string, string|null>> => {
const reverseMapping = new Map<string, string|null>();
if (!Common.Settings.Settings.instance().moduleSetting('js-source-maps-enabled').get()) {
return reverseMapping;
}
const script = location.script();
if (!script) {
return reverseMapping;
}
const scopeTreeAndText = await computeScopeTree(script);
if (!scopeTreeAndText) {
return reverseMapping;
}
const {scopeTree, text} = scopeTreeAndText;
const locationOffset = text.offsetFromPosition(location.lineNumber, location.columnNumber);
const scopeChain = findScopeChain(scopeTree, {start: locationOffset, end: locationOffset});
const compiledNames = new Set<string>();
while (scopeChain.length > 0) {
const {variableMapping} = await resolveScope(script, scopeChain);
for (const [compiledName, originalName] of variableMapping) {
if (!originalName) {
continue;
}
if (!reverseMapping.has(originalName)) {
// An inner scope might have shadowed {compiledName}. Mark it as "unavailable" in that case.
const compiledNameOrNull = compiledNames.has(compiledName) ? null : compiledName;
reverseMapping.set(originalName, compiledNameOrNull);
}
compiledNames.add(compiledName);
}
scopeChain.pop();
}
return reverseMapping;
};
export const resolveExpression = async(
callFrame: SDK.DebuggerModel.CallFrame, originalText: string, uiSourceCode: Workspace.UISourceCode.UISourceCode,
lineNumber: number, startColumnNumber: number, endColumnNumber: number): Promise<string> => {
if (uiSourceCode.mimeType() === 'application/wasm') {
// For WebAssembly disassembly, lookup the different possiblities.
return `memories["${originalText}"] ?? locals["${originalText}"] ?? tables["${originalText}"] ?? functions["${
originalText}"] ?? globals["${originalText}"]`;
}
if (!uiSourceCode.contentType().isFromSourceMap()) {
return '';
}
const reverseMapping = await allVariablesInCallFrame(callFrame);
if (reverseMapping.has(originalText)) {
return reverseMapping.get(originalText) as string;
}
const rawLocations =
await Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().uiLocationToRawLocations(
uiSourceCode, lineNumber, startColumnNumber);
const rawLocation = rawLocations.find(location => location.debuggerModel === callFrame.debuggerModel);
if (!rawLocation) {
return '';
}
const script = rawLocation.script();
if (!script) {
return '';
}
const sourceMap = script.sourceMap();
if (!sourceMap) {
return '';
}
const text = await getTextFor(script);
if (!text) {
return '';
}
const textRanges = sourceMap.reverseMapTextRanges(
uiSourceCode.url(),
new TextUtils.TextRange.TextRange(lineNumber, startColumnNumber, lineNumber, endColumnNumber));
if (textRanges.length !== 1) {
return '';
}
const [compiledRange] = textRanges;
const subjectText = text.extract(compiledRange);
if (!subjectText) {
return '';
}
// Map `subjectText` back to the authored code and check that the source map spits out
// `originalText` again modulo some whitespace/punctuation.
const authoredText = await getTextFor(uiSourceCode);
if (!authoredText) {
return '';
}
// Take the "start point" and the "end point - 1" of the compiled range and map them
// with the source map. Note that for "end point - 1" we need the line endings array to potentially
// move to the end of the previous line.
const startRange = sourceMap.findEntryRanges(compiledRange.startLine, compiledRange.startColumn);
const endLine = compiledRange.endColumn === 0 ? compiledRange.endLine - 1 : compiledRange.endLine;
const endColumn = compiledRange.endColumn === 0 ? text.lineEndings()[endLine] : compiledRange.endColumn - 1;
const endRange = sourceMap.findEntryRanges(endLine, endColumn);
if (!startRange || !endRange) {
return '';
}
// Merge `startRange` with `endRange`. This might not be 100% correct if there are interleaved ranges inbetween.
const mappedAuthoredText = authoredText.extract(new TextUtils.TextRange.TextRange(
startRange.sourceRange.startLine, startRange.sourceRange.startColumn, endRange.sourceRange.endLine,
endRange.sourceRange.endColumn));
// Check that what we found after applying the source map roughly matches `originalText`.
const originalTextRegex = new RegExp(`^[\\s,;]*${Platform.StringUtilities.escapeForRegExp(originalText)}`, 'g');
if (!originalTextRegex.test(mappedAuthoredText)) {
return '';
}
return await Formatter.FormatterWorkerPool.formatterWorkerPool().evaluatableJavaScriptSubstring(subjectText);
};
export const resolveThisObject =
async(callFrame: SDK.DebuggerModel.CallFrame): Promise<SDK.RemoteObject.RemoteObject|null> => {
const scopeChain = callFrame.scopeChain();
if (scopeChain.length === 0) {
return callFrame.thisObject();
}
const {thisMapping} = await resolveDebuggerScope(scopeChain[0]);
if (!thisMapping) {
return callFrame.thisObject();
}
const result = await callFrame.evaluate(({
expression: thisMapping,
objectGroup: 'backtrace',
includeCommandLineAPI: false,
silent: true,
returnByValue: false,
generatePreview: true,
}));
if ('exceptionDetails' in result) {
return !result.exceptionDetails && result.object ? result.object : callFrame.thisObject();
}
return null;
};
export const resolveScopeInObject = function(scope: SDK.DebuggerModel.ScopeChainEntry): SDK.RemoteObject.RemoteObject {
const endLocation = scope.range()?.end;
const startLocationScript = scope.range()?.start.script() ?? null;
if (scope.type() === Protocol.Debugger.ScopeType.Global || !startLocationScript || !endLocation ||
!startLocationScript.sourceMapURL) {
return scope.object();
}
return new RemoteObject(scope);
};
/**
* Wraps a debugger `Scope` but returns a scope object where variable names are
* mapped to their authored name.
*
* This implementation does not utilize source map "Scopes" information but obtains
* original variable names via parsing + mappings + names.
*/
class ScopeWithSourceMappedVariables implements SDK.DebuggerModel.ScopeChainEntry {
readonly #debuggerScope: SDK.DebuggerModel.ScopeChainEntry;
/** The resolved `this` of the current call frame */
readonly #thisObject: SDK.RemoteObject.RemoteObject|null;
constructor(scope: SDK.DebuggerModel.ScopeChainEntry, thisObject: SDK.RemoteObject.RemoteObject|null) {
this.#debuggerScope = scope;
this.#thisObject = thisObject;
}
callFrame(): SDK.DebuggerModel.CallFrame {
return this.#debuggerScope.callFrame();
}
type(): string {
return this.#debuggerScope.type();
}
typeName(): string {
return this.#debuggerScope.typeName();
}
name(): string|undefined {
return this.#debuggerScope.name();
}
range(): SDK.DebuggerModel.LocationRange|null {
return this.#debuggerScope.range();
}
object(): SDK.RemoteObject.RemoteObject {
return resolveScopeInObject(this.#debuggerScope);
}
description(): string {
return this.#debuggerScope.description();
}
icon(): string|undefined {
return this.#debuggerScope.icon();
}
extraProperties(): SDK.RemoteObject.RemoteObjectProperty[] {
const extraProperties = this.#debuggerScope.extraProperties();
if (this.#thisObject && this.type() === Protocol.Debugger.ScopeType.Local) {
extraProperties.unshift(new SDK.RemoteObject.RemoteObjectProperty(
'this', this.#thisObject, undefined, undefined, undefined, undefined, undefined, /* synthetic */ true));
}
return extraProperties;
}
}
export class RemoteObject extends SDK.RemoteObject.RemoteObject {
private readonly scope: SDK.DebuggerModel.ScopeChainEntry;
private readonly object: SDK.RemoteObject.RemoteObject;
constructor(scope: SDK.DebuggerModel.ScopeChainEntry) {
super();
this.scope = scope;
this.object = scope.object();
}
override customPreview(): Protocol.Runtime.CustomPreview|null {
return this.object.customPreview();
}
override get objectId(): Protocol.Runtime.RemoteObjectId|undefined {
return this.object.objectId;
}
override get type(): string {
return this.object.type;
}
override get subtype(): string|undefined {
return this.object.subtype;
}
override get value(): typeof this.object.value {
return this.object.value;
}
override get description(): string|undefined {
return this.object.description;
}
override get hasChildren(): boolean {
return this.object.hasChildren;
}
override get preview(): Protocol.Runtime.ObjectPreview|undefined {
return this.object.preview;
}
override arrayLength(): number {
return this.object.arrayLength();
}
override getOwnProperties(generatePreview: boolean): Promise<SDK.RemoteObject.GetPropertiesResult> {
return this.object.getOwnProperties(generatePreview);
}
override async getAllProperties(accessorPropertiesOnly: boolean, generatePreview: boolean):
Promise<SDK.RemoteObject.GetPropertiesResult> {
const allProperties = await this.object.getAllProperties(accessorPropertiesOnly, generatePreview);
const {variableMapping} = await resolveDebuggerScope(this.scope);
const properties = allProperties.properties;
const internalProperties = allProperties.internalProperties;
const newProperties = properties?.map(property => {
const name = variableMapping.get(property.name);
return name !== undefined ? property.cloneWithNewName(name) : property;
});
return {properties: newProperties ?? [], internalProperties};
}
override async setPropertyValue(argumentName: string|Protocol.Runtime.CallArgument, value: string):
Promise<string|undefined> {
const {variableMapping} = await resolveDebuggerScope(this.scope);
let name;
if (typeof argumentName === 'string') {
name = argumentName;
} else {
name = (argumentName.value as string);
}
let actualName: string = name;
for (const compiledName of variableMapping.keys()) {
if (variableMapping.get(compiledName) === name) {
actualName = compiledName;
break;
}
}
return await this.object.setPropertyValue(actualName, value);
}
override async deleteProperty(name: Protocol.Runtime.CallArgument): Promise<string|undefined> {
return await this.object.deleteProperty(name);
}
override callFunction<T, U>(
functionDeclaration: (this: U, ...args: any[]) => T,
args?: Protocol.Runtime.CallArgument[]): Promise<SDK.RemoteObject.CallFunctionResult> {
return this.object.callFunction(functionDeclaration, args);
}
override callFunctionJSON<T, U>(
functionDeclaration: (this: U, ...args: any[]) => T, args?: Protocol.Runtime.CallArgument[]): Promise<T> {
return this.object.callFunctionJSON(functionDeclaration, args);
}
override release(): void {
this.object.release();
}
override debuggerModel(): SDK.DebuggerModel.DebuggerModel {
return this.object.debuggerModel();
}
override runtimeModel(): SDK.RuntimeModel.RuntimeModel {
return this.object.runtimeModel();
}
override isNode(): boolean {
return this.object.isNode();
}
}
// Resolve the frame's function name using the name associated with the opening
// paren that starts the scope. If there is no name associated with the scope
// start or if the function scope does not start with a left paren (e.g., arrow
// function with one parameter), the resolution returns null.
async function getFunctionNameFromScopeStart(
script: SDK.Script.Script, lineNumber: number, columnNumber: number): Promise<string|null> {
// To reduce the overhead of resolving function names,
// we check for source maps first and immediately leave
// this function if the script doesn't have a sourcemap.
const sourceMap = script.sourceMap();
if (!sourceMap) {
return null;
}
const scopeName = sourceMap.findOriginalFunctionName({line: lineNumber, column: columnNumber});
if (scopeName !== null) {
return scopeName;
}
const mappingEntry = sourceMap.findEntry(lineNumber, columnNumber);
if (!mappingEntry || !mappingEntry.sourceURL) {
return null;
}
const name = mappingEntry.name;
if (!name) {
return null;
}
const text = await getTextFor(script);
if (!text) {
return null;
}
const openRange = new TextUtils.TextRange.TextRange(lineNumber, columnNumber, lineNumber, columnNumber + 1);
if (text.extract(openRange) !== '(') {
return null;
}
return name;
}
export async function resolveDebuggerFrameFunctionName(frame: SDK.DebuggerModel.CallFrame): Promise<string|null> {
const startLocation = frame.localScope()?.range()?.start;
if (!startLocation) {
return null;
}
return await getFunctionNameFromScopeStart(frame.script, startLocation.lineNumber, startLocation.columnNumber);
}
export async function resolveProfileFrameFunctionName(
{scriptId, lineNumber, columnNumber}: Partial<Protocol.Runtime.CallFrame>,
target: SDK.Target.Target|null): Promise<string|null> {
if (!target || lineNumber === undefined || columnNumber === undefined || scriptId === undefined) {
return null;
}
const debuggerModel = target.model(SDK.DebuggerModel.DebuggerModel);
const script = debuggerModel?.scriptForId(String(scriptId));
if (!debuggerModel || !script) {
return null;
}
const debuggerWorkspaceBinding = Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance();
const location = new SDK.DebuggerModel.Location(debuggerModel, scriptId, lineNumber, columnNumber);
const functionInfoFromPlugin = await debuggerWorkspaceBinding.pluginManager.getFunctionInfo(script, location);
if (functionInfoFromPlugin && 'frames' in functionInfoFromPlugin) {
const last = functionInfoFromPlugin.frames.at(-1);
if (last?.name) {
return last.name;
}
}
return await getFunctionNameFromScopeStart(script, lineNumber, columnNumber);
}
let scopeResolvedForTest: (...arg0: unknown[]) => void = function(): void {};
export const getScopeResolvedForTest = (): (...arg0: unknown[]) => void => {
return scopeResolvedForTest;
};
export const setScopeResolvedForTest = (scope: (...arg0: unknown[]) => void): void => {
scopeResolvedForTest = scope;
};