Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain whitespace in javascript generated from typescript

I want the javascript code generated from typescript to have the same white spacing. Long story short, we're migrating from js to ts and maintaining whitespace would allow us to compare generated code against the unmigrated version of the code.

My input ts looks like:

id = this.getOrCreate(
    entity.uuid,
    dao.wrap(utils.trimProxyPrefix(entity.name)),
    dao.wrap(entity.type),
    dao.wrap(entity.targetName),
    dao.wrap(entity.targetType),
    dao.wrap(entity.targetIp),
    dao.wrap(host),
    dao.wrap(version),
    dao.wrap(status)
);  

The output looks this ugly super long line:

id = this.getOrCreate(entity.uuid, dao.wrap(utils.trimProxyPrefix(entity.name)), dao.wrap(entity.type), dao.wrap(entity.targetName), dao.wrap(entity.targetType), dao.wrap(entity.targetIp), dao.wrap(host), dao.wrap(version), dao.wrap(status));  

My expected output would be:

id = this.getOrCreate(
    entity.uuid,
    dao.wrap(utils.trimProxyPrefix(entity.name)),
    dao.wrap(entity.type),
    dao.wrap(entity.targetName),
    dao.wrap(entity.targetType),
    dao.wrap(entity.targetIp),
    dao.wrap(host),
    dao.wrap(version),
    dao.wrap(status)
); 

My tsconfig looks like:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
  },
  "include": ["./src/**/*"],
  "exclude": [
    "node_modules",
    "./src/real/*"
  ]
}
like image 909
joseph Avatar asked Aug 15 '26 07:08

joseph


1 Answers

Transpiled TypeScript code could be significantly different depending on the tsconfig.json configuration, so preserving whitespace isn't really an option in the compiler.

One thing you could do is to first run your source through a formatter like Prettier, and also run it again post-compile (prettier --write **/*.ts should do the trick). That should minimize the diffs you see between the source and target code.

like image 124
Oleg Vaskevich Avatar answered Aug 17 '26 21:08

Oleg Vaskevich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!