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

namespace / module: Duplicate declaration & needlessly remove line-breaks #5553

Open
redstrike opened this issue Nov 6, 2015 · 5 comments
Open
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@redstrike
Copy link

I wonder why TSC re-declare a global var after each namespace or module block.

namespace OurName {
    export var isAndroid = true;
    export var isWinPhone = false;
    // do something
    var checkCookies = function() {
        return true;
    }
}

namespace OurName {
    export var HTML = '<b>hello</b>';
    export var state = [1, 2, 3];
    export var addToCart = function() {
        return true;
    }
}

namespace OurName {
    var xyz = false;
    // do something
    export module Office {
        var isClosed = false;
    }
}

Above code is translated to:

var OurName;
(function (OurName) {
    OurName.isAndroid = true;
    OurName.isWinPhone = false;
    // do something
    var checkCookies = function () {
        return true;
    };
})(OurName || (OurName = {}));
var OurName;
(function (OurName) {
    OurName.HTML = '<b>hello</b>';
    OurName.state = [1, 2, 3];
    OurName.addToCart = function () {
        return true;
    };
})(OurName || (OurName = {}));
var OurName;
(function (OurName) {
    var xyz = false;
    // do something
    var Office;
    (function (Office) {
        var isClosed = false;
    })(Office = OurName.Office || (OurName.Office = {}));
})(OurName || (OurName = {}));

It also removes the line-breaks that help me keeping the code easier to look. I am porting our production ES5 code to TypeScript, so I have to carefully re-check the output of TSC. This behaviour makes my work even harder.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2015

it is an error to use a variable before declaring it in strict mode, and namespaces can span multiple files/compilations. so there is no way to know for sure if it does exist or not. moreover re-declaring a variable in JS is a no-op.

you could argue that in a single file we can be sure if we emitted one var declaration with the same name and do not do it again, though in my opinion the complexity added would not warrant the value added.

@redstrike
Copy link
Author

Thanks, I just want to make sure if this kind of behaviour is a trade-off in design or not.

How about the line-breaks? I believe that reserved them is better than strip them away. TSC should try it best to keep the compiled code as similar as possible to the source.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 8, 2015

The compiler does not make promises about the output formatting, e.g. line breaks, location of curlys, semicolons, spaces vs. tabs, etc.. these are done on best effort bases. just like in my previous comment, supporting this is doable, though i do not believe the value added does not warrant the complexity needed to support output formatting.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Nov 8, 2015
@redstrike
Copy link
Author

If support output formatting adds much complexity like you said, so we don't need to do it. Just try to make the output easier to look at by adding a line-break after each block. For example:

var OurName;
(function (OurName) {
    OurName.isAndroid = true;
    OurName.isWinPhone = false;
    // do something
    var checkCookies = function () {
        return true;
    };
})(OurName || (OurName = {}));

var OurName;
(function (OurName) {
    OurName.HTML = '<b>hello</b>';
    OurName.state = [1, 2, 3];
    OurName.addToCart = function () {
        return true;
    };
})(OurName || (OurName = {}));

var OurName;
(function (OurName) {
    var xyz = false;
    // do something
    var Office;
    (function (Office) {
        var isClosed = false;
    })(Office = OurName.Office || (OurName.Office = {}));
})(OurName || (OurName = {}));

Or add line breaks before and after a function/namespace/module declaration. (optionally)

var OurName;
(function (OurName) {
    var xyz = false;
    OurName.HTML = '<b>hello</b>';
    OurName.state = [1, 2, 3];

    OurName.addToCart = function () {
        return true;
    };

    var Office;
    (function (Office) {
        var isClosed = false;
    })(Office = OurName.Office || (OurName.Office = {}));

})(OurName || (OurName = {}));

Does it easier to look? I know that using line-breaks for formatting code are not a priority in many style guides. But for those who don't have good eyesight like me (short-sighted: 6.25 & astigmatism: 2.25), it truly helps much.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Question An issue which isn't directly actionable in code labels Nov 9, 2015
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Aug 17, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Sep 30, 2016

Should be fixed by #11286

@mhegazy mhegazy added this to the Community milestone Jan 4, 2018
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants