Microsoft/TypeScript

Suggestion: Formatting: Disable inserting spaces around leading semicolon

Open

#48,571 创建于 2022年4月5日

在 GitHub 查看
 (1 评论) (2 反应) (0 负责人)TypeScript (6,726 fork)batch import
Domain: FormatterExperience EnhancementHelp WantedSuggestion

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Suggestion

🔍 Search Terms

semicolon at the start of a line formatting spaces around semi

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

In setups where semicolons are not used and the code relies on ASI, there are certain statements which can confuse both the developer and the engine. In such cases, some people prefer to use semicolon at the start of the particular statement that could potentially cause such behavior regardless of what is actually on the preceding line.

📃 Motivating Example

This adjustment will fix formatting issue and make it consistent.

💻 Use Cases

With vscode formatting setting

"typescript.format.semicolons": "remove"

and eslint rules,

"semi-style": ["error", "first"],
"semi-spacing": "error"
"indent": ["error", 2],

I get this automatic eslint --fix,

// before
console.log('hi');
(something ??= {_default: 0})[key] = 1

// after 
console.log('hi')
;(something ??= {_default: 0})[key] = 1
^ ..... this is desired

which is then auto-formatted by vscode to this,

console.log('hi')
  ; (something ??= {_default: 0})[key] = 1
^^ ^ ..... spaces inserted, undesired

which in turn raises this eslint error.

Expected indentation of 14 spaces but found 16. eslint(indent)

Migrated from https://github.com/microsoft/vscode/issues/146620

贡献者指南