From e9453f411a3599d811e043390a167c40866e9630 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Fri, 21 Jan 2022 00:58:28 +0200 Subject: [PATCH] fix(47524): skip assertion on checking this in fixImplicitThis QF (#47527) --- src/services/codefixes/fixImplicitThis.ts | 4 ++-- ...plicitThis_ts_functionDeclarationInGlobalScope.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts diff --git a/src/services/codefixes/fixImplicitThis.ts b/src/services/codefixes/fixImplicitThis.ts index 743db5c7f35..a373c283052 100644 --- a/src/services/codefixes/fixImplicitThis.ts +++ b/src/services/codefixes/fixImplicitThis.ts @@ -4,7 +4,7 @@ namespace ts.codefix { const errorCodes = [Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation.code]; registerCodeFix({ errorCodes, - getCodeActions(context) { + getCodeActions: function getCodeActionsToFixImplicitThis(context) { const { sourceFile, program, span } = context; let diagnostic: DiagnosticAndArguments | undefined; const changes = textChanges.ChangeTracker.with(context, t => { @@ -20,7 +20,7 @@ namespace ts.codefix { function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, checker: TypeChecker): DiagnosticAndArguments | undefined { const token = getTokenAtPosition(sourceFile, pos); - Debug.assert(token.kind === SyntaxKind.ThisKeyword); + if (!isThis(token)) return undefined; const fn = getThisContainer(token, /*includeArrowFunctions*/ false); if (!isFunctionDeclaration(fn) && !isFunctionExpression(fn)) return undefined; diff --git a/tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts b/tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts new file mode 100644 index 00000000000..a2d51ba8611 --- /dev/null +++ b/tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts @@ -0,0 +1,12 @@ +/// + +// @noImplicitThis: true + +////function foo() { +//// let x: typeof /**/this; +////} + +verify.codeFixAvailable([ + { description: "Infer 'this' type of 'foo' from usage" }, + { description: "Remove unused declaration for: 'x'" } +]);