Skip to main content

What it flags

A try that reads a file with fs.readFileSync, fs.promises.readFile or readFile from node:fs/promises and parses it with JSON.parse, the yaml, js-yaml, json5 or TOML parsers, or fs-extra’s readJson, whose catch swallows every error and returns or assigns a default value, when the same path is written afterwards. A promise chain counts when it passes the file’s contents directly to a parser before .catch(() => ({})). The write can be writeFile, writeFileSync, createWriteStream, a rename or copy onto the path, fs-extra’s write helpers, write-file-atomic, or a function in the project that passes its parameter to one of them. The rule looks for writes after the fallback in the same function or a reader call that runs before a write or as one of its arguments. This includes writes of state the module keeps when the call order is visible. If an exported reader has no calls in the file, the rule also considers a writer exported from the same module. The path matches by static string, stable binding or readonly property such as this.file. A reader parameter can stand for an argument when there is one call site in the file. A direct process.chdir call at module level or in the reader, writer or an in-file caller used to establish their order requires a matching absolute path. The rule recognizes file contents passed directly to a parser or held in a const before parsing. A parse failure can leave a recoverable file on disk, for example after a typo in a hand edit or an interrupted save. The next write can replace those contents with data computed from the fallback or with an independent payload. The rule recognizes imported aliases, require('fs') and namespace imports by their bindings. The rule skips rethrows, exit handlers, error checks such as ENOENT or instanceof SyntaxError, and handlers that first preserve the broken file with a recognizable move or copy. It skips cache and temporary paths and options that preserve existing contents such as flag: 'wx', 'a', or force: false on cp. It checks common unconditional write wrappers and direct awaited async reader calls. An intervening branch that may exit or a conditional reader call can prevent the rule from establishing that the write follows recovery. The finding is contextual because the program may intend to rebuild the file.

How to review or fix

Fall back to the default only when the error code is ENOENT, and rethrow or report a file that does not parse. If the program must continue, keep the broken file (rename it aside) or skip the write until the file parses.

Configuration

Set the rule to "off" in loadwise.config.json to drop its findings: