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); + }); }); });