[英]How to maintain whitespace in javascript generated from typescript
我希望從打字稿生成的javascript代碼具有相同的白色間距。 長話短說,我們正在從js遷移到ts,並且保持空白將使我們能夠將生成的代碼與未遷移的代碼版本進行比較。
我的輸入ts看起來像:
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)
);
輸出看起來像這樣丑陋的超長行:
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));
我的預期輸出將是:
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)
);
我的tsconfig看起來像:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
},
"include": ["./src/**/*"],
"exclude": [
"node_modules",
"./src/real/*"
]
}
根據tsconfig.json
配置的不同,轉譯的TypeScript代碼可能會有很大的不同,因此在編譯器中保留空格實際上不是一個選擇。
您可以做的一件事是,首先通過Prettier這樣的格式化程序運行源代碼,然后在編譯后再次運行它( prettier --write **/*.ts
應該可以解決問題)。 那應該最小化您在源代碼和目標代碼之間看到的差異。
我最終使用了漂亮的樣式,並在遷移到ts和從ts生成的js代碼之前還刪除了空行,然后對rbcommons發表了一篇評論,比較了“清理過的”代碼。
有關更漂亮的信息,請參閱Oleg的答案https://stackoverflow.com/a/57470670/1810962
要刪除空行,我使用了一些bash:
for i in `ls`; do sed -i .bak '/^[[:space:]]*$/d' $i ; done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.