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

  1. Replace the irregular whitespace character with a normal space (U+0020) at the reported span; most editors' 'show invisibles' mode highlights it
  2. 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
  3. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/02624ae5d4819cbc. Report an issue: GitHub.