js2-mode icon indicating copy to clipboard operation
js2-mode copied to clipboard

js2-jump-to-definition does not work for class methods

Open mostafah opened this issue 8 years ago • 8 comments

This is a small file that can reproduce this problem:

import React, { Component } from "react";

class App extends Component {
  something() {
    return "something";
  }

  render() {
    const name = this.something();
    return (<div className="App">{name}</div>);
  }
}

export default App;

When I hit M-. on this.something(), I get this error:

js2-search-scope: Wrong type argument: js2-name-node, [cl-struct-js2-keyword-node 43 0 4 nil [cl-struct-js2-prop-get-node 33 0 14 nil [cl-struct-js2-call-node 38 7 16 nil [cl-struct-js2-var-init-node 153 6 23 nil [cl-struct-js2-var-decl-node 121 0 29 nil [cl-struct-js2-expr-stmt-node 132 6 30 nil [cl-struct-js2-block-node 128 9 402 nil [cl-struct-js2-function-node 108 0 411 ((METHOD_TYPE FUNCTION)) [cl-struct-js2-method-node -1 77 411 nil [cl-struct-js2-class-node 163 94 490 nil [cl-struct-js2-ast-root 135 1 605 nil nil ... ... nil nil ...] ... CLASS_STATEMENT [cl-struct-js2-name-node 39 6 3 nil #9 "App" nil] [cl-struct-js2-name-node 39 18 9 nil #9 "Component" nil]] nil [cl-struct-js2-name-node 39 0 6 nil #8 "render" nil] #7] nil ((name . [cl-struct-js2-symbol 153 "name" [cl-struct-js2-name-node 39 0 4 nil #3 "name" nil]])) [cl-struct-js2-ast-root 135 1 605 nil nil ([cl-struct-js2-import-node 110 0 41 nil #8 [cl-struct-js2-import-clause-node -1 0 27 nil #10 nil ... [cl-struct-js2-export-binding-node -1 7 5 nil #11 [cl-struct-js2-name-node 39 0 5 nil #12 "React" nil] [cl-struct-js2-name-node 39 0 5 nil #12 "React" nil]]] [cl-struct-js2-from-clause-node 39 28 12 nil #10 "react" nil] "react"] [cl-struct-js2-import-node 110 42 30 nil #8 [cl-struct-js2-import-clause-node -1 0 11 nil #10 nil nil [cl-struct-js2-export-binding-node -1 7 4 nil #11 [cl-struct-js2-name-node 39 0 4 nil #12 "logo" nil] [cl-struct-js2-name-node 39 0 4 nil #12 "logo" nil]]] [cl-struct-js2-from-clause-node 39 12 17 nil #10 "./logo.svg" nil] "./logo.svg"] [cl-struct-js2-import-node 110 73 19 nil #8 nil nil "./App.css"] [cl-struct-js2-class-node 163 94 490 nil #8 ... CLASS_STATEMENT [cl-struct-js2-name-node 39 6 3 nil #10 "App" nil] [cl-struct-js2-name-node 39 18 9 nil #10 "Component" nil]] [cl-struct-js2-export-node 109 586 19 nil #8 nil nil nil [cl-struct-js2-name-node 39 15 4 nil #10 "App" nil]]) (... ... ... ...) nil nil ...] nil ...] (#5 [cl-struct-js2-return-node 4 41 357 nil #6 [cl-struct-js2-paren-node 86 7 349 nil #8 [cl-struct-rjsx-node 58 8 335 nil #9 [cl-struct-rjsx-member 61 1 3 nil #10 nil ...] ... ... [cl-struct-rjsx-closing-tag 59 329 6 nil #10 [cl-struct-rjsx-member 61 568 3 nil nil nil ...]]]]])] #4] (#3) 153] [cl-struct-js2-name-node 39 0 4 nil #3 "name" nil] #2] #1 nil 14 15] 204 #0 [cl-struct-js2-name-node 39 5 9 nil #1 "something" nil]]]

I’m using Emacs 25.1.1, rjsx-mode 0.1.2, and js2-mode 20170503.1846.

mostafah avatar May 10 '17 14:05 mostafah

I don't see it working with "normal" functions either.

@ScottyB, would you like to look into this? On the surface, the problem is that js2-search-scope receives the keyword node corresponding to this in its names list. I'm not sure what to do with it, though.

Can't simply filter it out, I think, because the same example with just something(); ends with No jump location found.

dgutov avatar May 14 '17 01:05 dgutov

The simplest test case:

var foo = {
    bar: 42,
};

foo.bar; // jump to bar

TatriX avatar May 30 '17 12:05 TatriX

@TatriX That works on my machine.

dgutov avatar May 30 '17 12:05 dgutov

Yes, my bad. Please try this:

var foo = {
    bar: 42,
};

function Foo() {
    this.bar = 42;
}

foo.bar; // jump to bar;

TatriX avatar May 30 '17 12:05 TatriX

I think one should check if (car names) of the (js2-search-scope) call is this and instead of js2-get-symbol-declaration use something to find a function/class declaration. What do you think?

TatriX avatar May 30 '17 13:05 TatriX

+1

I'm also observing this error.

mgummelt avatar Nov 26 '17 23:11 mgummelt

This problem is still happening on emacs 26.1, has anyone found a fix?

jimmywongroo avatar Dec 06 '18 12:12 jimmywongroo

I've pushed a couple of fixes. There should be no this-related errors anymore, and all of the examples in this thread now work.

There's still no navigation to a method of an instance, though (like let c = new Class(); c.method(), point on method).

dgutov avatar Jul 12 '21 02:07 dgutov