Skip to content

Add codefix for --noImplicitThis #27565

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 10 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More redundancy removal
Don't need to add `@this` anymore either since inferFromUsage will do
that.
  • Loading branch information
sandersn committed Mar 17, 2020
commit 8074c74202a37c4ef30e5cd79e5d39d58775f6ca
12 changes: 4 additions & 8 deletions src/services/codefixes/fixImplicitThis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace ts.codefix {
const fn = getThisContainer(token, /*includeArrowFunctions*/ false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we be in a default argument of the function? Does that matter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a parameter initializer, 'this' refers to the same 'this' as inside the body of the function, not to 'this' of an outer function. So this will work just as well in that case.

if (!isFunctionDeclaration(fn) && !isFunctionExpression(fn)) return undefined;

// TODO: Flip this conditional
if (!isSourceFile(getThisContainer(fn, /*includeArrowFunctions*/ false))) { // 'this' is defined outside, convert to arrow function
const fnKeyword = Debug.assertDefined(findChildOfKind(fn, SyntaxKind.FunctionKeyword, sourceFile));
const { name } = fn;
Expand Down Expand Up @@ -53,13 +52,10 @@ namespace ts.codefix {
return [Diagnostics.Convert_function_declaration_0_to_arrow_function, name!.text];
}
}
// TODO: inline this conditional after the above is flipped
else { // No outer 'this', must add a jsdoc tag / parameter
if (isSourceFileJS(sourceFile)) {
const addClassTag = isPropertyAccessExpression(token.parent) && isAssignmentExpression(token.parent.parent);
addJSDocTags(changes, sourceFile, fn, [addClassTag ? createJSDocClassTag() : createJSDocThisTag(createJSDocTypeExpression(createKeywordTypeNode(SyntaxKind.AnyKeyword)))]);
return addClassTag ? Diagnostics.Add_class_tag : Diagnostics.Add_this_tag;
}
// No outer 'this', add a @class tag if in a JS constructor function
else if (isSourceFileJS(sourceFile) && isPropertyAccessExpression(token.parent) && isAssignmentExpression(token.parent.parent)) {
addJSDocTags(changes, sourceFile, fn, [createJSDocClassTag()]);
return Diagnostics.Add_class_tag;
}
}
}
9 changes: 0 additions & 9 deletions tests/cases/fourslash/codeFixImplicitThis_js_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
////function f() {
//// this.x = 1;
////}
////function g() {
//// this;
////}
////class C {
//// m() {
//// function h() {
Expand All @@ -29,12 +26,6 @@ verify.codeFixAll({
function f() {
this.x = 1;
}
/**
* @this {any}
*/
function g() {
this;
}
class C {
m() {
const h = () => {
Expand Down
22 changes: 0 additions & 22 deletions tests/cases/fourslash/codeFixImplicitThis_js_typeTag.ts

This file was deleted.