Dustin Wehr
Dustin Wehr

Reputation: 165

What to do about sweet.js renaming top-level variables in Typescript output?

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?

Upvotes: 3

Views: 358

Answers (2)

Dustin Wehr
Dustin Wehr

Reputation: 165

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

Upvotes: 2

soegaard
soegaard

Reputation: 31147

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;

Upvotes: 1

Related Questions