You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AcornJS中借助同步回调处理程序规避全局状态的技术问询

How to Accumulate [type, path] Arrays in AcornJS Without Global Variables (Functional Approach)

Absolutely! You can ditch global variables entirely here and lean into functional programming patterns to accumulate your [type, path] pairs from the VID and VCE handlers in types.visit. The key is to use closures to encapsulate your accumulated state, keeping it isolated to your processing function instead of polluting the global scope.

Here's a clean, functional implementation that does exactly what you need:

import * as acorn from 'acorn';
import * as types from 'acorn-walk';

function collectTargetNodes(ast) {
  // Encapsulate the accumulated array inside the function (no globals!)
  const collectedPairs = [];

  // Define a shared handler for both VID and VCE nodes
  const handleNode = (node, path) => {
    // Add the [type, path] pair to our encapsulated array
    collectedPairs.push([node.type, [...path]]); // Spread path to avoid reference issues
    // Return undefined (default) to let Acorn continue traversing child nodes
  };

  // Run the visitor, mapping both VID and VCE to our shared handler
  types.visit(ast, {
    VID: handleNode,
    VCE: handleNode
  });

  // Return the fully accumulated array
  return collectedPairs;
}

// Example usage
const sampleCode = `// Your JS code here`;
const ast = acorn.parse(sampleCode);
const result = collectTargetNodes(ast);

console.log(result); // Logs all [type, path] pairs from VID and VCE nodes

Why this works (and fits functional paradigms):

  • No global state: The collectedPairs array is completely encapsulated inside collectTargetNodes—it can't be modified or accessed from outside the function, eliminating side effects.
  • Pure function behavior: The function takes an AST as input and returns the accumulated array as output. Given the same AST, it will always return the same result (no hidden state changes).
  • Reusable: You can call this function with any AST, and it will safely collect the target nodes without interfering with other code.

If you need distinct logic for VID vs VCE:

If you have different processing steps for each node type but still want to accumulate pairs, you can extend the handler while keeping the accumulation logic centralized:

const handleNode = (node, path) => {
  let entry;
  if (node.type === 'VID') {
    // Add VID-specific processing here
    entry = [node.type, [...path, 'vid-specific']];
  } else if (node.type === 'VCE') {
    // Add VCE-specific processing here
    entry = [node.type, path.filter(segment => segment.type !== 'Ignored')];
  }
  collectedPairs.push(entry);
};

This way, you maintain functional purity while handling node-specific logic, all without relying on global variables.

内容的提问来源于stack exchange,提问作者ThomasReggi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:35:30