Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--annotateTransforms switch to add transformer diagnostics to Source Maps #51307

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Oct 25, 2022

This adds an --annotateTransforms compiler option that can (eventually) be used as a debugging tool for compiler transforms. When --annotateTransforms is set, along with --sourceMap (and/or --declarationMap), an extra x_ms_ts_annotations property is added to the output source map containing annotations associated with a line/character in the generated output.

However, this isn't extremely useful on its own. In addition to this, we would need to publish a VS Code extension capable of reading the source map that can then add hovers or text editor decorations to display the diagnostic information. The end result would be something not unlike VS Code's own "Developer: Inspect Editor Tokens and Scopes" command, allowing you to inspect which transformers lead to a specific node being included in the output.

Most of this was originally part of #50820, but I'm pulling the functionality out of that PR. The original version injected comments into the output, but that made the output file difficult to read and thus defeated the purpose.

The format of the x_ms_ts_annotations field is not yet set in stone and is likely to change before (if) this becomes available. The information stored in x_ms_ts_annotations is intentionally generic: an "Annotation" is an object consisting of a name (a string) and a value (any valid JSON). One or more annotations can be associated with a generated line/character:

interface SourceMapAnnotation {
  name: string;
  value: unknown;
}

interface DecodedSourceMapAnnotation {
  generatedLine: number;
  generatedCharacter: number;
  annotations: SourceMapAnnotation[];
}

The provided name need not be unique. All annotations will be included at the generated position since multiple nodes can share the same generated start and end positions.

In addition, this adds a public decodeSourceMapAnnotations function that can be used to deserialize the x_ms_ts_annotations property into each DecodedSourceMapAnnotation entry.

When --annotateTransforms is enabled, the following annotations are written before and after each Node:

{ name: "typescript.transformers", value: { kind: "start", transformers: ["ts", "es2022", ...] } }
{ name: "typescript.transformers", value: { kind: "end" } }

Since this can be fairly chatty, this information is only written to the source map when --annotateTransforms is enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants