[gnome-builder] gjs_symbols: be compatible with CommonJS too



commit 48af8a91093b66043054f0152f0e9b7c0ba496e9
Author: Giovanni Campagna <gcampagn cs stanford edu>
Date:   Sun Nov 26 02:38:06 2017 -0800

    gjs_symbols: be compatible with CommonJS too
    
    Recognize the Common JS idioms:
    
    "const Foo = require(...)" to import a module
    "module.exports = class Foo { ... }" to declare the main class of
    a module
    
    https://bugzilla.gnome.org/show_bug.cgi?id=790846

 src/plugins/gjs-symbols/gjs_symbols.py |   29 ++++++++++++++++++++++++++++-
 1 files changed, 28 insertions(+), 1 deletions(-)
---
diff --git a/src/plugins/gjs-symbols/gjs_symbols.py b/src/plugins/gjs-symbols/gjs_symbols.py
index e37026d..be9ab12 100644
--- a/src/plugins/gjs-symbols/gjs_symbols.py
+++ b/src/plugins/gjs-symbols/gjs_symbols.py
@@ -151,10 +151,34 @@ class JsSymbolTree(GObject.Object, Ide.SymbolTree):
                                 kind=Ide.SymbolKind.METHOD,
                                 name=name,
                                 file=file_)
+        elif type_ == 'ExpressionStatement' and JsSymbolTree._is_module_exports(dict_):
+            if dict_['expression']['right']['type'] != 'ClassExpression':
+                return None
+            class_ = dict_['expression']['right']
+            children = JsSymbolTree._nodes_from_list(class_['body'], file_)
+            line = max(class_['loc']['start']['line'] - 1, 0)
+            col = class_['loc']['start']['column']
+            return JsSymbolNode(children, line=line, col=col,
+                                kind=Ide.SymbolKind.CLASS,
+                                name=class_['id']['name'],
+                                file=file_)
         else:
             return None
 
     @staticmethod
+    def _is_module_exports(dict_):
+        if dict_['expression']['type'] != 'AssignmentExpression':
+            return False
+        left = dict_['expression']['left']
+        if left['type'] != 'MemberExpression':
+            return False
+        if left['object']['type'] != 'Identifier' or left['object']['name'] != 'module':
+            return False
+        if left['property']['type'] != 'Identifier' or left['property']['name'] != 'exports':
+            return False
+        return True
+
+    @staticmethod
     def _is_module_import(dict_):
         try:
             return dict_['init']['object']['name'] == 'imports'
@@ -162,7 +186,10 @@ class JsSymbolTree(GObject.Object, Ide.SymbolTree):
             try:
                 return dict_['init']['object']['object']['name'] == 'imports'
             except (KeyError, TypeError):
-                return False
+                try:
+                    return dict_['init']['callee']['name'] == 'require'
+                except (KeyError, TypeError):
+                    return False
 
     @staticmethod
     def _is_gobject_class(dict_):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]