microsoft / microsoft/TypeScript

API Suggestion: Add function for getting syntax kind name from syntax kind

Offen
#40,090 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Search Terms

function get SyntaxKind name

Suggestion

A common mistake people will do when using the compiler api is getting the syntax kind name by doing ts.SyntaxKind[kind], where kind is the syntax kind number value (ex. I had this mistake in my presentation at tsconf in ts-ast-viewer, though that's been fixed for a couple years now, I've still seen people do it occasionally).

This is wrong because it will give the last entry in the enum, which could be an alias like LastTypeNode instead of ImportType.

It would be nice to...

  1. Add a function... maybe getSyntaxKindName(kind: SyntaxKind): string... that gets the syntax kind name from the provided syntax kind value.
  2. Add a doc comment above SyntaxKind to suggest using getSyntaxKindName to get the name.

Examples

ts.getSyntaxKindName(ts.SyntaxKind.ImportType); // "ImportType"

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 damit, die exportierte SyntaxKind-Deklaration und die Einstiegspunkte der Compiler-API zu finden, die Hilfsfunktionen im Zusammenhang mit enum bereitstellen. Verfolge, wie aliased enum-Werte dargestellt werden, und füge dann Abdeckung für das ImportType-Beispiel und die angeforderte SyntaxKind-Dokumentation hinzu; fertig ist es, wenn der öffentliche Helfer den vorgesehenen Namen anstelle eines Alias zurückgibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
api, compilers
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 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.