Skip to content

Commit 016884d

Browse files
Add assert comments in CodeFixes and Refactors (#33016)
* Add comments to assert calls * Add comments to assert calls in codefixes * So linty
1 parent f6155f8 commit 016884d

18 files changed

+78
-78
lines changed

src/services/codefixes/addConvertToUnknownForNonOverlappingTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ts.codefix {
1414

1515
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
1616
const token = getTokenAtPosition(sourceFile, pos);
17-
const assertion = Debug.assertDefined(findAncestor(token, (n): n is AsExpression | TypeAssertion => isAsExpression(n) || isTypeAssertion(n)));
17+
const assertion = Debug.assertDefined(findAncestor(token, (n): n is AsExpression | TypeAssertion => isAsExpression(n) || isTypeAssertion(n)), "Expected to find an assertion expression");
1818
const replacement = isAsExpression(assertion)
1919
? createAsExpression(assertion.expression, createKeywordTypeNode(SyntaxKind.UnknownKeyword))
2020
: createTypeAssertion(createKeywordTypeNode(SyntaxKind.UnknownKeyword), assertion.expression);

src/services/codefixes/annotateWithTypeFromJSDoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ namespace ts.codefix {
6060
}
6161
}
6262
else {
63-
const jsdocType = Debug.assertDefined(getJSDocType(decl)); // If not defined, shouldn't have been an error to fix
64-
Debug.assert(!decl.type); // If defined, shouldn't have been an error to fix.
63+
const jsdocType = Debug.assertDefined(getJSDocType(decl), "A JSDocType for this declaration should exist"); // If not defined, shouldn't have been an error to fix
64+
Debug.assert(!decl.type, "The JSDocType decl should have a type"); // If defined, shouldn't have been an error to fix.
6565
changes.tryInsertTypeAnnotation(sourceFile, decl, transformJSDocType(jsdocType));
6666
}
6767
}

src/services/codefixes/convertToEs6Module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ namespace ts.codefix {
178178
// `const a = require("b").c` --> `import { c as a } from "./b";
179179
return [makeSingleImport(name.text, propertyName, moduleSpecifier, quotePreference)];
180180
default:
181-
return Debug.assertNever(name);
181+
return Debug.assertNever(name, `Convert to ES6 module got invalid syntax form ${(name as BindingName).kind}`);
182182
}
183183
}
184184

@@ -238,7 +238,7 @@ namespace ts.codefix {
238238
case SyntaxKind.MethodDeclaration:
239239
return !isIdentifier(prop.name) ? undefined : functionExpressionToDeclaration(prop.name.text, [createToken(SyntaxKind.ExportKeyword)], prop);
240240
default:
241-
Debug.assertNever(prop);
241+
Debug.assertNever(prop, `Convert to ES6 got invalid prop kind ${(prop as ObjectLiteralElementLike).kind}`);
242242
}
243243
});
244244
return statements && [statements, false];
@@ -375,7 +375,7 @@ namespace ts.codefix {
375375
case SyntaxKind.Identifier:
376376
return convertSingleIdentifierImport(file, name, moduleSpecifier, changes, checker, identifiers, quotePreference);
377377
default:
378-
return Debug.assertNever(name);
378+
return Debug.assertNever(name, `Convert to ES6 module got invalid name kind ${(name as BindingName).kind}`);
379379
}
380380
}
381381

@@ -399,7 +399,7 @@ namespace ts.codefix {
399399
const { parent } = use;
400400
if (isPropertyAccessExpression(parent)) {
401401
const { expression, name: { text: propertyName } } = parent;
402-
Debug.assert(expression === use); // Else shouldn't have been in `collectIdentifiers`
402+
Debug.assert(expression === use, "Didn't expect expression === use"); // Else shouldn't have been in `collectIdentifiers`
403403
let idName = namedBindingsNames.get(propertyName);
404404
if (idName === undefined) {
405405
idName = makeUniqueName(propertyName, identifiers);

src/services/codefixes/fixAddModuleReferTypeMissingTypeof.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace ts.codefix {
1919

2020
function getImportTypeNode(sourceFile: SourceFile, pos: number): ImportTypeNode {
2121
const token = getTokenAtPosition(sourceFile, pos);
22-
Debug.assert(token.kind === SyntaxKind.ImportKeyword);
23-
Debug.assert(token.parent.kind === SyntaxKind.ImportType);
22+
Debug.assert(token.kind === SyntaxKind.ImportKeyword, "This token should be an ImportKeyword");
23+
Debug.assert(token.parent.kind === SyntaxKind.ImportType, "Token parent should be an ImportType");
2424
return <ImportTypeNode>token.parent;
2525
}
2626

src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace ts.codefix {
2828
});
2929

3030
function getClass(sourceFile: SourceFile, pos: number): ClassLikeDeclaration {
31-
return Debug.assertDefined(getContainingClass(getTokenAtPosition(sourceFile, pos)));
31+
return Debug.assertDefined(getContainingClass(getTokenAtPosition(sourceFile, pos)), "There should be a containing class");
3232
}
3333

3434
function symbolPointsToNonPrivateMember (symbol: Symbol) {

src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ts.codefix {
1717

1818
function getNode(sourceFile: SourceFile, pos: number): ConstructorDeclaration {
1919
const token = getTokenAtPosition(sourceFile, pos);
20-
Debug.assert(token.kind === SyntaxKind.ConstructorKeyword);
20+
Debug.assert(token.kind === SyntaxKind.ConstructorKeyword, "token should be at the constructor keyword");
2121
return token.parent as ConstructorDeclaration;
2222
}
2323

src/services/codefixes/fixSpelling.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ namespace ts.codefix {
3636

3737
let suggestion: string | undefined;
3838
if (isPropertyAccessExpression(node.parent) && node.parent.name === node) {
39-
Debug.assert(node.kind === SyntaxKind.Identifier);
39+
Debug.assert(node.kind === SyntaxKind.Identifier, "Expected an identifier for spelling (property access)");
4040
const containingType = checker.getTypeAtLocation(node.parent.expression);
4141
suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType);
4242
}
4343
else if (isImportSpecifier(node.parent) && node.parent.name === node) {
44-
Debug.assert(node.kind === SyntaxKind.Identifier);
44+
Debug.assert(node.kind === SyntaxKind.Identifier, "Expected an identifier for spelling (import)");
4545
const importDeclaration = findAncestor(node, isImportDeclaration)!;
4646
const resolvedSourceFile = getResolvedSourceFileFromImportDeclaration(sourceFile, context, importDeclaration);
4747
if (resolvedSourceFile && resolvedSourceFile.symbol) {

src/services/codefixes/fixUnreachableCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace ts.codefix {
1515
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number, length: number): void {
1616
const token = getTokenAtPosition(sourceFile, start);
1717
const statement = findAncestor(token, isStatement)!;
18-
Debug.assert(statement.getStart(sourceFile) === token.getStart(sourceFile));
18+
Debug.assert(statement.getStart(sourceFile) === token.getStart(sourceFile), "token and statement should start at the same point");
1919

2020
const container = (isBlock(statement.parent) ? statement.parent : statement).parent;
2121
if (!isBlock(statement.parent) || statement === first(statement.parent.statements)) {
@@ -40,7 +40,7 @@ namespace ts.codefix {
4040

4141
if (isBlock(statement.parent)) {
4242
const end = start + length;
43-
const lastStatement = Debug.assertDefined(lastWhere(sliceAfter(statement.parent.statements, statement), s => s.pos < end));
43+
const lastStatement = Debug.assertDefined(lastWhere(sliceAfter(statement.parent.statements, statement), s => s.pos < end), "Some statement should be last");
4444
changes.deleteNodeRange(sourceFile, statement, lastStatement);
4545
}
4646
else {

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace ts.codefix {
117117
}
118118

119119
function deleteTypeParameters(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node): void {
120-
changes.delete(sourceFile, Debug.assertDefined(cast(token.parent, isDeclarationWithTypeParameterChildren).typeParameters));
120+
changes.delete(sourceFile, Debug.assertDefined(cast(token.parent, isDeclarationWithTypeParameterChildren).typeParameters, "The type parameter to delete should exist"));
121121
}
122122

123123
// Sometimes the diagnostic span is an entire ImportDeclaration, so we should remove the whole thing.
@@ -231,7 +231,7 @@ namespace ts.codefix {
231231
// Can't remove a non-last parameter in a callback. Can remove a parameter in code-fix-all if future parameters are also unused.
232232
const { parameters } = parent;
233233
const index = parameters.indexOf(p);
234-
Debug.assert(index !== -1);
234+
Debug.assert(index !== -1, "The parameter should already be in the list");
235235
return isFixAll
236236
? parameters.slice(index + 1).every(p => p.name.kind === SyntaxKind.Identifier && !p.symbol.isReferenced)
237237
: index === parameters.length - 1;

src/services/codefixes/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace ts.codefix {
8787
ambient ? undefined : createStubbedMethodBody(preferences)));
8888
}
8989
else {
90-
Debug.assertNode(accessor, isSetAccessorDeclaration);
90+
Debug.assertNode(accessor, isSetAccessorDeclaration, "The counterpart to a getter should be a setter");
9191
const parameter = getSetAccessorValueParameter(accessor);
9292
const parameterName = parameter && isIdentifier(parameter.name) ? idText(parameter.name) : undefined;
9393
out(createSetAccessor(
@@ -115,7 +115,7 @@ namespace ts.codefix {
115115
}
116116

117117
if (declarations.length === 1) {
118-
Debug.assert(signatures.length === 1);
118+
Debug.assert(signatures.length === 1, "One declaration implies one signature");
119119
const signature = signatures[0];
120120
outputMethod(signature, modifiers, name, ambient ? undefined : createStubbedMethodBody(preferences));
121121
break;
@@ -132,7 +132,7 @@ namespace ts.codefix {
132132
outputMethod(signature, modifiers, name, createStubbedMethodBody(preferences));
133133
}
134134
else {
135-
Debug.assert(declarations.length === signatures.length);
135+
Debug.assert(declarations.length === signatures.length, "Declarations and signatures should match count");
136136
out(createMethodImplementingSignatures(signatures, name, optional, modifiers, preferences));
137137
}
138138
}

src/services/codefixes/importFixes.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ts.codefix {
5757
pushIfUnique(entry.namedImports, symbolName);
5858
}
5959
else {
60-
Debug.assert(entry.defaultImport === undefined || entry.defaultImport === symbolName);
60+
Debug.assert(entry.defaultImport === undefined || entry.defaultImport === symbolName, "(Add to Existing) Default import should be missing or match symbolName");
6161
entry.defaultImport = symbolName;
6262
}
6363
break;
@@ -70,22 +70,22 @@ namespace ts.codefix {
7070
}
7171
switch (importKind) {
7272
case ImportKind.Default:
73-
Debug.assert(entry.defaultImport === undefined || entry.defaultImport === symbolName);
73+
Debug.assert(entry.defaultImport === undefined || entry.defaultImport === symbolName, "(Add new) Default import should be missing or match symbolName");
7474
entry.defaultImport = symbolName;
7575
break;
7676
case ImportKind.Named:
7777
pushIfUnique(entry.namedImports, symbolName);
7878
break;
7979
case ImportKind.Equals:
8080
case ImportKind.Namespace:
81-
Debug.assert(entry.namespaceLikeImport === undefined || entry.namespaceLikeImport.name === symbolName);
81+
Debug.assert(entry.namespaceLikeImport === undefined || entry.namespaceLikeImport.name === symbolName, "Namespacelike import shoudl be missing or match symbolName");
8282
entry.namespaceLikeImport = { importKind, name: symbolName };
8383
break;
8484
}
8585
break;
8686
}
8787
default:
88-
Debug.assertNever(fix);
88+
Debug.assertNever(fix, `fix wasn't never - got kind ${(fix as ImportFix).kind}`);
8989
}
9090
});
9191

@@ -165,7 +165,7 @@ namespace ts.codefix {
165165
preferences: UserPreferences,
166166
): { readonly moduleSpecifier: string, readonly codeAction: CodeAction } {
167167
const exportInfos = getAllReExportingModules(sourceFile, exportedSymbol, moduleSymbol, symbolName, sourceFile, program.getCompilerOptions(), program.getTypeChecker(), program.getSourceFiles());
168-
Debug.assert(exportInfos.some(info => info.moduleSymbol === moduleSymbol));
168+
Debug.assert(exportInfos.some(info => info.moduleSymbol === moduleSymbol), "Some exportInfo should match the specified moduleSymbol");
169169
// We sort the best codefixes first, so taking `first` is best for completions.
170170
const moduleSpecifier = first(getNewImportInfos(program, sourceFile, position, exportInfos, host, preferences)).moduleSpecifier;
171171
const fix = first(getFixForImport(exportInfos, symbolName, position, program, sourceFile, host, preferences));
@@ -288,7 +288,7 @@ namespace ts.codefix {
288288
moduleSpecifiers.getModuleSpecifiers(moduleSymbol, program.getCompilerOptions(), sourceFile, host, program.getSourceFiles(), preferences, program.redirectTargetsMap)
289289
.map((moduleSpecifier): FixAddNewImport | FixUseImportType =>
290290
// `position` should only be undefined at a missing jsx namespace, in which case we shouldn't be looking for pure types.
291-
exportedSymbolIsTypeOnly && isJs ? { kind: ImportFixKind.ImportType, moduleSpecifier, position: Debug.assertDefined(position) } : { kind: ImportFixKind.AddNew, moduleSpecifier, importKind }));
291+
exportedSymbolIsTypeOnly && isJs ? { kind: ImportFixKind.ImportType, moduleSpecifier, position: Debug.assertDefined(position, "position should be defined") } : { kind: ImportFixKind.AddNew, moduleSpecifier, importKind }));
292292
// Sort to keep the shortest paths first
293293
return sort(choicesForEachExportingModule, (a, b) => a.moduleSpecifier.length - b.moduleSpecifier.length);
294294
}
@@ -369,7 +369,7 @@ namespace ts.codefix {
369369
// Fall back to the `import * as ns` style import.
370370
return ImportKind.Namespace;
371371
default:
372-
return Debug.assertNever(moduleKind);
372+
return Debug.assertNever(moduleKind, `Unexpected moduleKind ${moduleKind}`);
373373
}
374374
}
375375

@@ -382,7 +382,7 @@ namespace ts.codefix {
382382
? checker.getJsxNamespace(sourceFile)
383383
: symbolToken.text;
384384
// "default" is a keyword and not a legal identifier for the import, so we don't expect it here
385-
Debug.assert(symbolName !== InternalSymbolName.Default);
385+
Debug.assert(symbolName !== InternalSymbolName.Default, "'default' isn't a legal identifier and couldn't occur here");
386386

387387
const fixes = arrayFrom(flatMapIterator(getExportInfos(symbolName, getMeaningFromLocation(symbolToken), cancellationToken, sourceFile, checker, program).entries(), ([_, exportInfos]) =>
388388
getFixForImport(exportInfos, symbolName, symbolToken.getStart(sourceFile), program, sourceFile, host, preferences)));
@@ -468,7 +468,7 @@ namespace ts.codefix {
468468

469469
if (defaultExport.flags & SymbolFlags.Alias) {
470470
const aliased = checker.getImmediateAliasedSymbol(defaultExport);
471-
return aliased && getDefaultExportInfoWorker(aliased, Debug.assertDefined(aliased.parent), checker, compilerOptions);
471+
return aliased && getDefaultExportInfoWorker(aliased, Debug.assertDefined(aliased.parent, "Alias targets of default exports must have a parent"), checker, compilerOptions);
472472
}
473473

474474
if (defaultExport.escapedName !== InternalSymbolName.Default &&
@@ -486,7 +486,7 @@ namespace ts.codefix {
486486
}
487487
}
488488
else if (isExportSpecifier(declaration)) {
489-
Debug.assert(declaration.name.text === InternalSymbolName.Default);
489+
Debug.assert(declaration.name.text === InternalSymbolName.Default, "Expected the specifier to be a default export");
490490
return declaration.propertyName && declaration.propertyName.text;
491491
}
492492
});
@@ -521,13 +521,13 @@ namespace ts.codefix {
521521
return [importKind === ImportKind.Default ? Diagnostics.Import_default_0_from_module_1 : Diagnostics.Import_0_from_module_1, symbolName, moduleSpecifier];
522522
}
523523
default:
524-
return Debug.assertNever(fix);
524+
return Debug.assertNever(fix, `Unexpected fix kind ${(fix as ImportFix).kind}`);
525525
}
526526
}
527527

528528
function doAddExistingFix(changes: textChanges.ChangeTracker, sourceFile: SourceFile, clause: ImportClause, defaultImport: string | undefined, namedImports: ReadonlyArray<string>): void {
529529
if (defaultImport) {
530-
Debug.assert(!clause.name);
530+
Debug.assert(!clause.name, "Default imports can't have names");
531531
changes.insertNodeAt(sourceFile, clause.getStart(sourceFile), createIdentifier(defaultImport), { suffix: ", " });
532532
}
533533

@@ -545,7 +545,7 @@ namespace ts.codefix {
545545
changes.replaceNode(sourceFile, clause.namedBindings, namedImports);
546546
}
547547
else {
548-
changes.insertNodeAfter(sourceFile, Debug.assertDefined(clause.name), namedImports);
548+
changes.insertNodeAfter(sourceFile, Debug.assertDefined(clause.name, "Named import specifiers must have names"), namedImports);
549549
}
550550
}
551551
}

src/services/codefixes/inferFromUsage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ namespace ts.codefix {
211211
declaration: p,
212212
type: isIdentifier(p.name) ? inferTypeForVariableFromUsage(p.name, program, cancellationToken) : program.getTypeChecker().getAnyType()
213213
}));
214-
Debug.assert(containingFunction.parameters.length === parameterInferences.length);
214+
Debug.assert(containingFunction.parameters.length === parameterInferences.length, "Parameter count and inference count should match");
215215

216216
if (isInJSFile(containingFunction)) {
217217
annotateJSDocParameters(changes, sourceFile, parameterInferences, program, host);
@@ -741,7 +741,7 @@ namespace ts.codefix {
741741
for (const i of inferences) {
742742
for (const { high, low } of priorities) {
743743
if (high(i)) {
744-
Debug.assert(!low(i));
744+
Debug.assert(!low(i), "Priority can't have both low and high");
745745
toRemove.push(low);
746746
}
747747
}

0 commit comments

Comments
 (0)