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

Suggestion: Preserve line numbers #3142

Closed
streamich opened this issue May 12, 2015 · 5 comments
Closed

Suggestion: Preserve line numbers #3142

streamich opened this issue May 12, 2015 · 5 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@streamich
Copy link

Currently, this TypeScript code

class PreserveLineNumbers {

    myMethod()  {

    }

}

is transpiled to this JavaScript code

var PreserveLineNumbers = (function () {
    function PreserveLineNumbers() {
    }
    PreserveLineNumbers.prototype.myMethod = function () {
    };
    return PreserveLineNumbers;
})();

However, you could reformat the code, so that the main code pieces keep the same lines, to something like this:

var PreserveLineNumbers = (function () { function PreserveLineNumbers() {}

    PreserveLineNumbers.prototype.myMethod = function () {

    };

return PreserveLineNumbers; })();

P.S. I know about the source maps.

@danquirk
Copy link
Member

@CyrusNajmabadi can probably chime in with the specifics around how and why we do this

@CyrusNajmabadi
Copy link
Contributor

The reason we do things in the current manner is that preservation of source code formatting was not a goal with the new compiler rewrite. So, the emitter acts as more of a pretty-printer than a true tree rewrite (i.e. one preserving as much of the original tree as possible).

We do attempt, in very specialized situations, to have our pretty-printing be informed by the original code. For example, we'll try to preserve certain single line constructs. Or we try to keep some newlines in complex expressions to help make the outputted code more readable.

Right now though, we don't do much additional work with keeping newline information around.

If you're interested in improving things here, feel free to contribute a PR. Thanks!

@danquirk danquirk added the Suggestion An idea for TypeScript label May 13, 2015
@mhegazy
Copy link
Contributor

mhegazy commented May 21, 2015

Though we can do this for simple transformations, with an added complexity bear in mind; we can not do that for all transformations. consider something like async/await. SourceMaps has been the standard for JS community to deal with these transformations. The TypeScript compiler supports soruce map generation with --soruceMap flag; the generated maps are supported by all major browsers and IDEs.

@mhegazy mhegazy closed this as completed May 21, 2015
@mhegazy mhegazy added Declined The issue was declined as something which matches the TypeScript vision Too Complex An issue which adding support for may be too complex for the value it adds labels May 21, 2015
@namnm
Copy link

namnm commented Sep 10, 2017

Sadly we can not have this feature. I implemented a log util to log the message together with the file name and line number. It's work great with babel since babel supports preserve line number. But not typescript.

@nicolasgarnier
Copy link

+1, there are many cases of develper tools that do not support courcemaps (for instance often hosted online logs of Functions As A Service providers)

@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
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

6 participants