You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/src/utils/walkThroughTopFunctionScope.ts

22 lines
487 B

import { Node } from '../interfaces';
import { walk } from 'estree-walker';
export default function walkThroughTopFunctionScope(body: Node, callback: Function) {
let lexicalDepth = 0;
walk(body, {
enter(node: Node) {
if (/^Function/.test(node.type)) {
lexicalDepth += 1;
} else if (lexicalDepth === 0) {
callback(node)
}
},
leave(node: Node) {
if (/^Function/.test(node.type)) {
lexicalDepth -= 1;
}
},
});
}