bun/no-literal-temp-dir
Disallow hard-coded temporary directories such as /tmp in favor of os.tmpdir()
Windows has no /tmp, and on other systems TMPDIR can point somewhere else, as it does for every user on macOS. A path that starts with /tmp ignores that. os.tmpdir() returns the directory that the system wants you to use.
The rule reports a string or a template that's one of the directories or starts with one followed by /. Text and attribute strings in JSX aren't checked.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"bun/no-literal-temp-dir" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"bun/no-literal-temp-dir" = ["error", {
directories = ["/tmp", "/var/tmp"],
}]Examples#
example.js2 problems
import { writeFileSync } from "node:fs";
writeFileSync("/tmp/report.json"/tmp is not the temporary directory on every system. Use os.tmpdir()., JSON.stringify(report));
writeFileSync(`/var/tmp/${name}.log`/var/tmp is not the temporary directory on every system. Use os.tmpdir()., lines.join("\n"));example.jsNo problems
import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
writeFileSync(join(tmpdir(), "report.json"), JSON.stringify(report));A name that only begins with the same letters is fine:
example.jsNo problems
const template = "/tmpl/email.html";Options#
| Option | Default | What it does |
|---|---|---|
directories | ["/tmp", "/var/tmp"] | The directories to report. It replaces the default list. |
example.jswith
{ "directories": ["/tmp", "/var/tmp", "/private/tmp"] }1 problemconst socket = "/private/tmp/worker.sock"/private/tmp is not the temporary directory on every system. Use os.tmpdir().;