microsoft / microsoft/TypeScript

Improve call expression source maps

Open
#60,565 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Source Maps Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 Search Terms
  • source map call expression
  • emitCallExpression source map
  • source map function call
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Source Maps"
⏯ Playground Link

https://www.typescriptlang.org/play/?module=1&inlineSourceMap=true#code/JYWwDg9gTgLgBCCBjA1gUQB4wKZQHYCGANnAGZQQhwBEAdAPSKq0BWAztQNwBQ3pArniQxgEPAmQoAPABUAfAAoCUAOYB+AFxwZASgDeAX15IxbeATgBeCagU6eJvGbgAjKzZRwFcOPe6PnJHcmaTMoYDwVOR8FagJ4gmo-APgAE2DJOHoAKgRsCAB3OGz6Lx8aZRUARmpyv256UoA5CBwtUmgkbDgYNiCYCDgVbDxcAhw4AnEI1OAobGE4JGIif1N4busQzBx8YnKymlrfHiA

💻 Code
import mockExternal from "./mock.js";

function mock<T>(arg?: T){}

const a = mock();
const b = mock (  );
const c = mock<string>  ("aaaa");
const d = mock /* meow */ (   "arg1"   );

// Note: force tsc to generate an indirect call
const e = mockExternal    (  ""  );

Compiled:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mock_js_1 = __importDefault(require("./mock.js"));
function mock(arg) { }
const a = mock();
const b = mock();
const c = mock("aaaa");
const d = mock /* meow */("arg1");
// Note: force tsc to generate an indirect call
const e = (0, mock_js_1.default)("");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbnB1dC50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSx3REFBcUM7QUFFckMsU0FBUyxJQUFJLENBQUksR0FBTyxJQUFFLENBQUM7QUFFM0IsTUFBTSxDQUFDLEdBQUcsSUFBSSxFQUFFLENBQUM7QUFDakIsTUFBTSxDQUFDLEdBQUcsSUFBSSxFQUFLLENBQUM7QUFDcEIsTUFBTSxDQUFDLEdBQUcsSUFBSSxDQUFXLE1BQU0sQ0FBQyxDQUFDO0FBQ2pDLE1BQU0sQ0FBQyxHQUFHLElBQUksQ0FBQyxVQUFVLENBQUssTUFBTSxDQUFJLENBQUM7QUFFekMsK0NBQStDO0FBQy9DLE1BQU0sQ0FBQyxHQUFHLElBQUEsaUJBQVksRUFBTyxFQUFFLENBQUcsQ0FBQyJ9
🙁 Actual behavior

The JS is compiled perfectly and works fine, however due to the way NodeJS reports call sites the call site of each mock function invocation is as follows:

position in JS (line, col) substring in JS at position position after source mapping substring in TS at position
(8, 11) mock (5, 11) mock
(9, 11) mock (6, 11) mock
(10, 11) mock (7, 11) mock
(11, 11) mock (8, 11) mock
(13, 31) (""); (11, 29) "" );

When looking at a call expressions reported call site I personally think that indirect call expressions positions are misleading after being source mapped as it seems to imply the first argument is the section of the code affected when there are spaces between the first argument and the open parenthesis token.

🙂 Expected behavior

I would expect the (13,31) call site to map directly to (12, 27) in the source map, so that users can easily see the invocation position of the function call, in a consistent and predictable way.

Additional information about the issue

I debated on whether this would be considered a bug or a feature request, but decided on bug due to the current call site position being inconsistent for users trying to map their codes call location. If you think this should be a feature request, I am more than happy to change this issue to be a feature request.

If this feature is deemed to be an acceptable change to typescripts source map emissions, I have created a simple implementation that in my own testing seems to work, I am happy to add automated tests and raise a PR (https://github.com/mini-ninja-64/TypeScript/tree/improve-source-map). My current implementation works by emitting a source map position in the emitNodeList function based on the start position of the provided NodeArray<T>, this means it could also improve the source map production for other node lists; however this could increase the size of source maps generated by tsc.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the emitNodeList function mentioned in the issue and reproduce the indirect-call mapping using the linked TypeScript Playground example. Compare the emitted source-map position with the expected invocation position, then add automated coverage showing that spaced and indirect call expressions map consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.