Skip to content

Instantly share code, notes, and snippets.

@PuruVJ
Created July 12, 2024 06:21
Show Gist options
  • Save PuruVJ/fd1653f3dd48079d452ce299e4386c56 to your computer and use it in GitHub Desktop.
Save PuruVJ/fd1653f3dd48079d452ce299e4386c56 to your computer and use it in GitHub Desktop.
import { parse, type Literal } from 'acorn';
import { type Node } from 'estree-walker';
import { walk } from 'zimmerframe';
const ast = parse(output, {
ecmaVersion: 2022,
sourceType: 'module',
sourceFile: full_path,
});
const imports_exports = new Map<string, Set<[number, number]>>();
walk(ast as Node, imports_exports, {
_(node, { state, next }) {
const node_types: (typeof node)['type'][] = [
'ImportExpression',
'ImportDeclaration',
'ExportNamedDeclaration',
'ExportAllDeclaration',
'ExportDefaultDeclaration',
];
if (!node_types.includes(node.type)) {
return next(state);
}
if ('source' in node && node.source != null && 'value' in node.source) {
const typed_node_source = node.source as Literal;
if (!state.has(typed_node_source.value + '')) {
state.set(typed_node_source.value + '', new Set());
}
state
.get(typed_node_source.value + '')
?.add([typed_node_source.start, typed_node_source.end]);
}
next(state);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment