-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Comments
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. |
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. |
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. |
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. |
Should be fixed by #11286 |
I wonder why TSC re-declare a global var after each namespace or module block.
Above code is translated to:
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.
The text was updated successfully, but these errors were encountered: