{"record":{"id":"2ac960815420df4e","repo":"dianping/cat","slug":"xpst0008","errorCode":"XPST0008","errorMessage":"\"{name}\": undeclared variable","messagePattern":"\"(.+?)\": undeclared variable","errorType":"error_code","errorClass":"StaticError","httpStatus":null,"severity":"error","filePath":"cat-home/src/main/webapp/assets/js/editor/worker-xquery.js","lineNumber":1943,"sourceCode":"            }\n            return variables;\n        },\n        \n        getVariable: function(qname) {\n            var key = getVarKey(qname);\n            var that = this;\n            while(that) {\n                if(that.variables[key]) {\n                    return that.variables[key];\n                }\n                that = that.parent;\n            }\n        },\n        \n        addVarRef: function(qname, pos){\n            var varDecl = this.getVariable(qname);\n            if(!varDecl && (qname.uri === '' || this.root.moduleResolver)) {\n                throw new StaticError('XPST0008', '\"' + qname.name + '\": undeclared variable', pos);\n            }\n            var key = getVarKey(qname);\n            this.varRefs[key] = true;\n        },\n        \n        addFunctionCall: function(qname, arity, pos){\n            var fn = this.getFunction(qname, arity);\n            if(!fn && (qname.uri === 'http://www.w3.org/2005/xquery-local-functions' || this.root.moduleResolver)){\n                if((qname.uri === 'http://www.w3.org/2005/xpath-functions' ||\n                    (qname.uri === '' && this.root.defaultFunctionNamespaces.concat(this.root.defaultFunctionNamespace).indexOf('http://www.w3.org/2005/xpath-functions') !== -1)) && qname.name === 'concat') {\n                } else if(!fn){\n                    throw new StaticError('XPST0008', '\"' + qname.name + '#' + arity + '\": undeclared function', pos);\n                }\n            }\n            var key = getFnKey(qname, arity);\n            this.functionCalls[key] = true;\n        },\n        ","sourceCodeStart":1925,"sourceCodeEnd":1961,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-home/src/main/webapp/assets/js/editor/worker-xquery.js#L1925-L1961","documentation":"XPST0008 here reports a reference to an undeclared variable. addVarRef() walks the parent static-context chain via getVariable(); if no declaration is found it errors only when qname.uri === '' (unqualified names must resolve locally) or when a moduleResolver is present (imported modules' variables are expected to be resolvable, so an unknown name is a real error). Variables in foreign namespaces are skipped when no resolver exists because they may come from unimported modules.","triggerScenarios":"Referencing $foo in the query body with no matching declare variable / import of its module (unqualified case), or referencing $mod:var when a moduleResolver is configured but the imported module does not export that variable.","commonSituations":"Typos in variable names; using a variable declared in a different module without importing it; deleting a declaration but leaving references; module resolver returning a module whose variables list doesn't cover the referenced name.","solutions":["Declare the variable: 'declare variable $foo := ...;' before use.","Fix the reference typo to match the declared name exactly (XQuery names are case-sensitive).","If it comes from a module, import that module and confirm the resolver merges its variables into the static context.","Check spelling of the prefix — a wrong prefix yields a different expanded QName."],"exampleFix":"// before\nlet $r := $resultCount return $r\n(: nothing declares $resultCount :)\n\n// after\ndeclare variable $resultCount as xs:integer := 42;\nlet $r := $resultCount return $r","handlingStrategy":"validation","validationCode":"// Before analysis: every referenced variable must have a declaration or module source\nvarRefs.forEach(function(ref) {\n  if (!declaredVars[ref.uri + '#' + ref.name] &&\n      !(ref.uri !== '' && !hasModuleResolver)) {\n    warn('Undeclared variable $' + ref.name);\n  }\n});","typeGuard":"function isDeclaredVariable(ctx, qname) {\n  return !!ctx.getVariable(qname); // walks parent static contexts\n}","tryCatchPattern":"if (e.code === 'XPST0008' && /variable/.test(e.message)) {\n  highlight(e.pos, 'Declare the variable or fix the name — names are case-sensitive');\n}","preventionTips":["Declare variables before referencing them in document order.","When importing modules for their variables, confirm the resolver merges mod.variables.","Use editor autocomplete over declared names to avoid typos."],"tags":["xquery","variable-resolution","static-analysis"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}