js2-mode
js2-mode copied to clipboard
js2-jump-to-definition does not work for class methods
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.
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.
The simplest test case:
var foo = {
bar: 42,
};
foo.bar; // jump to bar
@TatriX That works on my machine.
Yes, my bad. Please try this:
var foo = {
bar: 42,
};
function Foo() {
this.bar = 42;
}
foo.bar; // jump to bar;
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?
+1
I'm also observing this error.
This problem is still happening on emacs 26.1, has anyone found a fix?
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).