Skip to content

Commit

Permalink
fix: avoid infinite recursion on self reference dep
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jan 4, 2022
1 parent e064f88 commit ac32fe6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/__tests__/fixtures/misc/self-reference-fn/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fixture from "../../../fixture";

describe("misc self reference", () => {
describe("with default", fixture("./templates/self-reference.marko"));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<const/fn() {
console.log(fn);
}/>
11 changes: 7 additions & 4 deletions src/transform/cached-function/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import isCoreTag from "../../util/is-core-tag";
import getAttr from "../../util/get-attr";
import isApi from "../../util/is-api";
type DepsVisitorState =
| { shallow?: undefined; deps?: Set<string> }
| { shallow: true; deps?: true };
| { root: t.NodePath; shallow?: undefined; deps?: Set<string> }
| { root: t.NodePath; shallow: true; deps?: true };

const depsVisitor = {
Function(fn, state) {
if (fn === state.root) fn.skip();
},
ReferencedIdentifier: ((identifier, state) => {
const { name } = identifier.node;
const binding = identifier.scope.getBinding(name);
Expand All @@ -26,7 +29,7 @@ const depsVisitor = {
// Const tag reflects the default value as dependencies.
const nestedState: DepsVisitorState = state.shallow
? state
: { shallow: true };
: { root: state.root, shallow: true };
getAttr(bindingTag, "default")!.traverse(depsVisitor, nestedState);
isDep = !!nestedState.deps;
} else {
Expand Down Expand Up @@ -60,7 +63,7 @@ export default {
return;
}

const state: DepsVisitorState & { deps?: Set<string> } = {};
const state: DepsVisitorState & { deps?: Set<string> } = { root: fn };
fn.skip();
fn.traverse(depsVisitor, state);

Expand Down

0 comments on commit ac32fe6

Please sign in to comment.