Skip to content

TypeScript removing comments where it shouldn't #2546

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

Closed
qyoz opened this issue Mar 30, 2015 · 15 comments · Fixed by #22141
Closed

TypeScript removing comments where it shouldn't #2546

qyoz opened this issue Mar 30, 2015 · 15 comments · Fixed by #22141
Assignees
Labels
Bug A bug in TypeScript Domain: Comment Emit The issue relates to the emission of comments when compiling Fixed A PR has been merged for this issue

Comments

@qyoz
Copy link

qyoz commented Mar 30, 2015

comment.ts

if (false) {
    //I will be removed
}
//I will be removed
else {
    //I will not be removed
    var tmp;
    //I will be removed
}

try{
    //I will not be removed
    var tmp;
    //I will be removed
}
//I will be removed
catch(e){
    //I will not be removed
    var tmp;
    //I will be removed
}

comment.js

if (false) {
}
else {
    //I will not be removed
    var tmp;
}
try {
    //I will not be removed
    var tmp;
}
catch (e) {
    //I will not be removed
    var tmp;
}

Are there any additional flags that can be set for TS to keep all comments?

Same issue from CodePlex

@JoshMcCullough
Copy link

I have "Keep comments in JavaScript output" checked but my header comment, at the top of my TS is being ignored. Really, I only want to keep comments in the /** Comment */ format for documentation purposes, and this is mostly working. The header comment is something like this:

/** Whatever JavaScript API v1.2.3.4 */
module whatever {
    export class API { ... }
}

The comment on the first line is excluded meanwhile other comments in the file are kept.

@qyoz
Copy link
Author

qyoz commented Mar 30, 2015

@JoshMcCullough Block comments are also excluded at those locations (end of if/try statements).
I know comments at the top of file are kept, but this is not what I need.

@JoshMcCullough
Copy link

I wasn't suggesting a solution to your problem, I was suggesting that comments at the top of my TS file are being excluded from the JS. E.g., we're in the same boat - our comments are being stripped!

@qyoz
Copy link
Author

qyoz commented Mar 30, 2015

+1

@mhegazy mhegazy added the Bug A bug in TypeScript label Mar 30, 2015
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Mar 30, 2015
@mhegazy mhegazy removed this from the TypeScript 1.8 milestone Dec 1, 2015
@mhegazy mhegazy added the Help Wanted You can do this label Dec 8, 2015
@mhegazy mhegazy added this to the Community milestone Dec 8, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2015

PRs are welcomed

staltz pushed a commit to staltz/TypeScript that referenced this issue Feb 24, 2016
This is a straightforward fix for bug microsoft#2546. Keeps most or all TS comments
in the output, unless the option --removeComments is enabled.
AntonTolmachev pushed a commit to AntonTolmachev/TypeScript that referenced this issue Apr 7, 2016
AntonTolmachev pushed a commit to AntonTolmachev/TypeScript that referenced this issue Apr 7, 2016
AntonTolmachev pushed a commit to AntonTolmachev/TypeScript that referenced this issue Apr 7, 2016
AntonTolmachev pushed a commit to AntonTolmachev/TypeScript that referenced this issue Apr 7, 2016
@nickzelei
Copy link

TypeScript also appears to be removing comments in this fashion:
ts:
const foo: boolean = var1 || /* istanbul ignore next */ null;

js:
var foo = var1 || null;

This is painful when trying to get code coverage and are setting ignore statements.

@JoshMcCullough
Copy link

3rd party hints within code? :-/

@mxtopher
Copy link

There are other places where it's been removed, I created this one last week without knowing it was a dup
#13602

@mchambaud
Copy link

Still does not work

@jimsugg
Copy link

jimsugg commented May 4, 2017

This is a big issue for us as well. We are trying to use jsDoc with Typescript, and Typescript's habit of removing comments, even with removeComments=false, is making this very painful. Please provide some way to keep all comments in emitted JS. In our case, I am trying to document interfaces and other things that do not emit JS code, and this makes it very difficult.

@mhegazy
Copy link
Contributor

mhegazy commented May 4, 2017

Comments on interfaces will not be emitted, since the interfaces themselves are not emited.

For other issues please be specific on what declarations are not getting their comments emitted properly.

@jimsugg
Copy link

jimsugg commented May 4, 2017

Yes, I understand the rationale for removing the comments before interfaces. In many cases it is correct and understandable. But what I am saying is that there are significant reasons to include an option to not remove any comments, including those before interfaces. In fact, there is one very significant reason and use case - documentation with tools like jsDoc. Think of why we use interfaces. They explicitly define the shape of objects for clients of a package. This functionality is a major advantage of using Typescript. Having no option to allow comments about these structures to flow through to documentation is a major failing, in my view. Why would adding a compiler option to suppress all comment removal be a problem? It would be up to the author to use it correctly.

@nickzelei
Copy link

nickzelei commented Jun 6, 2017

I would like to add another to this list. Let me know if it deserves a separate issue.

TypeScript removes top level comments when a file contains a shebang.
We have a company standard that requires copyright statements be added to all of our ts/js files.

Given the typescript file below:

#! /usr/bin/env node
/**
 * @copyright <copyright goes here>
 */
export const foo: string = "bar";

The emitted javascript output is:

#! /usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = "bar";
//# sourceMappingURL=test.js.map

This is not a problem for non-executable javascript files and the header comment is emitted directly below the "use strict";

This is using typescript 2.3.x

@ArtemGovorov
Copy link

I would like to add few more cases. All comments in the example below are removed when compiling the code:

var a;
a
  // comment
  .b()

if (/*comment*/1) {
}

a(/*comment*/)

if /*comment*/(1) {

}
else /*comment*/{}

@gsdnano
Copy link

gsdnano commented Jul 5, 2017

Also removes

for (const movieFile of fm.movieFiles) { // comment

@weswigham weswigham self-assigned this Mar 3, 2018
@weswigham weswigham modified the milestones: Community, TypeScript 2.8 Mar 3, 2018
@weswigham weswigham added Fixed A PR has been merged for this issue Domain: Comment Emit The issue relates to the emission of comments when compiling and removed Help Wanted You can do this labels Mar 3, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: Comment Emit The issue relates to the emission of comments when compiling Fixed A PR has been merged for this issue
Projects
None yet
10 participants