Skip to content

Commit 1f813ab

Browse files
szuendDevtools-frontend LUCI CQ
authored andcommitted
[scopes] Omit name for arrow functions
When building scope information from the AST, we map a scope's start position to get at the original function name. This does not work for arrow function as the start position points to the first argument. And the original name of the first argument is not the right name for the function. For now, we just don't translate arrow functions. In the future, we'll report the position of the `=>` as well as the scope start, and map the position of the `=>` (as per spec), to get to the name. [email protected] Bug: 463452667 Change-Id: If524d2d36475479d757b4ec373e025bb0b701abc Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7202683 Reviewed-by: Kim-Anh Tran <[email protected]> Commit-Queue: Simon Zünd <[email protected]>
1 parent d4a2211 commit 1f813ab

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

front_end/core/sdk/SourceMapScopesInfo.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,18 @@ export class SourceMapScopesInfo {
121121
const canMapOriginalPosition = startEntry && endEntry && sourceIndex !== undefined &&
122122
startEntry.sourceIndex === endEntry.sourceIndex && startEntry.sourceIndex !== undefined && sourceIndex >= 0 &&
123123
sourceIndex < numSourceUrls;
124-
const isStackFrame = node.kind === Formatter.FormatterWorkerPool.ScopeKind.FUNCTION;
124+
const isStackFrame = node.kind === Formatter.FormatterWorkerPool.ScopeKind.FUNCTION ||
125+
node.kind === Formatter.FormatterWorkerPool.ScopeKind.ARROW_FUNCTION;
126+
// TODO(crbug.com/368222773): Instead of mapping `start`, we should report a number of candidates. e.g. for arrow functions we should
127+
// follow the spec and map the `=>` as the spec says that is where the original name (if any) for arrow functions can be found.
128+
const name = node.kind === Formatter.FormatterWorkerPool.ScopeKind.FUNCTION ? startEntry?.name : undefined;
125129

126130
let scope: ScopesCodec.OriginalScope|undefined;
127131
if (canMapOriginalPosition) {
128132
scope = {
129133
start: {line: startEntry.sourceLineNumber, column: startEntry.sourceColumnNumber},
130134
end: {line: endEntry.sourceLineNumber, column: endEntry.sourceColumnNumber},
131-
name: startEntry.name,
135+
name,
132136
isStackFrame,
133137
variables: [],
134138
children: [],

front_end/entrypoints/formatter_worker/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ devtools_foundation_module("unittests") {
9292
"Substitute.test.ts",
9393
]
9494

95-
deps = [ ":bundle" ]
95+
deps = [
96+
":bundle",
97+
":formatter_actions",
98+
]
9699
}

front_end/entrypoints/formatter_worker/FormatterActions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const enum ScopeKind {
4848
BLOCK = 1,
4949
FUNCTION = 2,
5050
GLOBAL = 3,
51+
ARROW_FUNCTION = 4,
5152
}
5253

5354
export interface ScopeTreeNode {

front_end/entrypoints/formatter_worker/ScopeParser.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import * as FormatterWorker from './formatter_worker.js';
6+
import * as FormatterAction from './FormatterActions.js'; // eslint-disable-line @devtools/es-modules-import
67

78
describe('ScopeParser', () => {
89
describe('parseScopes', () => {
@@ -14,6 +15,7 @@ describe('ScopeParser', () => {
1415
const innerScope = scopes?.children[0];
1516
assert.strictEqual(innerScope?.start, 12);
1617
assert.strictEqual(innerScope?.end, 17);
18+
assert.strictEqual(innerScope?.kind, FormatterAction.ScopeKind.FUNCTION);
1719
assert.deepEqual(innerScope?.variables?.get('a')?.uses.map(u => u.offset), [13]);
1820
});
1921

@@ -24,6 +26,7 @@ describe('ScopeParser', () => {
2426
const innerScope = scopes?.children[0];
2527
assert.strictEqual(innerScope?.start, 8);
2628
assert.strictEqual(innerScope?.end, 17);
29+
assert.strictEqual(innerScope?.kind, FormatterAction.ScopeKind.ARROW_FUNCTION);
2730
assert.deepEqual(innerScope?.variables?.size, 1);
2831
assert.deepEqual(innerScope?.variables?.get('a')?.uses.map(u => u.offset), [9]);
2932
});

front_end/entrypoints/formatter_worker/ScopeParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class ScopeVariableAnalysis {
172172
node.elements.forEach(item => this.#processNode(item));
173173
break;
174174
case 'ArrowFunctionExpression': {
175-
this.#pushScope(node.start, node.end, ScopeKind.FUNCTION);
175+
this.#pushScope(node.start, node.end, ScopeKind.ARROW_FUNCTION);
176176
node.params.forEach(this.#processNodeAsDefinition.bind(this, DefinitionKind.VAR, false));
177177
if (node.body.type === 'BlockStatement') {
178178
// Include the body of the arrow function in the same scope as the arguments.

0 commit comments

Comments
 (0)