如何在打字稿生成的 JavaScript 中维护空格

jos*_*eph 6 javascript whitespace 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)
);  
Run Code Online (Sandbox Code Playgroud)

输出看起来很丑陋的超长行:

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));  
Run Code Online (Sandbox Code Playgroud)

我的预期输出是:

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)
); 
Run Code Online (Sandbox Code Playgroud)

我的 tsconfig 看起来像:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
  },
  "include": ["./src/**/*"],
  "exclude": [
    "node_modules",
    "./src/real/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

Ole*_*ich 0

根据tsconfig.json配置的不同,转换后的 TypeScript 代码可能会有很大不同,因此保留空格实际上并不是编译器中的一个选项。

您可以做的一件事是首先通过像Prettier这样的格式化程序运行源代码,然后在编译后再次运行它(prettier --write **/*.ts应该可以解决问题)。这应该可以最小化您在源代码和目标代码之间看到的差异。