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

Comments for call expression arguments are not preserved in output #3844

Closed
mhegazy opened this issue Jul 13, 2015 · 15 comments
Closed

Comments for call expression arguments are not preserved in output #3844

mhegazy opened this issue Jul 13, 2015 · 15 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Jul 13, 2015

Reported by @NickHeiner in #3283

And I know that TS offers multiline strings via template strings, but here's another use case:

index.ts

declare var angular: any;

angular.module('app').factory('myService', /* @ngInject */ function foo($scope, filter) {

});

tsconfig.json

{
    "compilerOptions": {
        "removeComments": false
    }
}

λ tsc -v
message TS6029: Version 1.5.3
λ tsc -p .

Generated index.js

angular.module('app').factory('myService', function foo($scope, filter) {
});

The /* @ngInject */ comment needs to be there so ng-annotate can annotate the Angular dependency injection function.

@CyrusNajmabadi
Copy link
Contributor

@NickHeiner Nick, if you use this form /*! @ngInject */ would angular still understand and respect the comment?

if so, then this would be the recommended way to solve this problem. It's already the design of the .ts compiler (modulo bugs) that /*! comments are preserved. So that would ideally solve this problem.

If angular only accepts /* @ and not /*! @, then it sounds like a new comment type we'll have to add special handling for. @yuit could do this as part of her efforts in this space.

@EmmanuelDemey
Copy link

According to the code of the ngAnnotate, it should work I think

function inspectComments(ctx) {
    const comments = ctx.comments;
    for (let i = 0; i < comments.length; i++) {
        const comment = comments[i];
        const yesPos = comment.value.indexOf("@ngInject");
        const noPos = (yesPos === -1 ? comment.value.indexOf("@ngNoInject") : -1);
        if (yesPos === -1 && noPos === -1) {
            continue;
        }

        const target = ctx.lut.findNodeFromPos(comment.range[1]);
        if (!target) {
            continue;
        }

        addSuspect(target, ctx, noPos >= 0);
    }
}

@mhegazy : If it failed, you can maybe use the other syntax, by using the '@ngInject" string

@CyrusNajmabadi
Copy link
Contributor

@Gillespie59 Thanks! If that's the case, i think the appropriate solution is for people to annotate their @ngInject and @ngNoInject annotations with /*! so that they are preserved.

If anyone sees a case where we are not preserving /*! then please file a bug on that. That is a bug and needs to be fixed.

@yuit
Copy link
Contributor

yuit commented Aug 4, 2015

@Gillespie59 let us know whether it is the case the angular respect /*!. As @CyrusNajmabadi says if it not the case, we may have to extend our logic to preserve this special angular comment

Edited: I miss this part "removeComments": false so this is a bug in our side that we didn't keep the comment.

@yuit
Copy link
Contributor

yuit commented Aug 4, 2015

This is a result of we don't preserve comment in the call expression. In following scenario, we didn't emit comment as well

function foo(x: any){}
foo(/*lol*/ 10);

@mthamil
Copy link

mthamil commented Aug 5, 2015

I have a similar situation where comments are not preserved when defining a function on an object literal.

resolve: {
    id: /*! @ngInject */ (details: FooDetail) => details.id
}

@CyrusNajmabadi
Copy link
Contributor

Any time that we don't preserve a /*! comment should be considered a bug. @yuit , can you take a look at these. We're likely just missing an obvious emitLeadingComments call somewhere.

@yuit
Copy link
Contributor

yuit commented Aug 5, 2015

@CyrusNajmabadi @mthamil I believe the root of this issue is that we do not preserve comment at all when the comment is not lead by new line regardless of the /*!

For example:

var x = 10;
/*comment*/ () => { }, /*comment1*/ () => { } 

/comment1/ will not be emitted as well

I am talking a look on how to solve the issue now

@NickHeiner
Copy link

@NickHeiner Nick, if you use this form /*! @ngInject */ would angular still understand and respect the comment?

Yes, I do believe that will work for me. I didn't know about /*! comments. Thanks!

@yuit yuit added the Fixed A PR has been merged for this issue label Aug 13, 2015
@yuit yuit closed this as completed Aug 13, 2015
@arthursw
Copy link

arthursw commented Sep 21, 2016

Comments are also always removed when placed after a function definition:


class ExampleClass {

    constructor() {}

    exampleFunction() /*! removed precious comment */ {
        console.log("exampleFunction");
    }
}

Neither the "removeComments": false compile option, nor the exclamation mark comment /*! */have any effect.

Those comments are really important for me since they are used by Unreal.js.

@RyanCavanaugh
Copy link
Member

@arthursw please log a new issue for a bug with a different repro

@elfrevaldes
Copy link

I have the same problem that @arthursw has. I need to keep the comment but even though the file tsconfig.json says "removeComments": false or I use the /*! it does not preserve the comment. I I need them to turn on or off hints in my application

@mchambaud
Copy link

+1

@RyanCavanaugh
Copy link
Member

Again, please log a new issue with explicit repro steps

@nishantkagrawal
Copy link

I have added details to bug #16727 where the /*! is not honoured if it is not on a new line.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests