Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
eoftedal committed Feb 14, 2024
1 parent 1aeed53 commit 5f82790
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## [1.0.0-beta8] - 2024-02-06
## [1.0.0-beta9] - 2024-02-14

### Bugfix

* More scoping issues for binding

## [1.0.0-beta8] - 2024-02-14

### Bugfix

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-q",
"version": "1.0.0-beta.8",
"version": "1.0.0-beta.9",
"description": "offers a way to query an babel AST to find specific patterns using a syntax somewhat similar to XPath.",
"scripts": {
"lint": "eslint . --ext .ts --fix --ignore-path .gitignore",
Expand Down
4 changes: 3 additions & 1 deletion src/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function createNodePath(node: Babel.Node, key: string | undefined, parentKey: st
function registerBinding(node: Babel.Node, parentNode: Babel.Node, grandParentNode: Babel.Node | undefined, scope: Scope) {
if (t.isBinding(node, parentNode, grandParentNode) && !t.isMemberExpression(node)) {
if (t.isIdentifier(node) && !t.isAssignmentExpression(parentNode)) {
//A bit of a hack here as well. Needs some further investigation
if (t.isFunctionDeclaration(parentNode) || t.isFunctionExpression(parentNode) || t.isScope(node, parentNode)) {
setBinding(scope, node.name, { path: createNodePath(node, undefined, undefined, scope) });
} else {
Expand All @@ -117,7 +118,8 @@ function registerBindings(node: Babel.Node, parentNode: Babel.Node, grandParentN
const keys = t.VISITOR_KEYS[node.type];

let childScope = scope;
if (t.isScopable(node)) {
// This is also buggy. Need to investigate what creates a new scope
if (t.isScopable(node) || t.isExportSpecifier(node)) {
childScope = createScope(scope);
}
for (const key of keys) {
Expand Down
13 changes: 13 additions & 0 deletions tests/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,19 @@ describe('testing index file', () => {
const nodes1 = query(ast!, "//MemberExpression/$:object");
const nodes2 = query(ast!, "//FunctionDeclaration//FunctionDeclaration/:id");
expect(nodes1).toEqual(nodes2);
});
test("find correct binding when exported", () => {
const code = `
let a = 1;
let b = 2;
b = a;
export {
a
}`
const ast = babel.parseSync(code);
const nodes = query(ast!, "//AssignmentExpression/$:right/:init/:value");
console.log(nodes);
expect(nodes).toEqual([1]);
})
});

Expand Down

0 comments on commit 5f82790

Please sign in to comment.