babel/babel · error · Error
Found multiple statements but wanted one
Error message
Found multiple statements but wanted one
What it means
The 'statement' formatter expects exactly one statement, but after substitution the body contained two or more statements.
Source
Thrown at packages/babel-template/src/formatters.ts:44
export const smart = makeStatementFormatter(body => {
if (body.length > 1) {
return body;
} else {
return body[0];
}
});
export const statements = makeStatementFormatter(body => body);
export const statement = makeStatementFormatter(body => {
// We do this validation when unwrapping since the replacement process
// could have added or removed statements.
if (body.length === 0) {
throw new Error("Found nothing to return.");
}
if (body.length > 1) {
throw new Error("Found multiple statements but wanted one");
}
return body[0];
});
export const expression: Formatter<t.Expression> = {
code: str => `(\n${str}\n)`,
validate: ast => {
if (ast.program.body.length > 1) {
throw new Error("Found multiple statements but wanted one");
}
if (expression.unwrap(ast).start === 0) {
throw new Error("Parse result included parens.");
}
},
unwrap: ({ program }) => {
const [stmt] = program.body;
assertExpressionStatement(stmt);View on GitHub (pinned to 06b6eae39d)
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.
Example fix
// before
template.statement`STMT`({ STMT: [s1, s2] })
// after
template.smart`STMT`({ STMT: [s1, s2] }) Defensive patterns
Strategy: validation
Validate before calling
// If a statement placeholder may expand to multiple statements, use .smart/.statements const fmt = Array.isArray(replacementForStmt) ? template.smart : template.statement; fmt`STMT`(replacements);
Try / catch
try { template.statement`STMT`(replacements); }
catch (e) {
if (/Found multiple statements/.test(e.message)) { /* switch to template.smart or template.statements */ }
else throw e;
} Prevention
- 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.
When it happens
Trigger: A statement placeholder is replaced with an array of multiple statements, or the template literally contains multiple statements while template.statement is used.
Common situations: Replacing a single statement placeholder with [stmtA, stmtB]; writing a multi-statement template but choosing the single-statement formatter.
Related errors
- Found nothing to return.
- Parse result included parens.
- Unexpected replacement overlap.
- Template replacements must be an array, object, null, or und
- Error: No substitution given for "${placeholderName}". If th
AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03).
Data as JSON: /data/errors/a485b139c554ef0c.json.
Report an issue: GitHub.