microsoft / microsoft/TypeScript

Preserve radix when transpiling number literals with separators

オープン
#42,197 コメント 0 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

🔍 Search Terms

  • number literals
  • radix

✅ 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

Currently, number literals with underscore separators are transpiled into decimal literals regardless of the radix used in the TypeScript source code.
I suggest to preserve the original radix of number literals whenever the ES target version supports it.

📃 Motivating Example

Consider this TypeScript code:

const a  = 0xFFFFFFFF;
const a_ = 0xFFFF_FFFF;
const b  = 0o666;
const b_ = 0o06_66;
const c  = 0b01010101;
const c_ = 0b0101_0101;

When transpiled to ES3/ES5 - Playground Link:

// ES3/ES5 (actual)
"use strict";
var a = 0xFFFFFFFF;
var a_ = 4294967295;
var b = 438;
var b_ = 438;
var c = 85;
var c_ = 85;

When transpiled to ES6 to ES2020 - Playground Link:

// ES6/ES2020 (actual)
"use strict";
const a = 0xFFFFFFFF;
const a_ = 4294967295;
const b = 0o666;
const b_ = 438;
const c = 0b01010101;
const c_ = 85;

If the original radix were kept, the output would rather be:

// ES3/ES5 (expected)
"use strict";
var a = 0xFFFFFFFF;
var a_ = 0xFFFFFFFF;
var b = 438;
var b_ = 438;
var c = 85;
var c_ = 85;

or

// ES6-ES2020 (expected)
"use strict";
const a = 0xFFFFFFFF;
const a_ = 0xFFFFFFFF;
const b = 0o666;
const b_ = 0o666;
const c = 0b01010101;
const c_ = 0b01010101;

ESNext as a target is not affected, since all number literals are kept unchanged.

Also, bigint literals in ES2020 are not affected, since the presence of an underscore separator does not make any difference: hexadecimal bigint literals always transpile to hexadecimal, octal and binary literals always transpile to decimal, regardless of any separators. Other minor inconsistencies like the case of the hexadecimal digits A-F or a-f for transpiled numbers vs. bigints are out of the scope of this suggestion.

💻 Use Cases

Non-decimal literals can make the code more readable, this is the obvious use here. As an additional effect, this would remove the inconsistent special treatment of hexadecimal number literals with separators with respect to their bigint counterparts.

// TypeScript
[0x10ffff, 0x10_ffff, 0x10ffffn, 0x10_ffffn]
// JavaScript

// Current:
[0x10ffff, 1114111, 0x10ffffn, 0x10ffffn]
           ^^^^^^^

// Probably better:
[0x10ffff, 0x10ffff, 0x10ffffn, 0x10ffffn]

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、TypeScript が ES3/ES5 および ES6–ES2020 ターゲット向けに、区切り文字を含む数値リテラルをどのようにトランスパイルするかを追跡します。区切り文字のある場合とない場合の 16 進数、8 進数、2 進数の例を比較し、期待される基数を保持した出力のカバレッジを追加します。対応するターゲットがソースの基数を保持し、ESNext の動作が変更されないことを確認できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。