Skip to content

refactor(pattern): move the ECMA 262 regex dialect out of JsonValidator into EcmaRegex - #100

Merged
rawo merged 2 commits into
masterfrom
refactor/95-ecma-regex
Nov 6, 2026
Merged

rawo merged 2 commits into
masterfrom
refactor/95-ecma-regex

Conversation

@rawo

@rawo rawo commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Summary

Moves the ECMA 262 regex dialect out of JsonValidator.kt into internal object EcmaRegex in the new EcmaRegex.kt. This is a pure move.

  • Entry points (fun): isValidEcmaRegex (the regex format) and translatePatternToJava (used by compiledPattern)
  • Private helpers: hasOnlyEcmaRegexSyntax, translateEcmaSyntax
  • Private constants, at the bottom: REGEX_UNICODE_CATEGORY, ECMA_REGEX_ESCAPES, ECMA_WHITESPACE, UNICODE_CATEGORY_NAMES

patternCache, compiledPattern, patternMatches and matchesAnyPattern stay in JsonValidator. The cache is per-validator state (#40), and the undecided handling needs Evaluation (#32, #51).

Why: DRY, because pattern and the regex format share the dialect, and SRP, because it changes with the differences between ECMA 262 and Java's engine. Behaviour and public API are unchanged. CLAUDE.md (Architecture entry, Format Validation bullet, JsonValidator size) and memory/MEMORY.md are updated. There is no CHANGELOG entry.

Verification

  • A mechanical comparison against origin/master, with indentation and visibility normalised, found every moved declaration identical, KDoc and comments included. The only removed lines not found in EcmaRegex.kt are the two call sites, which are now qualified with EcmaRegex..
  • ./gradlew ktlintFormat made no changes.
  • CI=true ./gradlew test ktlintCheck --rerun-tasks passed, with the official suite and ecmascript-regex.json included: 5,849 tests, 0 failures, 0 errors, 0 skipped (from build/test-results/test/*.xml).
  • test_counts.py exited 0 (full run 5,849, custom 1,379).
  • JsonValidator.kt went from 2,222 to 2,031 lines. EcmaRegex.kt has 200 lines.

Closes #95

🤖 Generated with Claude Code

…or into EcmaRegex

Move the regex format's syntax check (isValidEcmaRegex, hasOnlyEcmaRegexSyntax), the ECMA 262 to Java
translation (translatePatternToJava, translateEcmaSyntax) and the constants only they use
(REGEX_UNICODE_CATEGORY, ECMA_REGEX_ESCAPES, ECMA_WHITESPACE, UNICODE_CATEGORY_NAMES) into
internal object EcmaRegex in EcmaRegex.kt.

DRY: the pattern keywords and the regex format share this dialect, so it belongs to neither.
SRP: it changes with the differences between ECMA 262 and Java's engine, not with validation.
patternCache, compiledPattern, patternMatches and matchesAnyPattern stay in JsonValidator: the cache
is per-validator state (#40) and the undecided handling needs Evaluation (#32, #51).

Function bodies and comments are unchanged; only visibility and qualification differ. Behaviour is
unchanged, and so is the public API.

Closes #95

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@rawo rawo added the refactor Restructuring without behaviour change label Nov 6, 2026
@rawo

rawo commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Review of #100 (head a2ec837, merge base fdf6c64)

Verdict: I found no blocking or should-fix issues. This is a pure move, and I list three documentation nits below.

How I verified the pure-move claim

  • Declaration diff. A script pulled each of the 8 moved declarations, with its preceding KDoc or // comment, out of base JsonValidator.kt and head EcmaRegex.kt. It then compared them in two ways:
    • with leading whitespace and private stripped;
    • more strictly, with each block dedented as a whole, so only a uniform indentation shift and the visibility change are allowed.
  • Result. All 8 are identical, comments included: isValidEcmaRegex (14 lines), hasOnlyEcmaRegexSyntax (36), translatePatternToJava (10), translateEcmaSyntax (68), REGEX_UNICODE_CATEGORY, ECMA_REGEX_ESCAPES, ECMA_WHITESPACE (with its KDoc) and UNICODE_CATEGORY_NAMES (46). The only visibility changes are isValidEcmaRegex and translatePatternToJava, which went from private to public inside the internal object. The other six stay private.
  • Line multiset. I compared the lines removed from JsonValidator.kt with the lines of EcmaRegex.kt, both normalised. Only two removed lines are missing from EcmaRegex.kt: the two call sites, which are re-added qualified with EcmaRegex.. The only extra lines in EcmaRegex.kt are the package line, the object's KDoc and the internal object EcmaRegex { … } wrapper. The diff adds nothing else to JsonValidator.kt.
  • Build. On a git archive export of the head, with the sibling suite in place, CI=true ./gradlew test ktlintCheck --rerun-tasks passed: 5,849 tests, 0 failures, 0 errors, 0 skipped. That includes OfficialTestSuiteRunner at 4,462/4,462 (with optional/ecmascript-regex.json), EcmaPatternTest and RegexFormatScanTest, and ktlint is clean.

Blocking

None. What I checked:

  • Nothing lost or duplicated. None of the 8 names is still declared in head JsonValidator.kt, and each is declared exactly once in EcmaRegex.kt. Nothing the validator still uses moved away: EMAIL_LOCAL_SPECIAL_CHARS, the REGEX_* format constants and MAX_MESSAGE_VALUE_LENGTH are still in the companion.
  • Call sites. git grep over the head finds no other caller of the moved names, in src/main, src/test (Kotlin and Java) or anywhere else.
    • JsonValidator.kt:97: compiledPattern still caches Regex(EcmaRegex.translatePatternToJava(it)) in the per-validator patternCache, so the key (the raw schema pattern) and the value are unchanged.
    • JsonValidator.kt:1135: FORMAT_REGEX -> EcmaRegex.isValidEcmaRegex(value), the same check as before.
    • patternMatches, matchesAnyPattern and every pattern/patternProperties/additionalProperties/unevaluatedProperties use (lines 793, 813, 1098, 1848, 1858) are untouched.

Should-fix

None. What I checked:

  • Visibility and API. EcmaRegex is internal, and only the two entry points are non-private. The removed JsonValidator members were all private, so the public API is unchanged, and the import block of JsonValidator.kt is untouched.
  • Native image. No reflection, runtime resources, ServiceLoader or proxies.
  • State. No mutable state: two read-only collections, one Regex and one const. No cache moved into the object, so patternCache stays per validator ([P2] Provide bounded ownership or retirement for live-validator schema caches #40).
  • Initialisation order. No val initializer in the object reads another member or calls a function. The functions read the tables only after <clinit> has finished, so declaring the constants at the bottom is safe. The only effect of the move is that the tables now initialise lazily on first use, instead of with JsonValidator's companion. JVM class init is thread-safe.
  • Style. It matches Idna.kt and UriSyntax.kt: file KDoc, internal object, public entry points first, private helpers next, private constants last.
  • KDoc. The object KDoc is accurate ("pure functions over immutable tables; compiled patterns are cached by the validator"). [translateEcmaSyntax] in the isValidEcmaRegex KDoc resolves inside the object. EcmaRegex.kt has no imports, and none are unused.
  • Docs.

Nits

  1. CLAUDE.md:123-125 lists what stays in the validator as patternCache, compiledPattern() and patternMatches(), but leaves out matchesAnyPattern(). The PR body and the commit message both include it. Suggest adding matchesAnyPattern() to that list.
  2. CLAUDE.md:383-384 says "Helper validators live in JsonValidator.kt … except the IDNA ones in Idna.kt and the regex dialect in EcmaRegex.kt". The URI/IRI/IP helpers in UriSyntax.kt are also an exception, which the bullet two lines below acknowledges. The gap predates this PR, but this PR edits the sentence. Suggest adding "… the URI/IP grammar in UriSyntax.kt".
  3. EcmaRegex.kt:24: translatePatternToJava is now the entry point compiledPattern calls from another file, yet it has no KDoc. The only description of the translation sits on the private translateEcmaSyntax. It had no KDoc before either, so adding one would go beyond a pure move. If you want one, a single line would do, for example: "Translates an ECMA 262 pattern to Java regex syntax: maps \p{...} category aliases, then [translateEcmaSyntax]." Otherwise, leave it as is.

The orphaned /** Validates the structure of a schema itself */ KDoc in JsonValidator.kt is out of scope, as stated: #98 removes it.

🤖 Generated with Claude Code

List matchesAnyPattern() among the pattern helpers that stay in JsonValidator, name UriSyntax.kt
among the files that hold format helpers, and document EcmaRegex.translatePatternToJava, which
JsonValidator now calls from another file.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@rawo
rawo merged commit 99a45e0 into master Nov 6, 2026
1 check passed
@rawo
rawo deleted the refactor/95-ecma-regex branch November 6, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Restructuring without behaviour change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: move the ECMA 262 regex dialect out of JsonValidator into EcmaRegex.kt

1 participant