istanbuljs / babel-plugin-istanbul

A babel plugin that adds istanbul instrumentation to ES6 code
BSD 3-Clause "New" or "Revised" License
616 stars 72 forks source link

TypeScript code generated with a syntax error #279

Open BenoitZugmeyer opened 1 year ago

BenoitZugmeyer commented 1 year ago

This plugin can output code with a syntax error. Taking the following TypeScript code as input:

toto = (function () {
  // tata
} as any)

The following output is emitted by the plugin:

[...]
toto = (function () {
  cov_131zeqpl6j().f[0]++;
} // tata
as any);

As you can see, the comment has been moved outside the function, and now the as operator is on a new line. It seems that this syntax is not supported by typescript.

Minimal Node.js script to reproduce:

const { transformSync } = require("@babel/core");

const result = transformSync(
  `
toto = (function () {
  // tata
} as any)
`,
  {
    filename: "toto.ts",
    plugins: [["@babel/plugin-syntax-typescript"], ["istanbul"]],
  }
);

console.log(result.code);