TypeScript Compiler API loses formatting during transformation TypeScript Compiler API loses formatting during transformation typescript typescript

TypeScript Compiler API loses formatting during transformation


You could use tools that capture the formatting and comments, and regenerate them on completion of the transformation process, as you note that Roslyn does. However, Roslyn and the TypeScript "compiler" are specific to their target languages.

In general what you want is a "program transformation system". These are tools that accept grammars, automatically build ASTs that capture all that formatting data, allow you to define transformations using source-level patterns, and execute these transformations by matching/patching the ASTs, and them prettyprint the modified tree preserving that formatting data.

Our DMS Software Reengineering Toolkit can do that.

One has to define the target language grammar to it; we've done for many languages including JavaScript but not yet for TypeScript. However, you can build language dialects by building on top of other definitions. Or, you can do TypeScript from scratch; it isnt hard if you have an explicit grammar, which I think exists for TypeScript. Part of that definition tells the parse how to recognize comments so they can be saved; DMS knows how to save all the formatting and layout data.

With that, to solve your particular task, you could write actually very simple transformations using DMS rewrite rules:

source domain ECMAScript~TypeScript; -- assuming TypeScript is built as a dialecttarget domain ECMAScript~TypeScript; -- we're defining rules that map TypeScript to itself    -- you could write rules map TypeScript to C++ if you insistrule InternationalizeStringLiteral(s:STRINGLITERAL): primary-> primary  = "\s"-> "Translate(\s)";rule InternationalizeJsText(jst:JSTText): primary -> primary  = " \jst " -> "Translate(\jst)";ruleset Internationalize = { InternationalizeStringLiteral, InternationalizeJsText};

You can ask DMS to parse a file, apply the ruleset bottom up to your tree, then prettyprint the result.

These rules are completely syntax aware, because they operate on the ASTs, so they are not fooled by text in comments or string literals, or line boundaries/whitespace/formats/interwoven comments, ...

Now, you have a 1000 files to change. This is big enough so it might be worth the effort to define TypeScript and apply DMS. (It would be a slam dunk if the TypeScript front end for DMS was ready, do the above). Sometimes it is not; YMMV depending on what you really want to do. DMS is best used on large code bases, and really shines if you have complex transformations to make.