microsoft / microsoft/TypeScript

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

Offen
#28,309 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: API In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Parser-API-Einstiegspunkt TypeScript.isNumericLiteral und der in src/compiler/types.ts referenzierten Definition numericLiteralFlags. Verfolge, wie numerische Literale dargestellt werden, ermittle dann die Form der öffentlichen API zur Identifizierung von Binär- und Oktalliteralen und füge Abdeckung für das vorgeschlagene Verhalten hinzu; abgeschlossen ist die Arbeit, wenn Verbraucher das interne Flag nicht mehr benötigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers, developer-experience
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.