Repository navigation
Fix semicolon-free imports and MDX parsing - #3
Merged
Merged
Conversation
- Scan statements so imports without semicolons end at a line break, a trailing comment or an incomplete statement instead of swallowing the code after them - Track MDX code fences (backtick and tilde, run length, indentation) and ESM block boundaries, so prose, inline code, `import()` and `import.meta` are no longer read as imports - Keep the last line when a stripped comment ends a file without a trailing newline, and keep emphasis comments after semicolon-free imports - Preserve CRLF line endings and escaped quotes when stripping comments - Parse in a single pass so parse time grows linearly, guarded by new optimization tests - Update the enhanceCodeEmphasis snapshots and drop the AGENTS.md warnings these fixes make stale Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes how
parseImportsAndCommentsreads imports, comments and MDX structure, and makes it linear. Found while building@fairgarden/design's demos, most of which omit semicolons.export … fromare read correctly. Stray words and strings no longer become fake imports.mainon the real corpus. The branch had introduced quadratic cases, andmainalready had some.Statements
findModuleStatementEndreplaces two near-duplicate loops and skips strings, template literals and comments whole.import React,\n { useState } …,import\n * as React …andexport type\n { A } from …work. Only binding names,type/as,*, commas and one{ … }may appear, andfromcounts only when a quoted path follows it.=,(,;or the end of the file. So an incomplete import can't swallow the rest of the file, andimport x = require()is left alone.;, end of line or a trailing comment ends the statement. Awith/assert { … }clause may continue on the next line.isExportFromStatementaccepts onlyexport [type] * [as x] from '…'andexport [type] { … } from '…'.Fixed along the way (several were on
main):fromin an exported body, JSX text, strings or comments. For example,<p>Rows loaded from './rows.json'</p>broke the build, and// Adapted from "React docs"became an uninstallable dependency in the sandboxpackage.json.import.meta, which was read as an import.filename=orvariant=.;, are stripped, and CRLF endings are kept.MDX
`or~at the start of a line.mdfiles don't take the Markdown path at all.isMdxEsmStartis the only check, and it requiresimportorexportfollowed by a space at the start of a line. Soimport{Fake}from './fake'in prose stays text.import()is detected anywhere inside an ESM block, but not in prose, fences or JSX expressions. remark-mdx treats expressions as render-time code.@fairgarden/design, plus the fixtures.absolutizeImports).Performance
The output is unchanged by the rework: byte-identical across 2,532 inputs × 3 option sets, including CRLF copies.
mainexport {bracesoptimization.test.tsreplaces the 500 ms wall-clock asserts with complexity guards. Each parses an input at n and 16n and fails if the time grows 64× or more; linear growth is about 16×. They fail against the pre-rework parser.Behavior check against
mainmainmissed are found.@fairgarden/designdemos (829 files): all 617 semicolon-style files are identical. The semicolon-free files change as intended (for example, relative imports detected go from 1 to 209).Test changes to review
enhanceCodeEmphasis.test.tsrecorded the dropped-last-line and unstripped-comment bugs. The "wrap complete elements" assertion only passed because an unstripped comment contained<mark>./* @highlight */before an import's;stays, contrary toremoveCommentsWithPrefix's docs.AGENTS.md: the stale warnings about trailing// @highlightandcreateDemo(import.meta.url, …, {are removed, since both bugs are fixed.Known limitations
import/exportline that directly continues a prose paragraph is read as ESM; MDX reads it as text. Indentedimport …lines are also read as ESM (deliberate, per the docstring). No real page does either.import()inside a template${…}isn't detected, in.tsfiles too.Verification
pnpm test --runpnpm typescript/pnpm eslint/ prettierpnpm docs:lib/pnpm docs:buildpnpm docs:validate