Repository navigation
fix(core): parse import attributes in import types (#422) - #555
Merged
Merged
Conversation
acorn-typescript's `tsParseImportType` expects the `)` right after the
module specifier, so `import("foo", { with: { type: "json" } })`, with a
qualifier or type arguments after it, or after `typeof`, failed with
`Unexpected token`. Override it in `TSRXPlugin` until
sveltejs/acorn-typescript#110 is released, with the same AST: the object
expression on `TSImportType.options`, and `options: null` without one.
Like TypeScript, it takes an object literal and no trailing comma after
either argument.
Every consumer of `TSImportType` now handles `options`:
- esrap's TSImportType printer drops them, so the TSX printer prints them,
and segments.js maps them back to the source.
- `@tsrx/prettier-plugin` lays the specifier and the attributes out like
call arguments, as Prettier does, without a trailing comma.
- tsrx-prettier-plugin's adapter already passed them to Prettier's printer;
two of the three Prettier cases now pass. `comma.ts` redeclares
`type A`, so it now fails on #415 instead.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rkaround Co-Authored-By: Claude Opus 5.5 <[email protected]>
…attributes-422 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged
leonidaz
added a commit
that referenced
this pull request
Sep 25, 2026
Regenerated prettier-known-failures.json: the 7 cases #511 skips remain. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Sep 25, 2026
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.
The bug
An import type with import attributes failed to parse with
Unexpected tokenat the comma after the module specifier (#422):@sveltejs/acorn-typescript1.0.13'stsParseImportTypeexpects the)right after the string.The override
TSRXPluginoverridestsParseImportType(packages/tsrx/src/plugin.js), marked// UPSTREAM(sveltejs/acorn-typescript#110): remove once a release includes the fix. The upstream fix is the commit "fix: parse import attributes on an import type" (61f804f) in sveltejs/acorn-typescript#110, which superseded the closed #104, so no upstream issue was filed. The override produces the same AST, so removing it later changes nothing for valid input:ObjectExpressiononTSImportType.options(the name acorn'sImportExpressionand typescript-estree use),options: null,argument,options,qualifier,typeArguments.It accepts what TypeScript accepts: an object literal after the comma, with trailing commas and line breaks inside it. Like TypeScript (5.9.3 and 7.0 native preview), it rejects
import("foo",),import("foo", { … },)(microsoft/TypeScript#61489), andimport("foo", attributes). Upstream #110 is looser there:import("foo",)givesoptions: null, and any expression atom is accepted as the attributes. A test pins TypeScript's behavior, so whoever removes the override sees the difference.The
TSImportTypetype now declaresoptions: ObjectExpression | nullandtypeArguments, andParse.ParserdeclarestsParseImportType,tsParseEntityName, andtsMatchLeftRelational.Upstream workarounds row for #511:
plugin.jstsParseImportTypeEvery consumer of
TSImportTypeTSImportTypeprinter dropsoptions, so the attributes would silently vanish from the output. That's true in 2.3.2, the locked version, and in 2.3.13 andmain, and it happens with typescript-estree ASTs too, so I filed TSImportType drops the import attributes (options) sveltejs/esrap#231.tsx_with_ts_locations(transform/jsx/helpers.js) prints them, as it does for esrap's other gaps, with a comment to remove it once esrap prints them. Without attributes it falls back to esrap's printer.TSImportTypebranch ofsegments.jsvisitsoptions, so the attributes map back to the source (the object,with, the keys, and the values).@tsrx/prettier-plugin.printTSImportTypeprintedimport(+ specifier +)and would have dropped the attributes. With attributes it now prints like Prettier: the specifier and the attributes go throughprintCallArguments(a newgetCallArgumentsreturns[argument, options]), with no trailing comma, hugging the object when only it breaks, and one argument per line when the specifier is long. Without attributes the output doesn't change. Expected output comes from Prettier 3.9.6'stypescriptparser on the same input. I also compared it on more variants (comments before and after either argument,singleQuote,semi: false,trailingCommaallandnone,printWidth30 and 40): all match Prettier and are idempotent.tsrx-prettier-plugin. Its adapter already reshapesTSImportType(argument→source,options ??= null), so no change was needed. Prettier's printer now gets the attributes and prints them like Prettier on the same variants.import-type-attributes.tsandlong-module-name5.tspass.comma.tsstill fails, now because it declarestype Atwice, which is [Bug]: tsrx-core: Parser throws on mistakes that TypeScript reports only as type-check diagnostics, even withcollect: true#415, not because of the attributes.options. The keys are non-computedPropertykeys, so no identifiers are misread as references.analyzeTsrxreports nothing, and ESLint'sno-undefoutput is the same with and without attributes. No visitor-key table listsTSImportType.Known failures (
tsrx-prettier-plugin)Refreshed with
UPDATE_KNOWN_FAILURES=1, then run normally: 75 → 73. Removed:typescript/import-type/attributes/import-type-attributes.ts format 1typescript/import-type/long-module-name/long-module-name5.ts format 1typescript/import-type/comma/comma.ts format 1stays, now for #415 (type 'A' has already been declared).Verification
AST comparison. I parsed 8,564 unique sources with
origin/main(d1f89bdd) and with this branch, in three modes (the formatter's options{ collect: true, errors: [], comments: [], preserveParens: true }, strict, and loose), and compared the fully serialized AST, comments, and errors. The sources were all 69 tracked.tsrxfiles, fixture sources, 6,948 code-like strings from test files, and all 2,040 Prettier snapshot inputs. 38 sources differ, all intended:main.options: nullon each import type without attributes: 3@tsrx/prettier-plugintest strings,comments/first-argument.ts,import-type.ts, andlong-module-name.tsthroughlong-module-name4.ts.import-type-attributes.tsandlong-module-name5.tsnow parse.comma.tsnow fails on the duplicatetype A([Bug]: tsrx-core: Parser throws on mistakes that TypeScript reports only as type-check diagnostics, even withcollect: true#415)..tsrxfile changed.After merging
main(9338fdd3), I ran it again on 8,702 sources and got the same result.Tests (
--maxWorkers=4):tsrx-utils,tsrx-analyze,tsrx-vite,tsrx-react,tsrx-preact,tsrx-solid,tsrx-vue,tsrx-hono,prettier-plugin,tsrx-prettier-plugin,language-server, andtypescript-pluginall pass: 8,419 passed, 73 expected fails, 70 skipped.New tests:
typeof import(…); a template body;import("foo")withoptions: null; and the three inputs TypeScript rejects.@tsrx/prettier-plugin: 10 tests.After the merge,
tsrx-utils,prettier-plugin,tsrx-prettier-plugin,tsrx-react, andtsrx-vuepass again, and so do the core andprettier-plugintypechecks.pnpm typecheckpasses, all 36 steps. This worktree was installed with--ignore-scripts, so thetsrx-tscbin wasn't linked, and I ran thevite-plugin-honotypecheck:testsstep asnode ../typescript-plugin/dist/tsc.js --noEmit -p tests/tsconfig.json.pnpm format:checkandpnpm changeset:checkpass.Changeset:
patchfor@tsrx/coreand@tsrx/prettier-plugin.Findings
Each has its own issue, verified on
mainatd1f89bdd:TSImportType drops the import attributes (
options) sveltejs/esrap#231: esrap'sTSImportTypeprinter dropsoptions(see above). It's worked around here.[Bug]: tsrx-core: The type arguments of an import type get no editor mappings, so tsrx-tsc drops their errors #552: the type arguments of an import type get no editor mappings. The
TSImportTypebranch ofsegments.jsvisitstypeParameters, but the parser setstypeArguments. Sotsrx-tscdropsCannot find nameinimport('./store.ts').Store<Missing>, and hover and go-to-definition don't work there. This PR doesn't change that line.[Bug]: @tsrx/prettier-plugin: Formatter never breaks between the arguments of
import(source, options)like Prettier #553:@tsrx/prettier-pluginnever breaks between the arguments of a dynamicimport(source, options)like Prettier. That's the same layout this PR gives import types, butImportExpressionhas its own printer.[Bug]: tsrx-core: Parser rejects a trailing comma in
import()(import("./a.js",)) #554: the parser rejects a trailing comma inimport()(import("./a.js",),import("./a.js", options,)). acorn-typescript 1.0.13'sparseDynamicImportreplaces acorn's, and its fix is also in test: run the parser over the TypeScript compiler corpus sveltejs/acorn-typescript#110 ("fix: defer dynamic import parsing to acorn").This is Phase 1 item 1.2 of #511.
Closes #422
🤖 Generated with Claude Code
Note
Medium Risk
Touches parsing, codegen, editor mappings, and formatting for a TypeScript syntax edge case; behavior is heavily tested but spans several pipeline stages.
Overview
Fixes
Unexpected tokenwhen import types include TypeScript 5.3+ import attributes (e.g.import("foo", { with: { type: "json" } })), including aftertypeofand with qualifiers.The parser overrides
tsParseImportTypeso the second argument is stored onTSImportType.options(object literal ornull), matching typescript-estree and rejecting trailing commas after the specifier or attributes like TypeScript. Compile output keeps attributes via an esrap workaround in the transform printer; source maps visitoptionsinsegments.js.@tsrx/prettier-pluginroutes import types with attributes throughprintCallArguments/getCallArguments, laying out specifier and attributes like Prettier call args without a trailing comma. Prettier conformance tests for import-type attributes are removed from known failures.Reviewed by Cursor Bugbot for commit 21e4c49. Bugbot is set up for automated code reviews on this repo. Configure here.