Skip to content

Commit e9ba547

Browse files
authored
Eliminate (ts as any).SyntaxKind (and similar) in favor of Debug.format functions (#49485)
1 parent 678afe8 commit e9ba547

12 files changed

+22
-61
lines changed

src/compiler/binder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ namespace ts {
391391
case SyntaxKind.Parameter:
392392
// Parameters with names are handled at the top of this function. Parameters
393393
// without names can only come from JSDocFunctionTypes.
394-
Debug.assert(node.parent.kind === SyntaxKind.JSDocFunctionType, "Impossible parameter parent kind", () => `parent is: ${(ts as any).SyntaxKind ? (ts as any).SyntaxKind[node.parent.kind] : node.parent.kind}, expected JSDocFunctionType`);
394+
Debug.assert(node.parent.kind === SyntaxKind.JSDocFunctionType, "Impossible parameter parent kind", () => `parent is: ${Debug.formatSyntaxKind(node.parent.kind)}, expected JSDocFunctionType`);
395395
const functionType = node.parent as JSDocFunctionType;
396396
const index = functionType.parameters.indexOf(node as ParameterDeclaration);
397397
return "arg" + index as __String;

src/compiler/transformers/declarations.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ namespace ts {
817817
while (length(lateMarkedStatements)) {
818818
const i = lateMarkedStatements!.shift()!;
819819
if (!isLateVisibilityPaintedStatement(i)) {
820-
return Debug.fail(`Late replaced statement was found which is not handled by the declaration transformer!: ${(ts as any).SyntaxKind ? (ts as any).SyntaxKind[(i as any).kind] : (i as any).kind}`);
820+
return Debug.fail(`Late replaced statement was found which is not handled by the declaration transformer!: ${Debug.formatSyntaxKind((i as Node).kind)}`);
821821
}
822822
const priorNeedsDeclare = needsDeclare;
823823
needsDeclare = i.parent && isSourceFile(i.parent) && !(isExternalModule(i.parent) && isBundledEmit);
@@ -1067,7 +1067,7 @@ namespace ts {
10671067
input.isTypeOf
10681068
));
10691069
}
1070-
default: Debug.assertNever(input, `Attempted to process unhandled node kind: ${(ts as any).SyntaxKind[(input as any).kind]}`);
1070+
default: Debug.assertNever(input, `Attempted to process unhandled node kind: ${Debug.formatSyntaxKind((input as Node).kind)}`);
10711071
}
10721072
}
10731073

@@ -1481,7 +1481,7 @@ namespace ts {
14811481
}
14821482
}
14831483
// Anything left unhandled is an error, so this should be unreachable
1484-
return Debug.assertNever(input, `Unhandled top-level node in declaration emit: ${(ts as any).SyntaxKind[(input as any).kind]}`);
1484+
return Debug.assertNever(input, `Unhandled top-level node in declaration emit: ${Debug.formatSyntaxKind((input as Node).kind)}`);
14851485

