ctags icon indicating copy to clipboard operation
ctags copied to clipboard

Question about the expected behavior of JavaScript parser

Open masatake opened this issue 6 years ago • 8 comments

I'm working hard to extend cork to support symbol tables.

foo.js (taken from angular.js):

function createChildScopeClass(parent) {
  function ChildScope() {
    this.$$watchers = this.$$nextSibling =
        this.$$childHead = this.$$childTail = null;
    this.$$listeners = {};
    this.$$listenerCount = {};
    this.$$watchersCount = 0;
    this.$id = nextUid();
    this.$$ChildScope = null;
  }
  ChildScope.prototype = parent;
  return ChildScope;
}

Without using the symbol table code, ctags reports ChildScope two times:

[yamato@slave]~/var/ctags-github% u-ctags-no-symbol-table -o - /tmp/foo.js
ChildScope	/tmp/foo.js	/^  ChildScope.prototype = parent;$/;"	c
ChildScope	/tmp/foo.js	/^  function ChildScope() {$/;"	c	function:createChildScopeClass
createChildScopeClass	/tmp/foo.js	/^function createChildScopeClass(parent) {$/;"	f

With the symbol table code, ctags reports it only once:

[yamato@slave]~/var/ctags-github% u-ctags-with-symbol-table -o - /tmp/foo.js
ChildScope	/tmp/foo.js	/^  function ChildScope() {$/;"	c	function:createChildScopeClass
createChildScopeClass	/tmp/foo.js	/^function createChildScopeClass(parent) {$/;"	f

How do you think about this difference?

I think the output of u-ctags-symbol-table is a bit better. However, the evaluation of the quality of tags output is very upto the design of parser especially about dynamically type language.

masatake avatar Jun 29 '19 18:06 masatake

The symbol table is introduced in 19f653c46e0684cdef1e0c40638b4076bfd42b54. So we can implement the idea I proposed in this issue.

masatake avatar Jul 05 '22 21:07 masatake

What needs to be done?

jafl avatar Jul 05 '22 22:07 jafl

How do you think about this idea? Is "unifying the tags for ChildScope into one" a good idea?

masatake avatar Jul 05 '22 22:07 masatake

Yes. I was toying with code to create tags for all fields in an object, by looking for this., and it would also only work if we filtered out duplicates.

jafl avatar Jul 05 '22 22:07 jafl

I see. Yes, please, go ahead.

masatake avatar Jul 05 '22 22:07 masatake

What do I need to do to build u-ctags-with-symbol-table?

jafl avatar Jul 05 '22 22:07 jafl

The symbol table is already introduced to the JavaScript parser. In the names of function level, symbol-table means registerEntry and anyKindsEntryInScope.

If you register a tag entry filling its scopeIndex member, you can find the tag entry in the scope specifying the scopeIndex LATER.

registerEntry() is for registering. anyKindsEntryInScope() is one of the function you can use for finding. More functions can be found in ... entry.h: foreachEntriesInScope and any*.

Both ChildScope are defined in createChildScopeClass. So register the first ChildScope after filling its scopeIndex with the cork index of createChildScopeClass.

When finding the second one, look up the table with anyEntryInScope or something. The any* may return the first one. So you can avoid tagging the second one.

If you find interesting information in the second one, you can attach the information to the first one.

masatake avatar Jul 05 '22 22:07 masatake

Got it. Thanks!

jafl avatar Jul 05 '22 23:07 jafl