MCPcopy Create free account
hub / github.com/Script-Hub-Org/Script-Hub / traverseTree

Function traverseTree

Rewrite-Parser.js:2967–3122  ·  view source on GitHub ↗
(node, platform, parentOperator = null)

Source from the content-addressed store, hash-verified

2965 }
2966 let hasPreMatching
2967 function traverseTree(node, platform, parentOperator = null) {
2968 node.flags = { ...node.flags, ...flags }
2969 const features = platformFeatures[platform]
2970 if (!features) {
2971 throw new Error(`未知的平台:${platform}`)
2972 }
2973
2974 if (!node || !node.type) {
2975 console.log('节点缺少 type 属性或节点为 null:', node)
2976 return ''
2977 }
2978
2979 if (node.type === 'LOGICAL') {
2980 const operator = node.operator
2981 const arity = LOGICAL_OPERATORS_ARITY[operator]
2982
2983 // 检查是否有 pre-matching 标志
2984 // const hasPreMatching = node.flags && node.flags.preMatching;
2985 if (node.routingPolicy) {
2986 hasPreMatching = node.flags && node.flags.preMatching
2987 }
2988 if (hasPreMatching && node.routingPolicy) {
2989 // 验证 routingPolicy 是否符合 ^REJECT(-[A-Z]+)*$ 的格式
2990 if (!features.rejectPolicyRegex.test(node.routingPolicy)) {
2991 console.log(`pre-matching 只能与 REJECT 系列策略一起使用,当前策略为:${node.routingPolicy}`)
2992 hasPreMatching = false
2993 }
2994 }
2995
2996 if (hasPreMatching) {
2997 // 检查所有子规则是否属于支持 pre-matching 的类型
2998 const notSupportedTypes = []
2999 const allChildrenSupported = node.children.every(childArray => {
3000 return Array.isArray(childArray)
3001 ? childArray.every(child => {
3002 const isSupported = FLAG_SUPPORTED_TYPES.preMatching.includes(child.operator)
3003 if (!isSupported) {
3004 notSupportedTypes.push(child.operator)
3005 }
3006 return isSupported
3007 })
3008 : true
3009 })
3010
3011 if (!allChildrenSupported) {
3012 console.log(
3013 `逻辑运算符 ${operator} 中的所有子规则必须是支持 pre-matching 的类型, 但 ${notSupportedTypes.join(
3014 ', '
3015 )} 不支持`
3016 )
3017 hasPreMatching = false
3018 }
3019 }
3020
3021 let childrenOutputs = []
3022 node.children.forEach(child => {
3023 flattenChildren(child).forEach(subChild => {
3024 const output = traverseTree(subChild, platform, operator)

Callers 1

generateRuleFunction · 0.70

Calls 3

flattenChildrenFunction · 0.70
needsParenthesesFunction · 0.70
logMethod · 0.45

Tested by

no test coverage detected