Skip to content

fix(core): parse import attributes in import types (#422) - #555

Merged
leonidaz merged 4 commits into
mainfrom
fix/core-import-type-attributes-422
Sep 25, 2026
Merged

leonidaz merged 4 commits into
mainfrom
fix/core-import-type-attributes-422

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

The bug

An import type with import attributes failed to parse with Unexpected token at the comma after the module specifier (#422):

type A = import("foo", { with: { type: "json" } });
type B = import("foo", { with: { "resolution-mode": "import" } }).Bar;
let c: typeof import("foo", { with: { type: "json" } });

@sveltejs/acorn-typescript 1.0.13's tsParseImportType expects the ) right after the string.

The override

TSRXPlugin overrides tsParseImportType (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:

  • the attributes are the ObjectExpression on TSImportType.options (the name acorn's ImportExpression and typescript-estree use),
  • an import type without them gets options: null,
  • the property order is 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), and import("foo", attributes). Upstream #110 is looser there: import("foo",) gives options: 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 TSImportType type now declares options: ObjectExpression | null and typeArguments, and Parse.Parser declares tsParseImportType, tsParseEntityName, and tsMatchLeftRelational.

Upstream workarounds row for #511:

Override in plugin.js tsrx issue Upstream issue Removed when
tsParseImportType #422 sveltejs/acorn-typescript#110 (61f804f) a release includes #110's fix

Every consumer of TSImportType

  • Compiled and type-only TypeScript (esrap). esrap's TSImportType printer drops options, so the attributes would silently vanish from the output. That's true in 2.3.2, the locked version, and in 2.3.13 and main, 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.
  • Editor mappings. The TSImportType branch of segments.js visits options, so the attributes map back to the source (the object, with, the keys, and the values).
  • @tsrx/prettier-plugin. printTSImportType printed import( + specifier + ) and would have dropped the attributes. With attributes it now prints like Prettier: the specifier and the attributes go through printCallArguments (a new getCallArguments returns [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's typescript parser on the same input. I also compared it on more variants (comments before and after either argument, singleQuote, semi: false, trailingComma all and none, printWidth 30 and 40): all match Prettier and are idempotent.
  • tsrx-prettier-plugin. Its adapter already reshapes TSImportType (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.ts and long-module-name5.ts pass. comma.ts still fails, now because it declares type A twice, which is [Bug]: tsrx-core: Parser throws on mistakes that TypeScript reports only as type-check diagnostics, even with collect: true #415, not because of the attributes.
  • Scope, analyzer, and ESLint. These walk every key generically (zimmerframe, and ESLint's fallback keys), so they reach options. The keys are non-computed Property keys, so no identifiers are misread as references. analyzeTsrx reports nothing, and ESLint's no-undef output is the same with and without attributes. No visitor-key table lists TSImportType.

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 1
  • typescript/import-type/long-module-name/long-module-name5.ts format 1

typescript/import-type/comma/comma.ts format 1 stays, 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 .tsrx files, fixture sources, 6,948 code-like strings from test files, and all 2,040 Prettier snapshot inputs. 38 sources differ, all intended:

    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, and typescript-plugin all pass: 8,419 passed, 73 expected fails, 70 skipped.

  • New tests:

    • Parser, in all three modes: each repro; trailing commas and line breaks inside the attributes; a qualified name and type arguments after them; typeof import(…); a template body; import("foo") with options: null; and the three inputs TypeScript rejects.
    • Every target: the compiled TypeScript keeps the attributes and has no syntax diagnostics, and the virtual TypeScript keeps them and maps each part back to the source.
    • @tsrx/prettier-plugin: 10 tests.
  • After the merge, tsrx-utils, prettier-plugin, tsrx-prettier-plugin, tsrx-react, and tsrx-vue pass again, and so do the core and prettier-plugin typechecks.

  • pnpm typecheck passes, all 36 steps. This worktree was installed with --ignore-scripts, so the tsrx-tsc bin wasn't linked, and I ran the vite-plugin-hono typecheck:tests step as node ../typescript-plugin/dist/tsc.js --noEmit -p tests/tsconfig.json. pnpm format:check and pnpm changeset:check pass.

  • Changeset: patch for @tsrx/core and @tsrx/prettier-plugin.

Findings

Each has its own issue, verified on main at d1f89bdd:

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 token when import types include TypeScript 5.3+ import attributes (e.g. import("foo", { with: { type: "json" } })), including after typeof and with qualifiers.

The parser overrides tsParseImportType so the second argument is stored on TSImportType.options (object literal or null), 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 visit options in segments.js.

@tsrx/prettier-plugin routes import types with attributes through printCallArguments / 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.

leonidaz and others added 3 commits September 25, 2026 12:39
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]>
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.

[Bug]: tsrx-core: Parser rejects import attributes in import types (import("foo", { with: { type: "json" } }))

1 participant