Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Sep 19, 2024
1 parent 2fa4e97 commit 24dd165
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default (callProvider: CallProvider) => {
callProvider({ kind: "global", name }, path);
}

function analyzeMemberExpression(path: NodePath<t.MemberExpression>) {
function analyzeMemberExpression(
path: NodePath<t.MemberExpression | t.OptionalMemberExpression>,
) {
const key = resolveKey(path.get("property"), path.node.computed);
return { key, handleAsMemberExpression: !!key && key !== "prototype" };
}
Expand All @@ -48,7 +50,9 @@ export default (callProvider: CallProvider) => {
handleReferencedIdentifier(path);
},

MemberExpression(path: NodePath<t.MemberExpression>) {
"MemberExpression|OptionalMemberExpression"(
path: NodePath<t.MemberExpression | t.OptionalMemberExpression>,
) {
const { key, handleAsMemberExpression } = analyzeMemberExpression(path);
if (!handleAsMemberExpression) return;

Expand Down
13 changes: 13 additions & 0 deletions packages/babel-helper-define-polyfill-provider/test/descriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,17 @@ describe("descriptors", () => {
expect(path.type).toBe("BinaryExpression");
expect(path.toString()).toBe("'values' in Object");
});

it("optional chains", () => {
const [desc, path] = getDescriptor("a?.includes();", "property");

expect(desc).toEqual({
kind: "property",
object: "a",
key: "includes",
placement: "static",
});
expect(path.type).toBe("OptionalMemberExpression");
expect(path.toString()).toBe("a?.includes");
});
});

0 comments on commit 24dd165

Please sign in to comment.