Skip to content

addMethodDeclaration: add after quickfix location if possible #24438

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 3 commits into from
May 30, 2018

Conversation

vpukhanov
Copy link
Contributor

Fixes #22674

Declare method quickfix/codeaction now inserts method after the current location.

Current behavior:

class Foo {
    b(): any {
        throw new Error("Method not implemented.");
    }
    a() {
        this.b()
    }
}

Changed behavior:

class Foo {
    a() {
        this.b()
    }
    b(): any {
        throw new Error("Method not implemented.");
    }
}

The old behavior is preserved if quickfix is applied outside of the class. In that case the method is inserted at the start of the class.

class Foo {
    b(): any {
        throw new Error("Method not implemented.");
    }
    f() {

    }
}

// quickfix is called inside of another class
class Bar {
    b() {
        let foo = new Foo();
        foo.b();
    }
}

// or outside of the class
let fooer = new Foo();
fooer.b();

This is my first contribution to TypeScript, so please review carefully :)

@vpukhanov
Copy link
Contributor Author

The tests pass locally, but none of the VSTS or Travis builds succeeded. Is that something to worry about or is this just a temporary issue with the build process?


// Gets the MethodDeclaration of a method of the cls class that contains the node, or undefined if the node is not in a method or that method is not in the cls class.
function getNodeToInsertMethodAfter(cls: ClassLikeDeclaration, node: Node): MethodDeclaration | undefined {
const nodeMethod = getParentMethodDeclaration(node);
Copy link
Contributor

Choose a reason for hiding this comment

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

how about:

function getContainingMethodDeclaration(node: Node, classDeclaration:ClassLikeDeclaration) {
    const method = getAncestor(node, SyntaxKind.MethodDeclaration);
    return method && method.parent === classDeclaration ? method : undefined;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

i would also inline these in the caller.

@@ -199,6 +199,34 @@ namespace ts.codefix {
preferences: UserPreferences,
): void {
const methodDeclaration = createMethodFromCallExpression(callExpression, token.text, inJs, makeStatic, preferences);
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
const currentMethod = getNodeToInsertMethodAfter(classDeclaration, callExpression);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, containingMethodDeclaration

@vpukhanov
Copy link
Contributor Author

Thank you so much @mhegazy! Implemented all of your suggestions, this is a much cleaner code.

@mhegazy mhegazy merged commit c47df3a into microsoft:master May 30, 2018
@mhegazy
Copy link
Contributor

mhegazy commented May 30, 2018

thanks!

@vpukhanov vpukhanov deleted the issue-22674 branch May 30, 2018 17:58
@vpukhanov vpukhanov restored the issue-22674 branch May 30, 2018 17:58
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Declare method quickfix/codeaction should create method after current quick fix location
2 participants