{"id":"a485b139c554ef0c","repo":"babel/babel","slug":"found-multiple-statements-but-wanted-one","errorCode":null,"errorMessage":"Found multiple statements but wanted one","messagePattern":"Found multiple statements but wanted one","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/babel-template/src/formatters.ts","lineNumber":44,"sourceCode":"\nexport const smart = makeStatementFormatter(body => {\n  if (body.length > 1) {\n    return body;\n  } else {\n    return body[0];\n  }\n});\n\nexport const statements = makeStatementFormatter(body => body);\n\nexport const statement = makeStatementFormatter(body => {\n  // We do this validation when unwrapping since the replacement process\n  // could have added or removed statements.\n  if (body.length === 0) {\n    throw new Error(\"Found nothing to return.\");\n  }\n  if (body.length > 1) {\n    throw new Error(\"Found multiple statements but wanted one\");\n  }\n\n  return body[0];\n});\n\nexport const expression: Formatter<t.Expression> = {\n  code: str => `(\\n${str}\\n)`,\n  validate: ast => {\n    if (ast.program.body.length > 1) {\n      throw new Error(\"Found multiple statements but wanted one\");\n    }\n    if (expression.unwrap(ast).start === 0) {\n      throw new Error(\"Parse result included parens.\");\n    }\n  },\n  unwrap: ({ program }) => {\n    const [stmt] = program.body;\n    assertExpressionStatement(stmt);","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/babel/babel/blob/06b6eae39da4ce0689fad64e2c48a6375a464208/packages/babel-template/src/formatters.ts#L26-L62","documentation":"The 'statement' formatter expects exactly one statement, but after substitution the body contained two or more statements.","triggerScenarios":"A statement placeholder is replaced with an array of multiple statements, or the template literally contains multiple statements while template.statement is used.","commonSituations":"Replacing a single statement placeholder with [stmtA, stmtB]; writing a multi-statement template but choosing the single-statement formatter.","solutions":["Use template.statements to allow an arbitrary number of statements.","Use template.smart which returns a single statement or an array automatically.","Reduce the template/replacements so only one statement remains."],"exampleFix":"// before\ntemplate.statement`STMT`({ STMT: [s1, s2] })\n// after\ntemplate.smart`STMT`({ STMT: [s1, s2] })","handlingStrategy":"validation","validationCode":"// If a statement placeholder may expand to multiple statements, use .smart/.statements\nconst fmt = Array.isArray(replacementForStmt) ? template.smart : template.statement;\nfmt`STMT`(replacements);","typeGuard":null,"tryCatchPattern":"try { template.statement`STMT`(replacements); }\ncatch (e) {\n  if (/Found multiple statements/.test(e.message)) { /* switch to template.smart or template.statements */ }\n  else throw e;\n}","preventionTips":["Use template.smart when statement count is variable.","Check replacement arrays length before substitution.","Reserve template.statement for templates known to hold exactly one statement."],"tags":["formatter","statement","replacements"],"analyzedSha":"06b6eae39da4ce0689fad64e2c48a6375a464208","analyzedAt":"2026-08-03T20:13:43.465Z","schemaVersion":2}