From e466bb94c173b1eaaf697590538b0c47651f5efa Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 22 May 2020 09:06:05 -0700 Subject: [PATCH] Fix multi-file usage 1. Follow aliases with getSupers. 2. Make fix in file with superclass, not with the error. --- .../codefixes/fixPropertyOverrideAccessor.ts | 1 + src/services/codefixes/generateAccessors.ts | 7 +++-- .../codeFixPropertyOverrideAccess3.ts | 28 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/codeFixPropertyOverrideAccess3.ts diff --git a/src/services/codefixes/fixPropertyOverrideAccessor.ts b/src/services/codefixes/fixPropertyOverrideAccessor.ts index 4729042bedf12..1941b5ccd5826 100644 --- a/src/services/codefixes/fixPropertyOverrideAccessor.ts +++ b/src/services/codefixes/fixPropertyOverrideAccessor.ts @@ -47,6 +47,7 @@ namespace ts.codefix { startPosition = baseProp.valueDeclaration.pos; endPosition = baseProp.valueDeclaration.end; + file = getSourceFileOfNode(baseProp.valueDeclaration); } else { Debug.fail("fixPropertyOverrideAccessor codefix got unexpected error code " + code); diff --git a/src/services/codefixes/generateAccessors.ts b/src/services/codefixes/generateAccessors.ts index 4b281139f0f23..7fed793a2f1ac 100644 --- a/src/services/codefixes/generateAccessors.ts +++ b/src/services/codefixes/generateAccessors.ts @@ -229,8 +229,11 @@ namespace ts.codefix { while (decl) { const superElement = getClassExtendsHeritageElement(decl); const superSymbol = superElement && checker.getSymbolAtLocation(superElement.expression); - const superDecl = superSymbol && find(superSymbol.declarations, isClassLike); - if (superDecl) { res.push(superDecl); } + if (!superSymbol) break; + const symbol = superSymbol.flags & SymbolFlags.Alias ? checker.getAliasedSymbol(superSymbol) : superSymbol; + const superDecl = find(symbol.declarations, isClassLike); + if (!superDecl) break; + res.push(superDecl); decl = superDecl; } return res; diff --git a/tests/cases/fourslash/codeFixPropertyOverrideAccess3.ts b/tests/cases/fourslash/codeFixPropertyOverrideAccess3.ts new file mode 100644 index 0000000000000..cc83c8e067f09 --- /dev/null +++ b/tests/cases/fourslash/codeFixPropertyOverrideAccess3.ts @@ -0,0 +1,28 @@ +/// + +// @strict: true + +// @Filename: foo.ts +//// import { A } from './source' +//// class B extends A { +//// get x() { return 2 } +//// } +// @Filename: source.ts +//// export class A { +//// x = 1 +//// } + +verify.codeFix({ + description: `Generate 'get' and 'set' accessors`, + newFileContent: { + '/tests/cases/fourslash/source.ts': `export class A { + private _x = 1; + public get x() { + return this._x; + } + public set x(value) { + this._x = value; + } +}`}, + index: 0 +})