From 187094b9a6f56d5ccb117c4701f8446a92c9f893 Mon Sep 17 00:00:00 2001 From: Yen-Wei Liu Date: Wed, 5 May 2021 14:08:43 -0700 Subject: [PATCH] [babel-types] Update `matchesPattern` to account for `this` (#13264) --- packages/babel-types/src/validators/matchesPattern.ts | 9 ++++++++- packages/babel-types/test/misc.js | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/babel-types/src/validators/matchesPattern.ts b/packages/babel-types/src/validators/matchesPattern.ts index 6088f2e48c..c8acedb8dd 100644 --- a/packages/babel-types/src/validators/matchesPattern.ts +++ b/packages/babel-types/src/validators/matchesPattern.ts @@ -1,4 +1,9 @@ -import { isIdentifier, isMemberExpression, isStringLiteral } from "./generated"; +import { + isIdentifier, + isMemberExpression, + isStringLiteral, + isThisExpression, +} from "./generated"; import type * as t from ".."; /** @@ -35,6 +40,8 @@ export default function matchesPattern( value = node.name; } else if (isStringLiteral(node)) { value = node.value; + } else if (isThisExpression(node)) { + value = "this"; } else { return false; } diff --git a/packages/babel-types/test/misc.js b/packages/babel-types/test/misc.js index 79b1155a0f..6fb9568be2 100644 --- a/packages/babel-types/test/misc.js +++ b/packages/babel-types/test/misc.js @@ -40,5 +40,13 @@ describe("misc helpers", function () { expect(t.matchesPattern(ast, "b.c.d", true)).toBe(false); expect(t.matchesPattern(ast, "a.b.c.d.e", true)).toBe(false); }); + + it("matches this expressions", function () { + const ast = parseCode("this.a.b.c.d").expression; + expect(t.matchesPattern(ast, "this.a.b.c.d")).toBeTruthy(); + expect(t.matchesPattern(ast, "this.a.b.c")).toBe(false); + expect(t.matchesPattern(ast, "this.b.c.d")).toBe(false); + expect(t.matchesPattern(ast, "this.a.b.c.d.e")).toBe(false); + }); }); });