Skip to content

Fix unnecessary non-null assertions caught by newer ts-eslint #57070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7825,7 +7825,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (context.tracker.canTrackSymbol && isComputedPropertyName(node) && isLateBindableName(node)) {
trackComputedName(node.expression, context.enclosingDeclaration, context);
}
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, /*context*/ undefined, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, /*context*/ undefined, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags);
if (isBindingElement(visited)) {
visited = factory.updateBindingElement(
visited,
Expand Down Expand Up @@ -32629,7 +32629,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Find the first enclosing class that has the declaring classes of the protected constituents
// of the property as base classes
let enclosingClass = forEachEnclosingClass(location, enclosingDeclaration => {
const enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfDeclaration(enclosingDeclaration)!) as InterfaceType;
const enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfDeclaration(enclosingDeclaration)) as InterfaceType;
return isClassDerivedFromDeclaringClasses(enclosingClass, prop, writing);
});
// A protected property is accessible if the property is within the declaring class or classes derived from it
Expand Down Expand Up @@ -36278,7 +36278,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getTypeOfSymbol(symbol);
}
else {
const symbol = getSymbolOfDeclaration(container)!;
const symbol = getSymbolOfDeclaration(container);
return getTypeOfSymbol(symbol);
}
}
Expand Down Expand Up @@ -40068,7 +40068,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// TypeScript 1.0 spec (April 2014)
// 3.7.4: An object type can contain at most one string index signature and one numeric index signature.
// 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration
const indexSymbol = getIndexSymbol(getSymbolOfDeclaration(node)!);
const indexSymbol = getIndexSymbol(getSymbolOfDeclaration(node));
if (indexSymbol?.declarations) {
const indexSignatureMap = new Map<TypeId, { type: Type; declarations: IndexSignatureDeclaration[]; }>();
for (const declaration of (indexSymbol.declarations as IndexSignatureDeclaration[])) {
Expand Down Expand Up @@ -41025,7 +41025,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.NamespaceImport:
case SyntaxKind.ImportClause:
let result = DeclarationSpaces.None;
const target = resolveAlias(getSymbolOfDeclaration(d as ImportEqualsDeclaration | NamespaceImport | ImportClause | ExportAssignment | BinaryExpression)!);
const target = resolveAlias(getSymbolOfDeclaration(d as ImportEqualsDeclaration | NamespaceImport | ImportClause | ExportAssignment | BinaryExpression));
forEach(target.declarations, d => {
result |= getDeclarationSpaces(d);
});
Expand Down Expand Up @@ -47539,7 +47539,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

if (isDeclarationNameOrImportPropertyName(node)) {
// This is a declaration, call getSymbolOfNode
const parentSymbol = getSymbolOfDeclaration(parent as Declaration)!;
const parentSymbol = getSymbolOfDeclaration(parent as Declaration);
return isImportOrExportSpecifier(node.parent) && node.parent.propertyName === node
? getImmediateAliasedSymbol(parentSymbol)
: parentSymbol;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3068,7 +3068,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
// If the number will be printed verbatim and it doesn't already contain a dot or an exponent indicator, add one
// if the expression doesn't have any comments that will be emitted.
return !(expression.numericLiteralFlags & TokenFlags.WithSpecifier)
&& !text.includes(tokenToString(SyntaxKind.DotToken)!)
&& !text.includes(tokenToString(SyntaxKind.DotToken))
&& !text.includes(String.fromCharCode(CharacterCodes.E))
&& !text.includes(String.fromCharCode(CharacterCodes.e));
}
Expand Down
4 changes: 2 additions & 2 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class SessionClient implements LanguageService {
let foundResponseMessage = false;
let response!: T;
while (!foundResponseMessage) {
const lastMessage = this.messages.dequeue()!;
const lastMessage = this.messages.dequeue();
Debug.assert(!!lastMessage, "Did not receive any responses.");
const responseBody = extractMessage(lastMessage);
try {
Expand Down Expand Up @@ -842,7 +842,7 @@ export class SessionClient implements LanguageService {

const request = this.processRequest<protocol.GetMoveToRefactoringFileSuggestionsRequest>(protocol.CommandTypes.GetMoveToRefactoringFileSuggestions, args);
const response = this.processResponse<protocol.GetMoveToRefactoringFileSuggestions>(request);
return { newFileName: response.body?.newFileName, files: response.body?.files }!;
return { newFileName: response.body?.newFileName, files: response.body?.files };
}

getEditsForRefactor(
Expand Down
2 changes: 1 addition & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5369,7 +5369,7 @@ function getContextualKeywords(
&& tokenLine === currentLine
) {
entries.push({
name: tokenToString(SyntaxKind.AssertKeyword)!,
name: tokenToString(SyntaxKind.AssertKeyword),
kind: ScriptElementKind.keyword,
kindModifiers: ScriptElementKindModifier.none,
sortText: SortText.GlobalsOrKeywords,
Expand Down
2 changes: 1 addition & 1 deletion src/services/documentRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export function createDocumentRegistryInternal(useCaseSensitiveFileNames?: boole
// the script snapshot. If so, update it appropriately. Otherwise, we can just
// return it as is.
if (entry.sourceFile.version !== version) {
entry.sourceFile = updateLanguageServiceSourceFile(entry.sourceFile, scriptSnapshot, version, scriptSnapshot.getChangeRange(entry.sourceFile.scriptSnapshot!)); // TODO: GH#18217
entry.sourceFile = updateLanguageServiceSourceFile(entry.sourceFile, scriptSnapshot, version, scriptSnapshot.getChangeRange(entry.sourceFile.scriptSnapshot));
if (externalCache) {
externalCache.setDocument(keyWithMode, path, entry.sourceFile);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ export function isInsideJsxElement(sourceFile: SourceFile, position: number): bo
/** @internal */
export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind.OpenBraceToken | SyntaxKind.OpenParenToken | SyntaxKind.OpenBracketToken, sourceFile: SourceFile) {
const closeTokenText = tokenToString(token.kind)!;
const matchingTokenText = tokenToString(matchingTokenKind)!;
const matchingTokenText = tokenToString(matchingTokenKind);
const tokenFullStart = token.getFullStart();
// Text-scan based fast path - can be bamboozled by comments and other trivia, but often provides
// a good, fast approximation without too much extra work in the cases where it fails.
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/helpers/baseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export function baselineBuildInfo(
if (!buildInfoPath || !sys.writtenFiles!.has(toPathWithSystem(sys, buildInfoPath))) return;
if (!sys.fileExists(buildInfoPath)) return;

const buildInfo = ts.getBuildInfo(buildInfoPath, (originalReadCall || sys.readFile).call(sys, buildInfoPath, "utf8")!);
const buildInfo = ts.getBuildInfo(buildInfoPath, (originalReadCall || sys.readFile).call(sys, buildInfoPath, "utf8"));
if (!buildInfo) return sys.writeFile(`${buildInfoPath}.baseline.txt`, "Error reading valid buildinfo file");
generateBuildInfoProgramBaseline(sys, buildInfoPath, buildInfo);

Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsbuild/outFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ ${internal} enum internalEnum { a, b, c }`,
fs.writeFileSync(
"/src/third/tsconfig.json",
jsonToReadableText({
...JSON.parse(fs.readFileSync("/src/third/tsconfig.json", "utf-8")!),
...JSON.parse(fs.readFileSync("/src/third/tsconfig.json", "utf-8")),
references: [{ path: "../second", prepend: true }],
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("unittests :: internalApi :: typeParameterIsPossiblyReferenced", () =>
.type as ts.TypeQueryNode // typeof a
;
const typeParameterDecl = (file.statements[0] as ts.FunctionDeclaration).typeParameters![0]; // T in f<T>
const typeParameter = checker.getTypeAtLocation(typeParameterDecl)! as ts.TypeParameter;
const typeParameter = checker.getTypeAtLocation(typeParameterDecl) as ts.TypeParameter;
const isReferenced = checker.isTypeParameterPossiblyReferenced(typeParameter, typeQueryNode);
assert.ok(isReferenced, "Type parameter is referenced in type query node");
});
Expand Down