oxc-project/oxc · warning · OxcDiagnostic
Unexpected irregular whitespace
Error message
Unexpected irregular whitespace
What it means
Diagnostic from oxlint's eslint/no-irregular-whitespace rule (crates/oxc_linter/src/rules/eslint/no_irregular_whitespace.rs:20). It reports Unicode whitespace/line-terminator characters that are valid in JS strings and comments but illegal or invisible in code positions: NBSP (U+00A0), zero-width space (U+200B), BOM (U+FEFF), line separator (U+2028), and similar. Such characters are visually identical to a normal space, causing confusing 'unexpected token' errors or broken string comparisons.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_irregular_whitespace.rs:20
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::{
identifier::is_irregular_whitespace, line_terminator::is_irregular_line_terminator,
};
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
fn no_irregular_whitespace_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected irregular whitespace")
.with_help("Try to remove the irregular whitespace")
.with_label(span)
}
#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoIrregularWhitespaceConfig {
/// Whether to skip irregular whitespace in string literals.
skip_strings: bool,
/// Whether to skip irregular whitespace in comments.
skip_comments: bool,
/// Whether to skip irregular whitespace in regular expression literals.
skip_reg_exps: bool,
/// Whether to skip irregular whitespace in template literals.
skip_templates: bool,
/// Whether to skip irregular whitespace in JSX text.
#[serde(rename = "skipJSXText")]
skip_jsx_text: bool,View on GitHub (pinned to e1e7af627c)
Solutions
- Replace the irregular whitespace character with a normal space (U+0020) at the reported span; most editors' 'show invisibles' mode highlights it
- Run a strip pass over affected files: sed -i 's/\xC2\xA0/ /g' (and similar for U+200B/U+FEFF) or use an editor 'strip trailing/zero-width characters' command
- Configure the rule's skipStrings/skipComments/skipRegExps/skipTemplates/skipJSON options if the characters are intentional data (e.g. NBSP inside French text strings)
Example fix
// before (contains U+00A0 between const and x) const\u00A0x = 1; // after const x = 1;
Defensive patterns
Strategy: validation
Validate before calling
// Pre-commit check: fail if a source file contains irregular whitespace in code
import { readFileSync } from 'node:fs';
const IRREGULAR = /[\u0085\u00A0\u1680\u180E\u2000-\u200F\u2028\u2029\u202F\u205F\u3000\uFEFF]/;
for (const file of process.argv.slice(2)) {
if (IRREGULAR.test(readFileSync(file, 'utf8'))) {
console.error(`irregular whitespace in ${file}`);
process.exitCode = 1;
}
} Prevention
- Enable 'show invisibles' / render whitespace in your editor
- Retype (don't paste) code from PDFs and chat tools, or paste through a plain-text buffer
- Add a pre-commit hook that strips or rejects NBSP/zero-width characters
- Configure skipStrings/skipComments options where the characters are legitimate data
When it happens
Trigger: Copying code from a web page, PDF, or chat message into a .js/.ts file; an editor or IME inserting NBSP inside identifiers, between keywords, or in operator position (e.g. 'const\u00A0x = 1'); BOM characters embedded mid-file; zero-width characters left by refactoring tools.
Common situations: Teams pasting snippets from documentation sites (common source of NBSP); macOS option-space typing NBSP instead of space; files that pass one editor but fail another that renders/strips the characters differently; CI failing on a file that looks clean locally.
Related errors
- Invalid regular expression: `u` and `v` flags should be used
- Unexpected surrogate pair in character class.
- Unexpected combining class in character class.
- `debugger` statement is not allowed
- Variables should not be deleted
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/02624ae5d4819cbc.
Report an issue: GitHub.