{"record":{"id":"23f8d71cda4e5f6c","repo":"evanw/esbuild","slug":"can-t-understand-test-file-file-line-col","errorCode":null,"errorMessage":"Can't understand test file {file} [{line},{col}]\n{code}","messagePattern":"Can't understand test file (.+?) \\[(.+?),(.+?)\\]\n(.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/terser-tests.js","lineNumber":253,"sourceCode":"  }\n  var tests = {};\n  var tw = new U.TreeWalker(function (node, descend) {\n    if (node instanceof U.AST_LabeledStatement\n      && tw.parent() instanceof U.AST_Toplevel) {\n      var name = node.label.name;\n      if (name in tests) {\n        throw new Error('Duplicated test name \"' + name + '\" in ' + file);\n      }\n      tests[name] = get_one_test(name, node.body);\n      return true;\n    }\n    if (!(node instanceof U.AST_Toplevel)) croak(node);\n  });\n  ast.walk(tw);\n  return tests;\n\n  function croak(node) {\n    throw new Error(tmpl(\"Can't understand test file {file} [{line},{col}]\\n{code}\", {\n      file: file,\n      line: node.start.line,\n      col: node.start.col,\n      code: make_code(node, { beautify: false })\n    }));\n  }\n\n  function read_boolean(stat) {\n    if (stat.TYPE == \"SimpleStatement\") {\n      var body = stat.body;\n      if (body instanceof U.AST_Boolean) {\n        return body.value;\n      }\n    }\n    throw new Error(\"Should be boolean\");\n  }\n\n  function read_string(stat) {","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/scripts/terser-tests.js#L235-L271","documentation":"Thrown by croak() inside parse_test() in esbuild's terser test harness (scripts/terser-tests.js:253). The harness walks the parsed AST of a terser compress-test file with a TreeWalker; every top-level node is expected to be an AST_LabeledStatement (a named test block). If the walker reaches any other node type besides AST_Toplevel, the test-file structure is considered unparseable and this error is raised with the offending file, line, column, and code snippet.","triggerScenarios":"A terser test file (consumed by scripts/terser-tests.js) contains a top-level statement that is not a label:test block — e.g. a bare function declaration, import, or stray expression at the top of the file. It is also hit if an AST_Assign's left side is not an AST_SymbolRef (the harness calls croak(node) at line 297 for malformed assignments).","commonSituations":"Contributors porting a new terser test case forget the required `name: { ... }` labeled-statement form, or a terser upstream format change adds top-level declarations the harness does not recognize. A syntax error that still parses to an unexpected node shape also surfaces here.","solutions":["Open the file/line reported in the message and confirm every top-level construct is a labeled test block (`testName: { input: ...; expect: ... }`).","If the offending node is an assignment, ensure its left-hand side is a bare symbol reference, not a member expression or destructuring pattern.","If terser changed its test-file DSL, extend get_one_test/parse_test in scripts/terser-tests.js to handle the new node shape.","Re-run `node scripts/terser-tests.js` to confirm the file now parses."],"exampleFix":"// before (top-level bare statement breaks parser)\nconsole.log('setup');\nmy_test: {\n  input: { a: 1 };\n  expect: { a: 1 };\n}\n\n// after (everything is a labeled test block)\nmy_test: {\n  input: { a: 1 };\n  expect: { a: 1 };\n}","handlingStrategy":"validation","validationCode":"// Before walking, confirm every top-level AST node is a labeled statement\nconst U = require('uglify-js');\nfunction validateTerserTestFile(file) {\n  const ast = U.parse(fs.readFileSync(file, 'utf8'), { filename: file });\n  for (const stmt of ast.body) {\n    if (!(stmt instanceof U.AST_LabeledStatement)) {\n      throw new Error(`${file}:${stmt.start.line}:${stmt.start.col} top-level node is not a labeled test block`);\n    }\n  }\n}","typeGuard":"const isLabeledTopLevel = (node, parent) =>\n  node instanceof U.AST_LabeledStatement && parent instanceof U.AST_Toplevel;","tryCatchPattern":"try {\n  const tests = parse_test(file);\n} catch (e) {\n  if (/Can't understand test file/.test(e.message)) {\n    console.error('Test file is malformed:', e.message); // fix the file, do not swallow\n  }\n  throw e;\n}","preventionTips":["Author new terser test cases by copying an existing passing test block to inherit the correct label:block shape.","Run scripts/terser-tests.js locally on the single file before pushing to CI.","Keep all top-level content inside a labeled block; never leave helper code at the file root."],"tags":["test-harness","terser","parser","ast","contributor"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}