Skip to content

Commit

Permalink
Add new method to check if node is null or not (#13940)
Browse files Browse the repository at this point in the history
Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com>
  • Loading branch information
danez and merceyz committed Feb 1, 2022
1 parent b092bd0 commit b91c3e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/babel-traverse/src/path/index.ts
Expand Up @@ -122,6 +122,10 @@ class NodePath<T extends t.Node = t.Node> {
return val;
}

hasNode(): this is NodePath<NonNullable<this["node"]>> {
return this.node != null;
}

buildCodeFrameError(
msg: string,
Error: new () => Error = SyntaxError,
Expand Down
15 changes: 15 additions & 0 deletions packages/babel-traverse/test/path/index.js
Expand Up @@ -47,5 +47,20 @@ describe("NodePath", () => {

expect(path.getData(symbol)).toBe(42);
});

describe("hasNode", () => {
it("returns false if node is null", () => {
const path = new NodePath({}, {});

expect(path.hasNode()).toBe(false);
});

it("returns true if node is not null", () => {
const path = new NodePath({}, {});
path.node = {};

expect(path.hasNode()).toBe(true);
});
});
});
});

0 comments on commit b91c3e1

Please sign in to comment.