This repository provides an ESLint rule that disallows empty string literals in runtime/value contexts for TypeScript and JSX/TSX. The rule integrates directly into your ESLint setup.
Ensure you have ESLint and the TypeScript ESLint parser/utilities installed. With Bun:
bun add -d eslint @typescript-eslint/parser @typescript-eslint/utils typescriptThen add this repository's plugin/rule to your project as appropriate (publish or local path depending on your workflow).
Extend your ESLint configuration to include the rule. Example (flat config):
// eslint.config.mjs
import tsEslint from "typescript-eslint";
import plugin from "./rules/index.js"; // or your published package entry
export default tsEslint.config([
{
plugins: {
"ubiquity-os": plugin,
},
rules: {
"ubiquity-os/no-empty-strings": [
"warn",
{
checkWhitespaceOnly: false,
checkNoSubstitutionTemplates: true,
ignoreZeroWidth: false,
allowContexts: [],
},
],
},
},
]);Rule key: ubiquity-os/no-empty-strings
All options are optional. Defaults shown.
-
checkWhitespaceOnly(boolean, defaultfalse)- When
true, whitespace-only strings (e.g.," ") are also reported.
- When
-
checkNoSubstitutionTemplates(boolean, defaulttrue)- When
true, empty no-substitution template literals (``````) are reported.
- When
-
ignoreZeroWidth(boolean, defaultfalse)- When
true, strings consisting only of zero-width characters (e.g.,\u200B,\u200C,\u200D,\u200E,\u200F,\uFEFF) are ignored.
- When
-
allowContexts(string[], default[])- Array of AST node types in whose context empty strings should be allowed. Any ancestor node type matching an entry will allow the empty string.
Notes:
- Type-only positions (e.g.,
type A = "") and import/export specifiers are not reported. - JSX attribute expressions like
<Comp title={expr} />are not reported; string literal attributes like<Comp title="" />are reported.
Valid:
const a = "x";
const a = `${x}`;
type A = "";
<Comp title={expr} />Invalid:
const a = "";
const a = myString ?? '';
const a = myString ? myString : '';
const a = ``; // when checkNoSubstitutionTemplates: true
<Comp title=""/>With checkWhitespaceOnly: true:
const a = " "; // invalid- Bun
- TypeScript
bun installbun testThis runs the ESLint RuleTester-based tests in tests/empty-string-eslint-rule.test.ts.
bun run format