Skip to content

Commit 4ced2a1

Browse files
motiz88facebook-github-bot
authored andcommittedJan 5, 2022
Infer names for anonymous functions in optional calls
Summary: TSIA Reviewed By: rh389 Differential Revision: D33427901 fbshipit-source-id: 42ba28c32f94cf3bf145a97877b148508ce72c69
1 parent 4875f3d commit 4ced2a1

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed
 

‎packages/metro-source-map/src/__tests__/generateFunctionMap-test.js

+46
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,52 @@ function parent2() {
893893
`);
894894
});
895895

896+
it('callback of optional method', () => {
897+
const ast = getAst(`
898+
object?.method(() => {}, [])
899+
`);
900+
901+
expect(generateCompactRawMappings(ast)).toMatchInlineSnapshot(`
902+
"
903+
<global> from 1:0
904+
object.method$argument_0 from 2:21
905+
<global> from 2:29
906+
"
907+
`);
908+
expect(generateFunctionMap(ast)).toMatchInlineSnapshot(`
909+
Object {
910+
"mappings": "AAA;qBCC,QD",
911+
"names": Array [
912+
"<global>",
913+
"object.method$argument_0",
914+
],
915+
}
916+
`);
917+
});
918+
919+
it('optional call', () => {
920+
const ast = getAst(`
921+
func?.(() => {}, [])
922+
`);
923+
924+
expect(generateCompactRawMappings(ast)).toMatchInlineSnapshot(`
925+
"
926+
<global> from 1:0
927+
func$argument_0 from 2:13
928+
<global> from 2:21
929+
"
930+
`);
931+
expect(generateFunctionMap(ast)).toMatchInlineSnapshot(`
932+
Object {
933+
"mappings": "AAA;aCC,QD",
934+
"names": Array [
935+
"<global>",
936+
"func$argument_0",
937+
],
938+
}
939+
`);
940+
});
941+
896942
it('JSX prop', () => {
897943
const ast = getAst(`
898944
<Button onClick={() => {}} />

‎packages/metro-source-map/src/generateFunctionMap.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type {Node} from '@babel/types';
1717
import traverse from '@babel/traverse';
1818
import {
1919
isAssignmentExpression,
20-
isCallExpression,
2120
isClassBody,
2221
isClassMethod,
2322
isClassProperty,
@@ -29,7 +28,6 @@ import {
2928
isJSXExpressionContainer,
3029
isJSXIdentifier,
3130
isLiteral,
32-
isNewExpression,
3331
isNullLiteral,
3432
isObjectExpression,
3533
isObjectMethod,
@@ -246,7 +244,7 @@ function getNameForPath(path: NodePath<>): string {
246244

247245
if (name == null) {
248246
// We couldn't find a name directly. Try the parent in certain cases.
249-
if (isCallExpression(parent) || isNewExpression(parent)) {
247+
if (isAnyCallExpression(parent)) {
250248
// foo(function () {})
251249
const argIndex = parent.arguments.indexOf(node);
252250
if (argIndex !== -1) {
@@ -296,9 +294,19 @@ function getNameForPath(path: NodePath<>): string {
296294
return name;
297295
}
298296

297+
function isAnyCallExpression(node: Node): boolean %checks {
298+
return (
299+
node.type === 'CallExpression' ||
300+
node.type === 'NewExpression' ||
301+
node.type === 'OptionalCallExpression'
302+
);
303+
}
304+
299305
function isAnyMemberExpression(node: Node): boolean %checks {
300306
return (
301-
node.type === 'MemberExpression' || node.type === 'JSXMemberExpression'
307+
node.type === 'MemberExpression' ||
308+
node.type === 'JSXMemberExpression' ||
309+
node.type === 'OptionalMemberExpression'
302310
);
303311
}
304312

@@ -331,7 +339,7 @@ function getNamePartsFromId(id: Node): $ReadOnlyArray<string> {
331339
return [];
332340
}
333341

334-
if (isCallExpression(id) || isNewExpression(id)) {
342+
if (isAnyCallExpression(id)) {
335343
return getNamePartsFromId(id.callee);
336344
}
337345

0 commit comments

Comments
 (0)
Please sign in to comment.