Skip to content

Add Swift highlighting - #29

Merged
tannerlinsley merged 2 commits into
TanStack:mainfrom
Sheraff:add-swift
Oct 7, 2026
Merged

tannerlinsley merged 2 commits into
TanStack:mainfrom
Sheraff:add-swift

Conversation

@Sheraff

@Sheraff Sheraff commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Changes

Swift fences currently fall back to plaintext. This adds an isolated swift definition (no aliases), registers it in the default entry, and exposes it as a selective import (@tanstack/highlight/languages/swift). It follows the same shape as the batch in #27: one module on the existing collectPatternRanges helpers with a small linear scanner for comments and strings, no new token classes, no new dependencies, no cross-language imports, and no changes to core or to existing language modules.

Context-aware behaviour: interpolated, multi-line, and raw #"…"# strings with nested quotes and parentheses inside \( ), nested block comments, #/…/# and bare /…/ regex literals vs division, argument labels, contextual keywords (open, package, set, some, … only in keyword position), attributes, and # directives and macros.

The branch is one commit for the module and tests, then one registration commit (default entry, barrel, size and bench profiles, fixture, docs tables, changeset).

Size tradeoff

Core + Swift, local Node 26: 7.30 KB minified / 3.41 KB gzip / 3.13 KB Brotli, with a 3.55 KB gzip budget. Core and every existing selective profile are unchanged. The all-language convenience entry grows from 49.99 KB minified / 16.82 KB gzip to 52.74 KB / 17.73 KB gzip, and its budget rises from 17.1 KB to 18.0 KB gzip.

Deliberate limits

  • An interpolated string is a single string token; a comment inside \( ) is not recognised.
  • Bare /…/ regexes are recognised only after =, (, ,, :, [, {, return, or case, and never span lines; elsewhere / is division. #/…/# has no such limit.
  • Text( / Foo( are type (same choice as the Dart module); all-caps names like URL( stay plain; single-letter generics stay plain.
  • Generic < / > are operators; ?. is ? plus a plain .; tuple access point.0 stays plain.
  • Trailing closures are function only for .name { at line start or when the closure starts with $0, !$0, a capture list, or params in.
  • Contextual modifiers are keywords only before a lowercase word, an uppercase name, (, or [; get / set only in accessor positions.
  • Backticked names are never keywords; word boundaries are ASCII-only.

Notes

Validation

  • pnpm run verify passes locally on Node 26.4.0: typecheck, build, docs verification, skills validation, publint, package and packed-runtime checks, 216 tests in 20 files, 26 size profiles, and 21 benchmark profiles.
  • test/swift.test.ts: 14 tests with a realistic showcase, focused regressions for every context-aware behaviour, negative cases for delimiters inside the wrong context, and plaintext fallback; every assertion also checks byte-for-byte source reconstruction.
  • An independent review pass against realistic SwiftUI, async/Codable, library-README, and Package.swift samples; every issue found has a regression assertion.
  • The tokenizer was timed on ~60k-character repeated-delimiter inputs (unterminated strings, raw strings, regexes, interpolation openers, nested comments) and stays under 20 ms per input.
  • Dedicated benchmark profile: 159 ms median for 10,000 blocks (budget 1,200 ms).
  • pnpm run changeset:status: one minor bump for @tanstack/highlight.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added Swift syntax highlighting with support for strings, interpolation, regular expressions, nested comments, attributes, and other Swift syntax.
    • Added Swift as a supported language in the language list and individual language exports.
  • Documentation
    • Updated language support and bundle-size information to include Swift and the expanded language set.

Sheraff and others added 2 commits October 7, 2026 00:13
Interpolated, multi-line, and raw strings with nested quotes in interpolations, regex literals vs division, nested block comments, contextual keywords, attributes, and directives.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Adds Swift to the default entry and language barrel, with a size and benchmark profile, a representative fixture, docs tables, and a minor changeset. The all-language convenience entry grows from 16.82 KB to 17.73 KB gzip and its budget rises to 18.0 KB; core and existing selective profiles are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Oct 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0b8d429

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/highlight Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 27b8f457-4e75-4716-b463-35d666ebfa41
📥 Commits

Reviewing files that changed from the base of the PR and between 803baba and 0b8d429.

📒 Files selected for processing (18)
  • .changeset/swift.md
  • README.md
  • docs/guides/performance.md
  • docs/installation.md
  • docs/language-support.md
  • docs/reference/default-entry.md
  • docs/reference/languages.md
  • docs/test-strategy.md
  • scripts/bench.mjs
  • scripts/language-utils.mjs
  • scripts/measure-size.mjs
  • skills/configure-selective-highlighting/references/languages.md
  • src/index.ts
  • src/languages/index.ts
  • src/languages/swift.ts
  • test/fixtures.ts
  • test/real-doc-fixtures.test.ts
  • test/swift.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

This change adds a Swift language definition to the highlighter, registers it in the language union, all-language registry, and exports, and adds Swift tests, benchmark coverage, bundle-size limits, and documentation.

Changes

Swift language support

Layer / File(s) Summary
Swift tokenization and coverage
src/languages/swift.ts, test/swift.test.ts, test/fixtures.ts, test/real-doc-fixtures.test.ts
The tokenizer identifies Swift syntax, including nested block comments, string interpolation, raw and multiline strings, and regex literals. Tests cover these forms and additional Swift syntax.
Language registration and catalog
src/index.ts, src/languages/index.ts, scripts/language-utils.mjs, docs/language-support.md, docs/reference/*, skills/configure-selective-highlighting/references/languages.md, README.md, .changeset/swift.md
Swift is added to the language union, all-language registry, exports, and supported-language list. The language references and changeset include Swift.
Benchmark and bundle-size tracking
scripts/bench.mjs, scripts/measure-size.mjs, README.md, docs/guides/performance.md, docs/installation.md, docs/test-strategy.md
A Swift benchmark and bundle-size profile are added. The all-language size figures and size budgets are updated.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Feature

Suggested reviewers: tannerlinsley

Merge Risk: ⚪ Minimal · up to 0b8d4

This adds Swift highlighting as an isolated, opt-in language module. Core and existing selective bundles are reported unchanged. No concrete merge-blocking risk was found.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding Swift highlighting.
Description check ✅ Passed The description covers the required Changes and Validation sections. It explains the behavior, scope, deliberate limits, size impact, tests, and benchmark results.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 9 files. (9 skipped: 9 …
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tannerlinsley
tannerlinsley merged commit 752bc83 into TanStack:main Oct 7, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants