Skip to content

sourcemap mapping is generated before node generates newline and indent #39170

Closed
@krisselden

Description

@krisselden

TypeScript Version: 4.0.0-dev.20200615

Search Terms:
sourcemap

Code

export type AsyncFn<TResult> = () => PromiseLike<TResult>;

export type RaceCancelFn = <TResult>(
  asyncFnOrPromise: AsyncFn<TResult> | PromiseLike<TResult>
) => Promise<TResult>;

export type DisposeFn = () => void;

export type DisposableExecutorFn<TResult> = (
  resolve: (value?: TResult | PromiseLike<TResult>) => void,
  reject: (reason?: any) => void
) => DisposeFn;

const noopRaceCancel: RaceCancelFn = <TResult>(
  asyncFnOrPromise: AsyncFn<TResult> | PromiseLike<TResult>
): Promise<TResult> =>
  typeof asyncFnOrPromise === "function"
    ? Promise.resolve().then(asyncFnOrPromise)
    : Promise.resolve(asyncFnOrPromise);

export default async function cancellablePromise<TResult>(
  disposableExecutor: DisposableExecutorFn<TResult>,
  raceCancel: RaceCancelFn = noopRaceCancel
): Promise<TResult> {
  let dispose: DisposeFn | undefined;
  try {
    return await raceCancel(
      () =>
        new Promise<TResult>((resolve, reject) => {
          dispose = disposableExecutor(resolve, reject);
        })
    );
  } finally {
    if (dispose !== undefined) {
      dispose();
    }
  }
}

copy above code to index.ts then run:
tsc --target esnext --sourceMap index.ts

Expected behavior:
I expect that since this should just be removing types that the generated sourcemap could map all JS AST nodes start/end back to the original but at a minimum starts of statements, expressions, identifiers, blocks. Ideally for source map generation identifiers would 1 to 1 map to identifiers and not include type. Ideally starts should not include whitespace.

Actual behavior:
Missing closing braces in the try/finally block and the if in the finally block (since tsc 2.8).
Closing parens often start on prior lines ending newline instead of the paren in the original (since as far back as tsc 2.0).
Identifiers missing mapping at end when typing info was removed or there are default expressions.

The last one maybe considered a nice to have but is helpful with various tooling and to survive downstream transforms like rollup and minification.

tsc-sourcemap

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions