Skip to content

Use compiled JS output for line number selection with TypeScript#3414

Closed
danielalanbates wants to merge 1 commit intoavajs:mainfrom
danielalanbates:fix/issue-3064
Closed

Use compiled JS output for line number selection with TypeScript#3414
danielalanbates wants to merge 1 commit intoavajs:mainfrom
danielalanbates:fix/issue-3064

Conversation

@danielalanbates
Copy link
Copy Markdown

Summary

Fixes #3064

When @ava/typescript is configured, line number selection (e.g. ava test.ts:10) fails because acorn cannot parse TypeScript syntax. This PR resolves the compiled JavaScript output file using the provider state's rewritePaths and parses that instead.

Changes

  • lib/worker/base.js: Added resolveCompiledFile() which uses the TypeScript provider state (extensions and rewritePaths) to map a .ts/.cts/.mts source file to its compiled .js/.cjs/.mjs output. The resolved path is passed to lineNumberSelection().

  • lib/worker/line-numbers.js: Accepts an optional compiledFile parameter. When provided:

    • Parses the compiled JavaScript instead of the TypeScript source
    • Looks up the source map for the compiled file (so positions translate back to the original TS source)
    • Matches callsites against both the original source file and the compiled file (extracted the comparison into a matchesFile() helper to avoid duplication)

How it works

  1. resolveCompiledFile() replicates the path rewriting logic from @ava/typescript's worker.load() — using the same rewritePaths and extension mapping that are already available in providerStates
  2. The compiled JS is parsed by acorn (which it can handle)
  3. Source map translation (already present) maps compiled JS positions back to TypeScript line numbers
  4. The user's selected line numbers (from the TS source) match correctly

Compatibility

  • No changes required to @ava/typescript — uses existing provider state data
  • When no TypeScript provider is configured, compiledFile is null and behavior is unchanged
  • All existing line number tests continue to pass (1615/1615 tap tests pass)

This PR was created with the assistance of Claude Opus 4.6 by Anthropic. Happy to make any adjustments! Reviewed and submitted by a human.

When @ava/typescript is configured, the line number parser now resolves
the compiled JavaScript file from the provider state's rewritePaths and
parses that instead of the TypeScript source. This fixes line number
selection (e.g. `ava test.ts:10`) for TypeScript projects, since acorn
cannot parse TypeScript syntax.

The source map lookup also uses the compiled file, so call expression
positions are correctly translated back to the original TypeScript
source lines. Callsite matching now checks both the original source
file and the compiled file to handle the runtime execution context.

Closes avajs#3064

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sindresorhus
Copy link
Copy Markdown
Member

A few things I think needs to be improved:

  • Move path resolution out of core and into provider capability. Core should not reimplement provider-specific rewritePaths logic.
  • Only resolve compiled targets when line-number selection is actually requested, not on every worker startup.
  • Add regression coverage for TypeScript line selection end to end (test.ts:line), including rewrite-path setups.
  • Add at least one edge-case test for overlapping rewrite rules so the most specific mapping is used.
  • Keep the current fallback behavior for older provider versions so compatibility is preserved.

@danielalanbates
Copy link
Copy Markdown
Author

Thank you for your time reviewing this. We're withdrawing this PR. Apologies for any inconvenience.

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.

Line number parser doesn't utilize @ava/typescript's compilation output

2 participants