Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

unicorn/text-encoding-identifier-case

Enforce one spelling for the encoding names utf8 and ascii

"UTF-8", "utf-8", "Utf8" and "utf8" all name the same encoding in most APIs. With one spelling, a search finds every use. The rule wants "utf8" and "ascii".

In three places it wants "utf-8", the name the web standards use: new TextDecoder("utf-8"), <meta charset="utf-8" /> and <form acceptCharset="utf-8">.

The rule reports any string that's exactly one of these names, wherever it appears, and JSX text such as <span>UTF-8</span>. Template literals aren't checked.

--fix rewrites the string.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/text-encoding-identifier-case" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/text-encoding-identifier-case" = ["error", {
  withDash = false,
}]

Examples

Reported:

const text = await fs.readFile(file, "UTF-8");
const bytes = Buffer.from(name, "ASCII");
const decoder = new TextDecoder("utf8");
const head = <meta charset="utf8" />;
const label = <span>UTF-8</span>;

Not reported:

const text = await fs.readFile(file, "utf8");
const bytes = Buffer.from(name, "ascii");
const decoder = new TextDecoder("utf-8");
const head = <meta charset="utf-8" />;
const label = <span>{`UTF-8`}</span>;

Options

OptionDefaultWhat it does
withDashfalseRequire "utf-8" everywhere instead of "utf8".
const text = await fs.readFile(file, "utf8");
const text = await fs.readFile(file, "utf-8");