microsoft / microsoft/TypeScript

Parser API: Expose a Method to Determine Whether a Numeric Literal is of Certain Flag

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

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

Domain: API In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Search Terms

Parser API numericLiteralFlags numeric literal flag

Suggestion

Currently the parser API apparently does not expose numericLiteralFlags for determining whether the original source is written using features such as binary or octal literal.

I have to do this to determine whether the original source was written using ECMAScript 2015 numeric literal:

    if (TypeScript.isNumericLiteral(node)) {
        let bitflag = node['numericLiteralFlags'];
        if (bitflag) {
            if (bitflag & (1 << 8)) {
                // internal flag: Octal
                // https://github.com/Microsoft/TypeScript/blob/a4a1bed88bdcb160eff032790f05629f9fa955b4/src/compiler/types.ts#L1659
                return true;
            }
            if (bitflag & (1 << 7)) {
                // internal flag: Binary
                // https://github.com/Microsoft/TypeScript/blob/a4a1bed88bdcb160eff032790f05629f9fa955b4/src/compiler/types.ts#L1658
                return true;
            }
        }
    }

AFAIK this is the only way to check it since node.text evaluates to the string representation of the base 10 number...

Use Cases

I need this 'internal' API to write a parser for helping determine whether a JS file is compatible with syntax of certain ECMAScript version. For example: Can this script run in ES5 environments?

node_modules may contain packages not distributed in ES5, which can cause minification error when being minified with minifier not capable of handling ES5, or worse, ninja runtime error. (I want to parse packages / libraries with TypeScript API, then transpile them only when necessary for fast build. Rather than transpiling everything)

Examples

Simple proposed API:

isES2015OctalNumericLiteral(node)
isES2015BinaryNumericLiteral(node)

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. new expression-level syntax)

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

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

はじめの一歩

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

調査の方向性

パーサー API のエントリポイントである TypeScript.isNumericLiteral と、src/compiler/types.ts で参照されている numericLiteralFlags の定義から始めます。数値リテラルがどのように表現されているかを追跡し、次に 2 進数リテラルと 8 進数リテラルを識別するための public API の形を決定して、提案された動作のカバレッジを追加します。完了の条件は、利用者が内部フラグを必要としなくなることです。

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

評価

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

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

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