microsoft / microsoft/TypeScript

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

Open
#5,553 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the namespace and module example with TSC and comparing the emitted JavaScript. Inspect the compiler path responsible for namespace/module emission, then define expected output for duplicate declarations and preserved line breaks. Add regression coverage for both formatting concerns and verify the generated output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.