Skip to content

ReferenceError: WIDTH is not defined when saving any JS/TS collection #23

Description

@Braintelligence

Editing a field of a record in a TypeScript data file (e.g. export const themen = [{ id, titel, text }, …]) and saving throws ReferenceError: WIDTH is not defined.

electron/jsCollections.js references WIDTH in literal() (lines 287 and 295) but never declares it:

if (pad.length + oneLine.length + 1 <= WIDTH && !oneLine.includes('\n')) return oneLine;
// …
if (!val.includes('\n') && inner.length + key.length + 2 + val.length + 1 > WIDTH) {

Any collection whose rows are objects is affected; only arrays of plain strings or numbers never reach that branch.

Fix: declare the constant next to ID_KEY. The comment above the usage already says it should match Prettier's default printWidth:

// Prettier's default printWidth; used by literal() to decide when a record
// stays on one line.
const WIDTH = 80;

With that in place, replaceCollection() writes the record back correctly, long strings dropping to their own line as intended:

  {
    id: "software",
    titel: "Software, Automatisierung und Auswertung",
    text:
      "Warenwirtschaft, Rechnungswesen, komplexe Automatisierungen sowie Berichte, Gutachten, Kataloge und barrierefreie PDFs.",
  },

Reproduces on main at e5e63d8 and in the shipped 0.1.23 build. There are currently no tests for jsCollections.js, which is why it went unnoticed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions