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

  1. Use template.statements to allow an arbitrary number of statements.
  2. Use template.smart which returns a single statement or an array automatically.
  3. 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

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


AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03). Data as JSON: /data/errors/a485b139c554ef0c.json. Report an issue: GitHub.