Skip to content
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

generateGetAccessorAndSetAccessor: Preserve a parameter property declaration #23318

Merged
3 commits merged into from
May 7, 2018
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
23 changes: 5 additions & 18 deletions src/services/refactors/generateGetAccessorAndSetAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
const accessorModifiers = getAccessorModifiers(isJS, declaration, isStatic, isInClassLike);
const fieldModifiers = getFieldModifiers(isJS, isStatic, isInClassLike);

updateFieldDeclaration(changeTracker, file, declaration, fieldName, fieldModifiers, container);
updateFieldDeclaration(changeTracker, file, declaration, fieldName, fieldModifiers);

const getAccessor = generateGetAccessor(fieldName, accessorName, type, accessorModifiers, isStatic, container);
const setAccessor = generateSetAccessor(fieldName, accessorName, type, accessorModifiers, isStatic, container);
Expand All @@ -61,7 +61,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
const edits = changeTracker.getChanges();
const renameFilename = file.fileName;
const renameLocationOffset = isIdentifier(fieldName) ? 0 : -1;
const renameLocation = renameLocationOffset + getRenameLocation(edits, renameFilename, fieldName.text, /*isDeclaredBeforeUse*/ false);
const renameLocation = renameLocationOffset + getRenameLocation(edits, renameFilename, fieldName.text, /*preferLastLocation*/ isParameter(declaration));
return { renameFilename, renameLocation, edits };
}

Expand Down Expand Up @@ -213,34 +213,21 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
changeTracker.replaceNode(file, declaration, property);
}

function updateParameterPropertyDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: ParameterDeclaration, fieldName: AccepedNameType, modifiers: ModifiersArray | undefined, classLikeContainer: ClassLikeDeclaration) {
const property = createProperty(
declaration.decorators,
modifiers,
fieldName,
declaration.questionToken,
declaration.type,
declaration.initializer
);

changeTracker.insertNodeAtClassStart(file, classLikeContainer, property);
changeTracker.deleteNodeInList(file, declaration);
}

function updatePropertyAssignmentDeclaration (changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyAssignment, fieldName: AccepedNameType) {
const assignment = updatePropertyAssignment(declaration, fieldName, declaration.initializer);
changeTracker.replacePropertyAssignment(file, declaration, assignment);
}

function updateFieldDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: AccepedDeclaration, fieldName: AccepedNameType, modifiers: ModifiersArray | undefined, container: ContainerDeclation) {
function updateFieldDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: AccepedDeclaration, fieldName: AccepedNameType, modifiers: ModifiersArray | undefined) {
if (isPropertyDeclaration(declaration)) {
updatePropertyDeclaration(changeTracker, file, declaration, fieldName, modifiers);
}
else if (isPropertyAssignment(declaration)) {
updatePropertyAssignmentDeclaration(changeTracker, file, declaration, fieldName);
}
else {
updateParameterPropertyDeclaration(changeTracker, file, declaration, fieldName, modifiers, <ClassLikeDeclaration>container);
changeTracker.replaceNode(file, declaration,
updateParameter(declaration, declaration.decorators, modifiers, declaration.dotDotDotToken, cast(fieldName, isIdentifier), declaration.questionToken, declaration.type, declaration.initializer));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ namespace ts {
* user was before extracting it.
*/
/* @internal */
export function getRenameLocation(edits: ReadonlyArray<FileTextChanges>, renameFilename: string, name: string, isDeclaredBeforeUse: boolean): number {
export function getRenameLocation(edits: ReadonlyArray<FileTextChanges>, renameFilename: string, name: string, preferLastLocation: boolean): number {
let delta = 0;
let lastPos = -1;
for (const { fileName, textChanges } of edits) {
Expand All @@ -1554,7 +1554,7 @@ namespace ts {
lastPos = span.start + delta + index;

// If the reference comes first, return immediately.
if (!isDeclaredBeforeUse) {
if (!preferLastLocation) {
return lastPos;
}
}
Expand All @@ -1563,7 +1563,7 @@ namespace ts {
}

// If the declaration comes first, return the position of the last occurrence.
Debug.assert(isDeclaredBeforeUse);
Debug.assert(preferLastLocation);
Debug.assert(lastPos >= 0);
return lastPos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/_a: string;
public get a(): string {
return this._a;
}
public set a(value: string) {
this._a = value;
}
constructor() { }
constructor(private /*RENAME*/_a: string) { }
}`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/_a: string;
protected get a(): string {
return this._a;
}
protected set a(value: string) {
this._a = value;
}
constructor() { }
constructor(private /*RENAME*/_a: string) { }
}`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/_a: string;
public get a(): string {
return this._a;
}
public set a(value: string) {
this._a = value;
}
constructor() { }
constructor(private /*RENAME*/_a: string) { }
}`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/_a: string;
public get a(): string {
return this._a;
}
public set a(value: string) {
this._a = value;
}
public a_1: number;
constructor() { }
constructor(private /*RENAME*/_a: string) { }
}`,
});