question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mhegazycommented, Sep 30, 2016
0reactions
redstrikecommented, Nov 8, 2015

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Removing duplicate namespaces in XML Literals (Shyam ...
Yes, the LINQ to XML API can figure out and remove duplicate namespaces. But it does not enforce the removal of duplicate namespaces...
Read more >
eslint-plugin-import | Yarn - Package Manager
This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, and prevent issues with misspelling of file paths and import names. All...
Read more >
node_modules/eslint-plugin-import/CHANGELOG.md · master ...
no-unused-modules : consider exported TypeScript interfaces, types and enums (#1819, ... [readme] Remove duplicate no-unused-modules from docs (#1690, ...
Read more >
RHSA-2021:3759 - Security Advisory - Red Hat 客户门户网站
... BZ - 1967621 - Operator fails to install and OLM tries to delete nonexistent catalog pods under openshift-marketplace/redhat-marketplace ...
Read more >
SWIG-4.0 Documentation
i */ %module example %{ /* Put headers and other declarations here */ extern double My_variable; extern int fact(int); extern int my_mod(int n,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found