st-bundle
Version:
CLI for watching and bundling SpringType projects.
90 lines (89 loc) • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function nodeIsString(node) {
return node.type === 'Literal' || node.type === 'StringLiteral';
}
exports.nodeIsString = nodeIsString;
function fastWalk(ast, walker) {
function visit(node, props, context) {
if (walker.withScope) {
if (node.context) {
context = node.context;
}
if (node.type === 'VariableDeclaration') {
if (node.declarations) {
for (const decl of node.declarations) {
if (decl.type === 'VariableDeclarator' && decl.id && decl.id.type === 'Identifier') {
if (context === undefined)
context = { locals: [] };
context.locals.push(decl.id.name);
// we need to check for the next item on the list (if we are in an array)
if (props.idx && props.prop) {
if (props.parent[props.prop][props.idx + 1]) {
props.parent[props.prop][props.idx + 1].context = context;
}
}
}
}
}
}
if (node.type === 'ExpressionStatement') {
if (node.expression && node.expression.arguments) {
node.expression.arguments.context = context;
}
}
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
if (node.id && node.id.type === 'Identifier') {
// handles the following case:
/*
function o() {}
console.log(o)
*/
if (context === undefined)
context = { locals: [] };
context.locals.push(node.id.name);
if (props.idx && props.prop) {
if (props.parent[props.prop][props.idx + 1]) {
props.parent[props.prop][props.idx + 1].context = context;
}
}
}
if (node.params) {
for (const item of node.params) {
if (item.type === 'Identifier') {
if (node.body.context === undefined) {
// create context
node.body.context = {
exports: context && context.exports ? context.exports.concat([]) : [],
// here we need to make a copy of the locals
locals: context ? context.locals.concat([]) : [],
};
}
node.body.context.locals.push(item.name);
}
}
}
}
}
walker.visit(node, props, context);
for (const prop in node) {
if (prop[0] === '$') {
continue;
}
const child = node[prop];
if (child instanceof Array) {
for (let i = 0; i < child.length; i++) {
if (child && child[i] && child[i].type) {
visit(child[i], { parent: node, prop, idx: i }, context);
}
}
}
else {
if (child && child.type)
visit(child, { parent: node, prop }, context);
}
}
}
visit(ast, {}, undefined);
}
exports.fastWalk = fastWalk;