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
}

0 commit comments

Comments
 (0)