Skip to content

[LOW] template-insert.js:325-328: implicit else branch silently handles 'append' mode with no comment or label #166

@JuliaKalder

Description

@JuliaKalder

Summary

insertTemplateIntoTab in modules/template-insert.js (lines 318–328) handles the four insert modes (replace, prepend, append, cursor) with an implicit else branch for "append" that carries no label or comment. A developer reading the function cold cannot tell what mode value reaches the final else without mentally tracing three separate call sites.

Impact

Maintenance burden. Any future addition of a new insertMode value (e.g. inline, header) that is not yet handled by an explicit branch would silently fall into the "append" path — with no error, no log, and no indication in the code that this is a fallback rather than the intended match. The existing cursor path is already handled by a separate outer if, so the reader must hold that context while reading the inner else if/else chain.

Evidence

modules/template-insert.js lines 318–328:

} else if (resolvedBody) {
  const body = await replaceVariables(resolvedBody, tabId);
  if (mode === "replace") {
    details.body = body;
  } else if (mode === "prepend") {
    const existing = await messenger.compose.getComposeDetails(tabId);
    details.body = body + (existing.body || "");
  } else {                                              // ← what mode is this?
    const existing = await messenger.compose.getComposeDetails(tabId);
    details.body = (existing.body || "") + body;
  }
}

To understand what reaches the bare else, a reader must trace:

  1. Line 221: const mode = template.insertMode || "append" — defaults missing modes to "append"
  2. Line 250: if (resolvedBody && mode === "cursor") — cursor mode is already dispatched above
  3. Line 318: else if (resolvedBody) — the current block covers all remaining modes

The sibling function executeImport in options.js (line 705) at least adds the comment // mode === "append" or no duplicate. insertTemplateIntoTab has no such annotation.

Recommended Fix

Change the implicit else to an explicit labeled form with a defensive fallback:

  } else if (mode === "append") {
    const existing = await messenger.compose.getComposeDetails(tabId);
    details.body = (existing.body || "") + body;
  } else {
    console.warn(`TemplateWing: unknown insertMode "${mode}", falling back to append`);
    const existing = await messenger.compose.getComposeDetails(tabId);
    details.body = (existing.body || "") + body;
  }

Alternatively, add a minimal comment if keeping the single else:

  } else {
    // "append" (default): add template body after existing content
    const existing = await messenger.compose.getComposeDetails(tabId);
    details.body = (existing.body || "") + body;
  }

This change is self-contained in modules/template-insert.js and takes under 5 minutes to apply.

References

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions