consistent-json-file-read
UnreleasedConfiguration
Rule Details
Use a consistent encoding when passing the result of readFile or readFileSync
to JSON.parse(). The default is to read the file as a UTF-8 string, which
matches the argument type of JSON.parse() in TypeScript.
The rule checks direct reads and reads assigned to local variables used only
for parsing. It handles both member calls and named imports from Node.js fs
and fs/promises.
Examples of incorrect code for the default option:
Examples of correct code for the default option:
Reads with additional options, such as signal or flag, are excluded so
automatic fixes can safely change the encoding without dropping those options.
Options
The rule accepts "string" (default) or "buffer".
Example of incorrect code with "buffer":
Example of correct code with "buffer":
Differences from upstream
- Rslint skips custom
JSONbindings and exported read results. For example,export const contents = fs.readFileSync(file); JSON.parse(contents);keeps the exportedBufferintact. Unicorn reports this read. - Rslint skips options objects with detectable mutations, including through an
alias. For example, if
const alias = options; alias.flag = 'r';adds an option to{ encoding: 'utf8' }before the read, Unicorn can remove the whole argument in"buffer"mode. Rslint preserves theflagoption. This also excludes writes after the read, such asObject.defineProperty(options, 'other', { set() {} }). - Rslint preserves option objects with computed keys that have side effects,
such as
{ [(log(), 'encoding')]: 'utf8' }. Unicorn can remove the argument in"buffer"mode. Rslint also preserves options with an explicit prototype, such as{ __proto__: { flag: 'r' }, encoding: null }, so the read keeps its inheritedflagoption. - Rslint skips reads whose options may be changed by another function. For
example, after
configure(options), the rule preserves the options argument so the read still honors anysignaladded byconfigure. - Rslint reports an encoding mismatch without a fix when removing the argument
would also remove a call such as
Object.freeze(options). A freeze in a separate options initializer still allows the fix.