chrome-devtools-frontend 1.0.974575 → 1.0.975056

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -666,6 +666,7 @@ grd_files_debug_sources = [
666
666
  "front_end/entrypoints/formatter_worker/JSONFormatter.js",
667
667
  "front_end/entrypoints/formatter_worker/JavaScriptFormatter.js",
668
668
  "front_end/entrypoints/formatter_worker/JavaScriptOutline.js",
669
+ "front_end/entrypoints/formatter_worker/Substitute.js",
669
670
  "front_end/entrypoints/heap_snapshot_worker/AllocationProfile.js",
670
671
  "front_end/entrypoints/heap_snapshot_worker/HeapSnapshot.js",
671
672
  "front_end/entrypoints/heap_snapshot_worker/HeapSnapshotLoader.js",
@@ -42,6 +42,7 @@ import {HTMLFormatter} from './HTMLFormatter.js';
42
42
  import {IdentityFormatter} from './IdentityFormatter.js';
43
43
  import {JavaScriptFormatter} from './JavaScriptFormatter.js';
44
44
  import {JSONFormatter} from './JSONFormatter.js';
45
+ import {computeSubstitution, applySubstitution} from './Substitute.js';
45
46
 
46
47
  export interface Chunk {
47
48
  // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
@@ -304,3 +305,5 @@ export function argumentsList(content: string): string[] {
304
305
  console.error = (): undefined => undefined;
305
306
  }
306
307
  })();
308
+
309
+ export {applySubstitution, computeSubstitution};
@@ -0,0 +1,536 @@
1
+ // Copyright 2022 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import * as Acorn from '../../third_party/acorn/acorn.js';
6
+
7
+ import {ECMA_VERSION} from './AcornTokenizer.js';
8
+
9
+ export interface Replacement {
10
+ from: string;
11
+ to: string;
12
+ offset: number;
13
+ isShorthandAssignmentProperty: boolean;
14
+ }
15
+
16
+ // Given an |expression| and a mapping from names to new names, the |computeSubstitution|
17
+ // function returns a list of replacements sorted by the offset. The function throws if
18
+ // it cannot parse the expression or the substitution is impossible to perform (for example
19
+ // if the substitution target is 'this' within a function, it would become bound there).
20
+ export function computeSubstitution(expression: string, nameMap: Map<string, string>): Replacement[] {
21
+ // Parse the expression and find variables and scopes.
22
+ const root = Acorn.parse(expression, {ecmaVersion: ECMA_VERSION, allowAwaitOutsideFunction: true, ranges: false}) as
23
+ Acorn.ESTree.Node;
24
+ const scopeVariables = new ScopeVariableAnalysis();
25
+ scopeVariables.processNode(root);
26
+ const freeVariables = scopeVariables.getFreeVariables();
27
+ const result: Replacement[] = [];
28
+
29
+ // Prepare the machinery for generating fresh names (to avoid variable captures).
30
+ const allNames = scopeVariables.getAllNames();
31
+ for (const rename of nameMap.values()) {
32
+ allNames.add(rename);
33
+ }
34
+ function getNewName(base: string): string {
35
+ let i = 1;
36
+ while (allNames.has(`${base}_${i}`)) {
37
+ i++;
38
+ }
39
+ const newName = `${base}_${i}`;
40
+ allNames.add(newName);
41
+ return newName;
42
+ }
43
+
44
+ // Perform the substitutions.
45
+ for (const [name, rename] of nameMap.entries()) {
46
+ const defUse = freeVariables.get(name);
47
+ if (!defUse) {
48
+ continue;
49
+ }
50
+
51
+ const binders = [];
52
+ for (const use of defUse) {
53
+ result.push({
54
+ from: name,
55
+ to: rename,
56
+ offset: use.offset,
57
+ isShorthandAssignmentProperty: use.isShorthandAssignmentProperty,
58
+ });
59
+ binders.push(...use.scope.findBinders(rename));
60
+ }
61
+ // If there is a capturing binder, rename the bound variable.
62
+ for (const binder of binders) {
63
+ if (binder.definitionKind === DefinitionKind.Fixed) {
64
+ // If the identifier is bound to a fixed name, such as 'this',
65
+ // then refuse to do the substitution.
66
+ throw new Error(`Cannot avoid capture of '${rename}'`);
67
+ }
68
+ const newName = getNewName(rename);
69
+ for (const use of binder.uses) {
70
+ result.push({
71
+ from: rename,
72
+ to: newName,
73
+ offset: use.offset,
74
+ isShorthandAssignmentProperty: use.isShorthandAssignmentProperty,
75
+ });
76
+ }
77
+ }
78
+ }
79
+ result.sort((l, r) => l.offset - r.offset);
80
+ return result;
81
+ }
82
+
83
+ export function applySubstitution(expression: string, replacements: Replacement[]): string {
84
+ const accumulator = [];
85
+ let last = 0;
86
+ for (const r of replacements) {
87
+ accumulator.push(expression.slice(last, r.offset));
88
+ let replacement = r.to;
89
+ if (r.isShorthandAssignmentProperty) {
90
+ // Let us expand the shorthand to full assignment.
91
+ replacement = `${r.from}: ${r.to}`;
92
+ }
93
+ accumulator.push(replacement);
94
+ last = r.offset + r.from.length;
95
+ }
96
+ accumulator.push(expression.slice(last));
97
+ return accumulator.join('');
98
+ }
99
+
100
+ interface Use {
101
+ offset: number;
102
+ scope: Scope;
103
+ isShorthandAssignmentProperty: boolean;
104
+ }
105
+
106
+ const enum DefinitionKind {
107
+ None = 0,
108
+ Let = 1,
109
+ Var = 2,
110
+ Fixed = 3,
111
+ }
112
+
113
+ interface VariableUses {
114
+ definitionKind: DefinitionKind;
115
+ uses: Use[];
116
+ }
117
+
118
+ class Scope {
119
+ readonly variables = new Map<string, VariableUses>();
120
+ readonly parent: Scope|null;
121
+
122
+ constructor(parent: Scope|null) {
123
+ this.parent = parent;
124
+ }
125
+
126
+ addVariable(name: string, offset: number, definitionKind: DefinitionKind, isShorthandAssignmentProperty: boolean):
127
+ void {
128
+ const variable = this.variables.get(name);
129
+ const use = {offset, scope: this, isShorthandAssignmentProperty};
130
+ if (!variable) {
131
+ this.variables.set(name, {definitionKind, uses: [use]});
132
+ return;
133
+ }
134
+ if (variable.definitionKind === DefinitionKind.None) {
135
+ variable.definitionKind = definitionKind;
136
+ }
137
+ variable.uses.push(use);
138
+ }
139
+
140
+ findBinders(name: string): VariableUses[] {
141
+ const result = [];
142
+ let scope: Scope|null = this;
143
+ while (scope !== null) {
144
+ const defUse = scope.variables.get(name);
145
+ if (defUse && defUse.definitionKind !== DefinitionKind.None) {
146
+ result.push(defUse);
147
+ }
148
+ scope = scope.parent;
149
+ }
150
+ return result;
151
+ }
152
+
153
+ #mergeChildDefUses(name: string, defUses: VariableUses): void {
154
+ const variable = this.variables.get(name);
155
+ if (!variable) {
156
+ this.variables.set(name, defUses);
157
+ return;
158
+ }
159
+ variable.uses.push(...defUses.uses);
160
+ if (defUses.definitionKind === DefinitionKind.Var) {
161
+ console.assert(variable.definitionKind !== DefinitionKind.Let);
162
+ if (variable.definitionKind === DefinitionKind.None) {
163
+ variable.definitionKind = defUses.definitionKind;
164
+ }
165
+ } else {
166
+ console.assert(defUses.definitionKind === DefinitionKind.None);
167
+ }
168
+ }
169
+
170
+ finalizeToParent(isFunctionScope: boolean): void {
171
+ if (!this.parent) {
172
+ console.error('Internal error: wrong nesting in scope analysis.');
173
+ throw new Error('Internal error');
174
+ }
175
+
176
+ // Move all unbound variables to the parent (also move var-bound variables
177
+ // if the parent is not a function).
178
+ const keysToRemove = [];
179
+ for (const [name, defUse] of this.variables.entries()) {
180
+ if (defUse.definitionKind === DefinitionKind.None ||
181
+ (defUse.definitionKind === DefinitionKind.Var && !isFunctionScope)) {
182
+ this.parent.#mergeChildDefUses(name, defUse);
183
+ keysToRemove.push(name);
184
+ }
185
+ }
186
+ keysToRemove.forEach(k => this.variables.delete(k));
187
+ }
188
+ }
189
+
190
+ class ScopeVariableAnalysis {
191
+ readonly #rootScope = new Scope(null);
192
+ readonly #allNames = new Set<string>();
193
+ #currentScope = this.#rootScope;
194
+
195
+ processNode(node: Acorn.ESTree.Node|null): void {
196
+ if (node === null) {
197
+ return;
198
+ }
199
+ switch (node.type) {
200
+ case 'AwaitExpression':
201
+ this.processNode(node.argument);
202
+ break;
203
+ case 'ArrayExpression':
204
+ node.elements.forEach(item => this.processNode(item));
205
+ break;
206
+ case 'ExpressionStatement':
207
+ this.processNode(node.expression);
208
+ break;
209
+ case 'Program':
210
+ console.assert(this.#currentScope === this.#rootScope);
211
+ node.body.forEach(item => this.processNode(item));
212
+ console.assert(this.#currentScope === this.#rootScope);
213
+ break;
214
+ case 'ArrayPattern':
215
+ node.elements.forEach(item => this.processNode(item));
216
+ break;
217
+ case 'ArrowFunctionExpression':
218
+ this.#pushScope();
219
+ node.params.forEach(this.#processNodeAsDefinition.bind(this, DefinitionKind.Var));
220
+ this.processNode(node.body);
221
+ this.#popScope(true);
222
+ break;
223
+ case 'AssignmentExpression':
224
+ case 'AssignmentPattern':
225
+ case 'BinaryExpression':
226
+ case 'LogicalExpression':
227
+ this.processNode(node.left);
228
+ this.processNode(node.right);
229
+ break;
230
+ case 'BlockStatement':
231
+ this.#pushScope();
232
+ node.body.forEach(this.processNode.bind(this));
233
+ this.#popScope(false);
234
+ break;
235
+ case 'CallExpression':
236
+ this.processNode(node.callee);
237
+ node.arguments.forEach(this.processNode.bind(this));
238
+ break;
239
+ case 'VariableDeclaration': {
240
+ const definitionKind = node.kind === 'var' ? DefinitionKind.Var : DefinitionKind.Let;
241
+ node.declarations.forEach(this.#processVariableDeclarator.bind(this, definitionKind));
242
+ break;
243
+ }
244
+ case 'CatchClause':
245
+ this.#pushScope();
246
+ this.#processNodeAsDefinition(DefinitionKind.Let, node.param);
247
+ node.body.body.forEach(this.processNode.bind(this));
248
+ this.#popScope(false);
249
+ break;
250
+ case 'ClassBody':
251
+ node.body.forEach(this.processNode.bind(this));
252
+ break;
253
+ case 'ClassDeclaration':
254
+ this.#processNodeAsDefinition(DefinitionKind.Let, node.id);
255
+ this.#pushScope();
256
+ this.processNode(node.superClass ?? null);
257
+ this.processNode(node.body);
258
+ this.#popScope(false);
259
+ break;
260
+ case 'ClassExpression':
261
+ this.#pushScope();
262
+ // Intentionally ignore the id.
263
+ this.processNode(node.superClass ?? null);
264
+ this.processNode(node.body);
265
+ this.#popScope(false);
266
+ break;
267
+ case 'ChainExpression':
268
+ this.processNode(node.expression);
269
+ break;
270
+ case 'ConditionalExpression':
271
+ this.processNode(node.test);
272
+ this.processNode(node.consequent);
273
+ this.processNode(node.alternate);
274
+ break;
275
+ case 'DoWhileStatement':
276
+ this.processNode(node.body);
277
+ this.processNode(node.test);
278
+ break;
279
+ case 'ForInStatement':
280
+ case 'ForOfStatement':
281
+ this.#pushScope();
282
+ this.processNode(node.left);
283
+ this.processNode(node.right);
284
+ this.processNode(node.body);
285
+ this.#popScope(false);
286
+ break;
287
+ case 'ForStatement':
288
+ this.#pushScope();
289
+ this.processNode(node.init ?? null);
290
+ this.processNode(node.test ?? null);
291
+ this.processNode(node.update ?? null);
292
+ this.processNode(node.body);
293
+ this.#popScope(false);
294
+ break;
295
+ case 'FunctionDeclaration':
296
+ this.#processNodeAsDefinition(DefinitionKind.Var, node.id);
297
+ this.#pushScope();
298
+ this.#addVariable('this', node.start, DefinitionKind.Fixed);
299
+ this.#addVariable('arguments', node.start, DefinitionKind.Fixed);
300
+ node.params.forEach(this.#processNodeAsDefinition.bind(this, DefinitionKind.Let));
301
+ this.processNode(node.body);
302
+ this.#popScope(true);
303
+ break;
304
+ case 'FunctionExpression':
305
+ // Id is intentionally ignored in function expressions.
306
+ this.#pushScope();
307
+ this.#addVariable('this', node.start, DefinitionKind.Fixed);
308
+ this.#addVariable('arguments', node.start, DefinitionKind.Fixed);
309
+ node.params.forEach(this.#processNodeAsDefinition.bind(this, DefinitionKind.Let));
310
+ this.processNode(node.body);
311
+ this.#popScope(true);
312
+ break;
313
+ case 'Identifier':
314
+ this.#addVariable(node.name, node.start);
315
+ break;
316
+ case 'IfStatement':
317
+ this.processNode(node.test);
318
+ this.processNode(node.consequent);
319
+ this.processNode(node.alternate ?? null);
320
+ break;
321
+ case 'LabeledStatement':
322
+ this.processNode(node.body);
323
+ break;
324
+ case 'MetaProperty':
325
+ break;
326
+ case 'MethodDefinition':
327
+ if (node.computed) {
328
+ this.processNode(node.key);
329
+ }
330
+ this.processNode(node.value);
331
+ break;
332
+ case 'NewExpression':
333
+ this.processNode(node.callee);
334
+ node.arguments.forEach(this.processNode.bind(this));
335
+ break;
336
+ case 'MemberExpression':
337
+ this.processNode(node.object);
338
+ if (node.computed) {
339
+ this.processNode(node.property);
340
+ }
341
+ break;
342
+ case 'ObjectExpression':
343
+ node.properties.forEach(this.processNode.bind(this));
344
+ break;
345
+ case 'ObjectPattern':
346
+ node.properties.forEach(this.processNode.bind(this));
347
+ break;
348
+ case 'PrivateIdentifier':
349
+ break;
350
+ case 'PropertyDefinition':
351
+ if (node.computed) {
352
+ this.processNode(node.key);
353
+ }
354
+ this.processNode(node.value ?? null);
355
+ break;
356
+ case 'Property':
357
+ if (node.shorthand) {
358
+ console.assert(node.value === node.key);
359
+ console.assert(node.value.type === 'Identifier');
360
+ this.#addVariable((node.value as Acorn.ESTree.Identifier).name, node.value.start, DefinitionKind.None, true);
361
+ } else {
362
+ if (node.computed) {
363
+ this.processNode(node.key);
364
+ }
365
+ this.processNode(node.value);
366
+ }
367
+ break;
368
+ case 'RestElement':
369
+ this.#processNodeAsDefinition(DefinitionKind.Let, node.argument);
370
+ break;
371
+ case 'ReturnStatement':
372
+ this.processNode(node.argument ?? null);
373
+ break;
374
+ case 'SequenceExpression':
375
+ node.expressions.forEach(this.processNode.bind(this));
376
+ break;
377
+ case 'SpreadElement':
378
+ this.processNode(node.argument);
379
+ break;
380
+ case 'SwitchCase':
381
+ this.processNode(node.test ?? null);
382
+ node.consequent.forEach(this.processNode.bind(this));
383
+ break;
384
+ case 'SwitchStatement':
385
+ this.processNode(node.discriminant);
386
+ node.cases.forEach(this.processNode.bind(this));
387
+ break;
388
+ case 'TaggedTemplateExpression':
389
+ this.processNode(node.tag);
390
+ this.processNode(node.quasi);
391
+ break;
392
+ case 'TemplateLiteral':
393
+ node.expressions.forEach(this.processNode.bind(this));
394
+ break;
395
+ case 'ThisExpression':
396
+ this.#addVariable('this', node.start);
397
+ break;
398
+ case 'ThrowStatement':
399
+ this.processNode(node.argument);
400
+ break;
401
+ case 'TryStatement':
402
+ this.processNode(node.block);
403
+ this.processNode(node.handler ?? null);
404
+ this.processNode(node.finalizer ?? null);
405
+ break;
406
+ case 'WithStatement':
407
+ this.processNode(node.object);
408
+ // TODO jarin figure how to treat the with body.
409
+ this.processNode(node.body);
410
+ break;
411
+ case 'YieldExpression':
412
+ this.processNode(node.argument ?? null);
413
+ break;
414
+ case 'UnaryExpression':
415
+ case 'UpdateExpression':
416
+ this.processNode(node.argument);
417
+ break;
418
+ case 'WhileStatement':
419
+ this.processNode(node.test);
420
+ this.processNode(node.body);
421
+ break;
422
+
423
+ // Ignore, no expressions involved.
424
+ case 'BreakStatement':
425
+ case 'ContinueStatement':
426
+ case 'DebuggerStatement':
427
+ case 'EmptyStatement':
428
+ case 'Literal':
429
+ case 'Super':
430
+ case 'TemplateElement':
431
+ break;
432
+ // Ignore, cannot be used outside of a module.
433
+ case 'ImportDeclaration':
434
+ case 'ImportDefaultSpecifier':
435
+ case 'ImportNamespaceSpecifier':
436
+ case 'ImportSpecifier':
437
+ case 'ImportExpression':
438
+ case 'ExportAllDeclaration':
439
+ case 'ExportDefaultDeclaration':
440
+ case 'ExportNamedDeclaration':
441
+ case 'ExportSpecifier':
442
+ break;
443
+
444
+ case 'VariableDeclarator':
445
+ console.error('Should not encounter VariableDeclarator in general traversal.');
446
+ break;
447
+ }
448
+ }
449
+
450
+ getFreeVariables(): Map<string, Use[]> {
451
+ const result = new Map<string, Use[]>();
452
+ for (const [name, defUse] of this.#rootScope.variables) {
453
+ if (defUse.definitionKind !== DefinitionKind.None) {
454
+ // Skip bound variables.
455
+ continue;
456
+ }
457
+ result.set(name, defUse.uses);
458
+ }
459
+ return result;
460
+ }
461
+
462
+ getAllNames(): Set<string> {
463
+ return this.#allNames;
464
+ }
465
+
466
+ #pushScope(): void {
467
+ this.#currentScope = new Scope(this.#currentScope);
468
+ }
469
+
470
+ #popScope(isFunctionContext: boolean): void {
471
+ if (this.#currentScope.parent === null) {
472
+ console.error('Internal error: wrong nesting in scope analysis.');
473
+ throw new Error('Internal error');
474
+ }
475
+ this.#currentScope.finalizeToParent(isFunctionContext);
476
+ this.#currentScope = this.#currentScope.parent;
477
+ }
478
+
479
+ #addVariable(
480
+ name: string, offset: number, definitionKind: DefinitionKind = DefinitionKind.None,
481
+ isShorthandAssignmentProperty: boolean = false): void {
482
+ this.#allNames.add(name);
483
+ this.#currentScope.addVariable(name, offset, definitionKind, isShorthandAssignmentProperty);
484
+ }
485
+
486
+ #processNodeAsDefinition(
487
+ definitionKind: DefinitionKind.Let|DefinitionKind.Var,
488
+ node: Acorn.ESTree.Pattern|Acorn.ESTree.AssignmentProperty|null): void {
489
+ if (node === null) {
490
+ return;
491
+ }
492
+ switch (node.type) {
493
+ case 'ArrayPattern':
494
+ node.elements.forEach(this.#processNodeAsDefinition.bind(this, definitionKind));
495
+ break;
496
+ case 'AssignmentPattern':
497
+ this.#processNodeAsDefinition(definitionKind, node.left);
498
+ this.processNode(node.right);
499
+ break;
500
+ case 'Identifier':
501
+ this.#addVariable(node.name, node.start, definitionKind);
502
+ break;
503
+ case 'MemberExpression':
504
+ this.processNode(node.object);
505
+ if (node.computed) {
506
+ this.processNode(node.property);
507
+ }
508
+ break;
509
+ case 'ObjectPattern':
510
+ node.properties.forEach(this.#processNodeAsDefinition.bind(this, definitionKind));
511
+ break;
512
+ case 'Property':
513
+ // This is AssignmentProperty inside an object pattern.
514
+ if (node.shorthand) {
515
+ console.assert(node.value === node.key);
516
+ console.assert(node.value.type === 'Identifier');
517
+ this.#addVariable((node.value as Acorn.ESTree.Identifier).name, node.value.start, definitionKind, true);
518
+ } else {
519
+ if (node.computed) {
520
+ this.processNode(node.key);
521
+ }
522
+ this.#processNodeAsDefinition(definitionKind, node.value);
523
+ }
524
+ break;
525
+ case 'RestElement':
526
+ this.#processNodeAsDefinition(definitionKind, node.argument);
527
+ break;
528
+ }
529
+ }
530
+
531
+ #processVariableDeclarator(
532
+ definitionKind: DefinitionKind.Let|DefinitionKind.Var, decl: Acorn.ESTree.VariableDeclarator): void {
533
+ this.#processNodeAsDefinition(definitionKind, decl.id);
534
+ this.processNode(decl.init ?? null);
535
+ }
536
+ }
@@ -16,6 +16,7 @@ import * as HTMLOutline from './HTMLOutline.js';
16
16
  import * as JavaScriptFormatter from './JavaScriptFormatter.js';
17
17
  import * as JavaScriptOutline from './JavaScriptOutline.js';
18
18
  import * as JSONFormatter from './JSONFormatter.js';
19
+ import * as Substitute from './Substitute.js';
19
20
 
20
21
  export {
21
22
  CSSFormatter,
@@ -27,4 +28,5 @@ export {
27
28
  JavaScriptFormatter,
28
29
  JavaScriptOutline,
29
30
  JSONFormatter,
31
+ Substitute,
30
32
  };
@@ -117,6 +117,12 @@ const issueCodeHandlers = new Map<
117
117
  */
118
118
  function createIssuesFromProtocolIssue(
119
119
  issuesModel: SDK.IssuesModel.IssuesModel, inspectorIssue: Protocol.Audits.InspectorIssue): Issue[] {
120
+ if (inspectorIssue.code.toString() === 'CookieIssue') {
121
+ // TODO: backward compatibility for the next chromium roll
122
+ const details = inspectorIssue.details as {cookieIssueDetails: Protocol.Audits.SameSiteCookieIssueDetails};
123
+ inspectorIssue.code = Protocol.Audits.InspectorIssueCode.SameSiteCookieIssue;
124
+ inspectorIssue.details.sameSiteCookieIssueDetails = details.cookieIssueDetails;
125
+ }
120
126
  const handler = issueCodeHandlers.get(inspectorIssue.code);
121
127
  if (handler) {
122
128
  return handler(issuesModel, inspectorIssue);
@@ -16,6 +16,7 @@
16
16
  .status-dialog-line {
17
17
  margin: 2px;
18
18
  height: 14px;
19
+ min-height: auto;
19
20
  display: flex;
20
21
  align-items: baseline;
21
22
  }
@@ -15,6 +15,7 @@ declare module 'estree' {
15
15
  // The @types/estree do not export the types to a namespace. Since we reference
16
16
  // these types as "ESTree.Node", we need to explicitly re-export them here.
17
17
  export type ArrayPattern = estree.ArrayPattern;
18
+ export type AssignmentProperty = estree.AssignmentProperty;
18
19
  export type CatchClause = estree.CatchClause;
19
20
  export type Class = estree.Class;
20
21
  export type Expression = estree.Expression;
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  "unittest": "scripts/test/run_unittests.py --no-text-coverage",
55
55
  "watch": "third_party/node/node.py --output scripts/watch_build.js"
56
56
  },
57
- "version": "1.0.974575"
57
+ "version": "1.0.975056"
58
58
  }