(usedValues: UsedScopeValues, s?: SourceCode)
| 57 | } |
| 58 | |
| 59 | function validateCode(usedValues: UsedScopeValues, s?: SourceCode): Code { |
| 60 | if (!s) throw new Error('moduleCode: function does not have "source" property') |
| 61 | if (usedState(s.validateName) === UsedValueState.Completed) return nil |
| 62 | setUsedState(s.validateName, UsedValueState.Started) |
| 63 | |
| 64 | const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode) |
| 65 | const code = new _Code(`${scopeCode}${_n}${s.validateCode}`) |
| 66 | return s.evaluated ? _`${code}${s.validateName}.evaluated = ${s.evaluated};${_n}` : code |
| 67 | |
| 68 | function refValidateCode(n: ValueScopeName): Code | undefined { |
| 69 | const vRef = n.value?.ref |
| 70 | if (n.prefix === "validate" && typeof vRef == "function") { |
| 71 | const v = vRef as AnyValidateFunction |
| 72 | return validateCode(usedValues, v.source) |
| 73 | } else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") { |
| 74 | const {validate, validateName} = vRef as SchemaEnv |
| 75 | if (!validateName) throw new Error("ajv internal error") |
| 76 | const def = ajv.opts.code.es5 ? varKinds.var : varKinds.const |
| 77 | const wrapper = _`${def} ${n} = {validate: ${validateName}};` |
| 78 | if (usedState(validateName) === UsedValueState.Started) return wrapper |
| 79 | const vCode = validateCode(usedValues, validate?.source) |
| 80 | return _`${wrapper}${_n}${vCode}` |
| 81 | } |
| 82 | return undefined |
| 83 | } |
| 84 | |
| 85 | function usedState(name: ValueScopeName): UsedValueState | undefined { |
| 86 | return usedValues[name.prefix]?.get(name) |
| 87 | } |
| 88 | |
| 89 | function setUsedState(name: ValueScopeName, state: UsedValueState): void { |
| 90 | const {prefix} = name |
| 91 | const names = (usedValues[prefix] = usedValues[prefix] || new Map()) |
| 92 | names.set(name, state) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | module.exports = exports = standaloneCode |
no test coverage detected
searching dependent graphs…