Skip to content

Commit

Permalink
[babel-types] Update matchesPattern to account for this (#13264)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyenwei committed May 5, 2021
1 parent 8873d0c commit 187094b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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 "..";

/**
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-types/test/misc.js
Expand Up @@ -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);
});
});
});

0 comments on commit 187094b

Please sign in to comment.