3

Typescript compiles

class ClassName { }

to

var ClassName = function () {
    function ClassName() {
    }
    return ClassName;
}();

I run that JS code through sweet.js, which even when there are no macros defined, produces something like this:

var ClassName$659 = function () {
    function ClassName$663() {
    }
    return ClassName$663;
}();

I understand that sweet.js would not rename the first occurrence of ClassName if the top-level var wasn't used, or if a different name was used for the constructor function, but it's the Typescript compiler that does those things, not me.

Why it's a problem

  1. I can no longer use ClassName in HTML files. That's not something I want to do often, and I can of course always do without the capability, but I still miss having it.
  2. The macros I want to use so-far don't require any hygienic renaming. Yet now it seems like I'll have to undo the renaming with my own script. Source maps won't help easily, since I already need a source map for the typescript --> javascript conversion.

My question

Is there a way to disable hygienic renaming in sweet.js? Is there a better way to deal with this issue?

Dustin Wehr
  • 165
  • 9

2 Answers2

2

Using the --readable-names flag with sjs, as @AnthonyCalandra suggested, resolved my problem.

Dustin Wehr
  • 165
  • 9
1

Is there a way to disable hygienic renaming in sweet.js?

No.

Is there a better way to deal with this issue?

Not sure. As a hack you can post-process the file with a script that searches for ClassName$xxx and then add

var ClassName = ClassName$xxx;
soegaard
  • 30,661
  • 4
  • 57
  • 106
  • Surely this is a bug, right? I mean, if I copy the "let macros" code verbatim from the documentation it renames the top-level function, making it uncallable (no function in existing code is going to know it has to add a dollar sign and some random number to the already known function name!) – Michael Oct 20 '15 at 05:57
  • also, for some reason it doesn't do this top-level renaming in the demo editor in the browser, only when running it under node. (surprise!) – Michael Oct 20 '15 at 06:10
  • @Michael This is just a hunch: Sweet.js switched to a new expansion algorithm. Maybe there is a difference between the old and the new one. Has the demo page switched yet? Note that the above answer is from before the switch. – soegaard Oct 21 '15 at 16:41