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

Some comments not emitted - upgrade 1.0.1 to 1.3.0 #1311

Open
jbondc opened this issue Nov 30, 2014 · 11 comments
Open

Some comments not emitted - upgrade 1.0.1 to 1.3.0 #1311

jbondc opened this issue Nov 30, 2014 · 11 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@jbondc
Copy link
Contributor

jbondc commented Nov 30, 2014

more file.ts

// test
var f = ''
// test #2
module foo {
        function bar() { }
}
// test #3
module empty {
}
// test #4

more file.js

// test
var f = '';
// test #2
var foo;
(function (foo) {
    function bar() {
    }
})(foo || (foo = {}));

Looks like comments after a 'block' aren't emitted. Could it be a missing emitTrailingComments(node) ?

@mhegazy mhegazy added the Bug A bug in TypeScript label Nov 30, 2014
@DanielRosenwasser
Copy link
Member

Just to be clear, when you say 1.0.3, do you mean 1.3.0?

@jbondc
Copy link
Contributor Author

jbondc commented Nov 30, 2014

Yip, corrected the title. I attempted a patch here:
https://github.com/jbondc/TypeScript/compare/debug

Tried to get rid of the trailingSeparator logic, still a few fixes needed.

@DanielRosenwasser DanielRosenwasser changed the title Some comments not emitted - upgrade 1.0.1 to 1.0.3 Some comments not emitted - upgrade 1.0.1 to 1.3.0 Dec 1, 2014
@DanielRosenwasser
Copy link
Member

Not to deflect from your current work, but it may be worth seeing what @sheetalkamat has to say about this issue and what the most appropriate fix is in general

@jbondc
Copy link
Contributor Author

jbondc commented Dec 1, 2014

Thanks @DanielRosenwasser, I'll wait for feedback.

Updated the patch with a couple of fixes. 2729 tests failing though most are because newlines are preserved for single line comments.

@mhegazy mhegazy added this to the Community milestone Dec 2, 2014
@jbondc
Copy link
Contributor Author

jbondc commented Dec 2, 2014

So I think I found the right idea to fix & improve, mainly simplify scanner & parser using:

    export interface CommentRange extends TextRange {
        leadingWhitespace?: string;
        trailingWhitespace?: string;
        isAttached?: boolean; /* attached to a Node (trailingWhitespace has at most 1 newline) */
        isFollowed?: boolean; /* followed *//* by another comment */
    }

The other bit is not emitting comments attached to !isDeclarationVisible(node)

As a heads up, only working on compiler scanner.ts emitter.ts, parser.ts

@CyrusNajmabadi
Copy link
Contributor

Hey @jbondc I'm going to be doing a little bit of work related to this. Namely, the introduction of an EndOfFile node to the ast. This should address the case where // test #4 is not in the final tree.

Note: it is by design that test #3 is not in the final tree.

@jbondc
Copy link
Contributor Author

jbondc commented Dec 3, 2014

Super @CyrusNajmabadi, EndOfFile node sounds great. Pushed latest experiment and test case I'm using:
https://github.com/jbondc/TypeScript/blob/debug/tests/cases/compiler/commentsWhitespace.ts

Thought: when scanning can determine 'isAttached', 'isFollowed' & keep whitespace?
https://github.com/jbondc/TypeScript/blob/debug/src/compiler/scanner.ts#L872

Would avoid re-scanning (though don't know about services.ts / keeping in memory):
https://github.com/jbondc/TypeScript/blob/debug/src/compiler/scanner.ts#L373

@synaesthesia
Copy link

The bug still exists in 1.5.3. And it's not all blocks:

function hello(ok: boolean) {
        if (ok) {
                console.log("ok1");
                // will be removed
        }
        // will be removed too
        else {
                console.log("ok2");
        }
        // will stay
}

hello(true);

compiles to

function hello(ok) {
    if (ok) {
        console.log("ok1");
    }
    else {
        console.log("ok2");
    }
    // will stay
}
hello(true);

@alvarorahul
Copy link

So, this reproduces in TypeScript 1.6 as well

Here's another case where it gets stripped out

export = Main;
module Main {
    // stays

    class Foo {
        // stays
        public bar = 5;
        // gets removed
        }

    // gets removed
}

@mhegazy mhegazy added the Help Wanted You can do this label Feb 20, 2016
@DickvdBrink
Copy link
Contributor

The last example is solved and it looks likes the first post is now correct to; only test is removed but as @CyrusNajmabadi mentioned #1311 (comment) that is by design.
So maybe close this one?

@timocov
Copy link
Contributor

timocov commented Sep 2, 2018

Another cases (moved from #26079 (comment)):

index.ts

// comment 1

import * as Module from './module';

// comment 2

import { SomeInterface } from './module';

export type A = typeof Module & SomeInterface;

module.ts

export interface SomeInterface {}

After compiling we get the next content in index.js (for es2015 module):

// comment 1

(it seems that new line after/before comment does not help - #26079 (comment))

function foo() {
  return {
    foo: 5,
    // this comment will be emitted
    bar: 10,
    // but this one wouldn't
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

9 participants