14861486
function cleanup<T extends Node>(node: T | undefined): T | undefined {
14871487
if (isEnclosingDeclaration(input)) {

src/compiler/transformers/declarations/diagnostics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace ts {
157157
return getTypeAliasDeclarationVisibilityError;
158158
}
159159
else {
160-
return Debug.assertNever(node, `Attempted to set a declaration diagnostic context for unhandled node kind: ${(ts as any).SyntaxKind[(node as any).kind]}`);
160+
return Debug.assertNever(node, `Attempted to set a declaration diagnostic context for unhandled node kind: ${Debug.formatSyntaxKind((node as Node).kind)}`);
161161
}
162162

163163
function getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult: SymbolAccessibilityResult) {
@@ -386,7 +386,7 @@ namespace ts {
386386
Diagnostics.Parameter_0_of_accessor_has_or_is_using_private_name_1;
387387

388388
default:
389-
return Debug.fail(`Unknown parent for parameter: ${(ts as any).SyntaxKind[node.parent.kind]}`);
389+
return Debug.fail(`Unknown parent for parameter: ${Debug.formatSyntaxKind(node.parent.kind)}`);
390390
}
391391
}
392392

src/compiler/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,9 @@ namespace ts {
379379
JSDocFunctionType,
380380
JSDocVariadicType,
381381
JSDocNamepathType, // https://jsdoc.app/about-namepaths.html
382+
JSDoc,
382383
/** @deprecated Use SyntaxKind.JSDoc */
383-
JSDocComment,
384+
JSDocComment = JSDoc,
384385
JSDocText,
385386
JSDocTypeLiteral,
386387
JSDocSignature,
@@ -457,7 +458,6 @@ namespace ts {
457458
LastJSDocTagNode = JSDocPropertyTag,
458459
/* @internal */ FirstContextualKeyword = AbstractKeyword,
459460
/* @internal */ LastContextualKeyword = OfKeyword,
460-
JSDoc = JSDocComment,
461461
}
462462

463463
export type TriviaSyntaxKind =

src/harness/harnessUtils.ts

+5-44
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ namespace Utils {
141141
const child = (node as any)[childName];
142142
if (isNodeOrArray(child)) {
143143
assert.isFalse(childNodesAndArrays.indexOf(child) < 0,
144-
"Missing child when forEach'ing over node: " + (ts as any).SyntaxKind[node.kind] + "-" + childName);
144+
"Missing child when forEach'ing over node: " + ts.Debug.formatSyntaxKind(node.kind) + "-" + childName);
145145
}
146146
}
147147
}
@@ -169,54 +169,15 @@ namespace Utils {
169169
export function sourceFileToJSON(file: ts.Node): string {
170170
return JSON.stringify(file, (_, v) => isNodeOrArray(v) ? serializeNode(v) : v, " ");
171171

172-
function getKindName(k: number | string): string {
173-
if (ts.isString(k)) {
172+
function getKindName(k: number | string | undefined): string | undefined {
173+
if (k === undefined || ts.isString(k)) {
174174
return k;
175175
}
176-
177-
// For some markers in SyntaxKind, we should print its original syntax name instead of
178-
// the marker name in tests.
179-
if (k === (ts as any).SyntaxKind.FirstJSDocNode ||
180-
k === (ts as any).SyntaxKind.LastJSDocNode ||
181-
k === (ts as any).SyntaxKind.FirstJSDocTagNode ||
182-
k === (ts as any).SyntaxKind.LastJSDocTagNode) {
183-
for (const kindName in (ts as any).SyntaxKind) {
184-
if ((ts as any).SyntaxKind[kindName] === k) {
185-
return kindName;
186-
}
187-
}
188-
}
189-
190-
return (ts as any).SyntaxKind[k];
191-
}
192-
193-
function getFlagName(flags: any, f: number): any {
194-
if (f === 0) {
195-
return 0;
196-
}
197-
198-
let result = "";
199-
ts.forEach(Object.getOwnPropertyNames(flags), (v: any) => {
200-
if (isFinite(v)) {
201-
v = +v;
202-
if (f === +v) {
203-
result = flags[v];
204-
return true;
205-
}
206-
else if ((f & v) > 0) {
207-
if (result.length) {
208-
result += " | ";
209-
}
210-
result += flags[v];
211-
return false;
212-
}
213-
}
214-
});
215-
return result;
176+
return ts.Debug.formatSyntaxKind(k);
216177
}
217178

218179
function getNodeFlagName(f: number) {
219-
return getFlagName((ts as any).NodeFlags, f);
180+
return ts.Debug.formatNodeFlags(f);
220181
}
221182

222183
function serializeNode(n: ts.Node): any {

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.@link tags.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"modifierFlagsCache": 0,
100100
"transformFlags": 0,
101101
"name": {
102-
"kind": "FirstNode",
102+
"kind": "QualifiedName",
103103
"pos": 86,
104104
"end": 97,
105105
"modifierFlagsCache": 0,
@@ -194,7 +194,7 @@
194194
"modifierFlagsCache": 0,
195195
"transformFlags": 0,
196196
"name": {
197-
"kind": "FirstNode",
197+
"kind": "QualifiedName",
198198
"pos": 163,
199199
"end": 181,
200200
"modifierFlagsCache": 0,
@@ -273,7 +273,7 @@
273273
"modifierFlagsCache": 0,
274274
"transformFlags": 0,
275275
"name": {
276-
"kind": "FirstNode",
276+
"kind": "QualifiedName",
277277
"pos": 333,
278278
"end": 338,
279279
"modifierFlagsCache": 0,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.Nested @param tags.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565
},
6666
"name": {
67-
"kind": "FirstNode",
67+
"kind": "QualifiedName",
6868
"pos": 50,
6969
"end": 53,
7070
"modifierFlagsCache": 0,

tests/baselines/reference/JSDocParsing/TypeExpressions.parsesCorrectly.newType1.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"modifierFlagsCache": 0,
3232
"transformFlags": 1,
3333
"typeName": {
34-
"kind": "FirstNode",
34+
"kind": "QualifiedName",
3535
"pos": 14,
3636
"end": 17,
3737
"flags": "JSDoc",

tests/baselines/reference/JSDocParsing/TypeExpressions.parsesCorrectly.thisType1.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"modifierFlagsCache": 0,
3232
"transformFlags": 1,
3333
"typeName": {
34-
"kind": "FirstNode",
34+
"kind": "QualifiedName",
3535
"pos": 15,
3636
"end": 18,
3737
"flags": "JSDoc",

tests/baselines/reference/JSDocParsing/TypeExpressions.parsesCorrectly.typeReference3.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"modifierFlagsCache": 0,
77
"transformFlags": 1,
88
"typeName": {
9-
"kind": "FirstNode",
9+
"kind": "QualifiedName",
1010
"pos": 1,
1111
"end": 11,
1212
"flags": "JSDoc",

tests/baselines/reference/api/tsserverlibrary.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ declare namespace ts {
425425
JSDocFunctionType = 317,
426426
JSDocVariadicType = 318,
427427
JSDocNamepathType = 319,
428+
JSDoc = 320,
428429
/** @deprecated Use SyntaxKind.JSDoc */
429430
JSDocComment = 320,
430431
JSDocText = 321,
@@ -493,7 +494,6 @@ declare namespace ts {
493494
LastJSDocNode = 347,
494495
FirstJSDocTagNode = 327,
495496
LastJSDocTagNode = 347,
496-
JSDoc = 320
497497
}
498498
export type TriviaSyntaxKind = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia | SyntaxKind.NewLineTrivia | SyntaxKind.WhitespaceTrivia | SyntaxKind.ShebangTrivia | SyntaxKind.ConflictMarkerTrivia;
499499
export type LiteralSyntaxKind = SyntaxKind.NumericLiteral | SyntaxKind.BigIntLiteral | SyntaxKind.StringLiteral | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.RegularExpressionLiteral | SyntaxKind.NoSubstitutionTemplateLiteral;

tests/baselines/reference/api/typescript.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ declare namespace ts {
425425
JSDocFunctionType = 317,
426426
JSDocVariadicType = 318,
427427
JSDocNamepathType = 319,
428+
JSDoc = 320,
428429
/** @deprecated Use SyntaxKind.JSDoc */
429430
JSDocComment = 320,
430431
JSDocText = 321,
@@ -493,7 +494,6 @@ declare namespace ts {
493494
LastJSDocNode = 347,
494495
FirstJSDocTagNode = 327,
495496
LastJSDocTagNode = 347,
496-
JSDoc = 320
497497
}
498498
export type TriviaSyntaxKind = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia | SyntaxKind.NewLineTrivia | SyntaxKind.WhitespaceTrivia | SyntaxKind.ShebangTrivia | SyntaxKind.ConflictMarkerTrivia;
499499
export type LiteralSyntaxKind = SyntaxKind.NumericLiteral | SyntaxKind.BigIntLiteral | SyntaxKind.StringLiteral | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.RegularExpressionLiteral | SyntaxKind.NoSubstitutionTemplateLiteral;

0 commit comments

Comments
 (0)