fix(import-map): make the JSONC comment stripper honor string literals - #627
Open
edunovaes wants to merge 1 commit into
Open
fix(import-map): make the JSONC comment stripper honor string literals#627edunovaes wants to merge 1 commit into
edunovaes wants to merge 1 commit into
Conversation
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.
Fixes #629
Problem
parseTsConfigTextinextract-import-map.mjsstrips JSONC comments with a regex that does not honor string literals. The code comments acknowledge this ("The strip is naive (it doesn't honor string contents), so we fall back to the raw text on failure"), but the raw-text fallback does not actually save the case that matters — because a tsconfig with comments is exactly the one that fails a rawJSON.parse.tsconfig path aliases and include globs legitimately contain the comment delimiters:
{ "compilerOptions": { // any comment at all "paths": { "@/*": ["./src/*"] } }, "include": ["**/*.ts"] }The
/*inside"@/*"opens a spurious block comment, and the*/inside"**/*.ts"closes it. Everything between the two is deleted:So the stripped parse fails, the raw parse fails (real comments),
parseTsConfigTextreturnsnull, and every alias from that tsconfig is dropped. The only signal is one stderr line:The import map then silently contains only relative imports. Nothing downstream distinguishes "this project has few aliases" from "we threw all of them away".
Trigger conditions
Both must hold, and both are ordinary:
//or/* */comment, andpathsalias with a*and aninclude/excludeglob ("**/*.ts"is the create-next-app default).Real-world impact
Measured on a Next.js project whose
tsconfig.jsoncarried a three-line//comment explaining anallowImportingTsExtensionsdecision, alongside the stock"@/*": ["./src/*"]and"include": ["**/*.ts", "**/*.tsx"]:64 of 103 import edges — 62% of the project's import graph — were being discarded. Every one of the 66
@/…imports in the source resolved to nothing.Fix
Replace the regex with a character scanner that tracks string/escape state, so delimiters inside string literals are left alone. Newlines inside block comments are preserved, keeping parse-error line numbers pointing at the original file. The raw-text parse remains as the fallback.
No signature change, no new dependency, no behavioural change for a tsconfig that already parsed.
Tests
Adds three cases to
tests/skill/understand/test_extract_import_map.test.mjs. They are written as literal text rather thanJSON.stringify, which is why the existing tsconfig tests never caught this — every one of them constructs comment-free JSON.// commentnext to"@/*"and"**/*.ts"/* *///inside a string"$schema": "https://json.schemastore.org/tsconfig"The third is a guard on the fix itself rather than a reproduction — the old code passed it too, because a URL alone does not break the raw-text fallback.
Verified against the shipped script with a working runtime, comparing the old stripper to the new one with everything else held constant:
failed to parsewarningfailed to parsewarning//inside a stringNote
This is independent of the leading-
./alias fix (#214) already onmain— that one is about the candidate path, this one is about the config never being parsed in the first place. A project hitting both saw its import edges go from 39 to 103 only after each was addressed.