<commit_before>var idents = require('javascript-idents'), infer = require('tern/lib/infer'), tern = require('tern');

tern.registerPlugin('ref', function(server, options) {
  var resolvedIdents = [], unresolvedIdents = [];
  return {
    passes: {
      preCondenseReach: function(state) {
        var prevGetSpan = state.getSpan;
        state.getSpan = function(node) {
          var span = prevGetSpan.apply(this, [node]);
          if (span) return node.origin + '@' + span;
        }
      },
      postCondenseReach: function(state) {
        var c = {};
        function getSpan(filename, node) {
          return filename + '@' + node.start + '-' + node.end;
        }
        function getPath(filename, node) {
          return c[getSpan(filename, node)];
        };
        function setPath(span, path) {
          span = span.replace(/\[\d+:\d+\]/g, '');
          if (c[span]) throw new Error('key "' + span + '" is already set to path "' + path + '"');
          return c[span] = path;
        };

        function resolveIdent(file, ident) {
          var target = getPath(file.name, ident);
          if (target) return {path: target, file: file.name};

          var expr = tern.findQueryExpr(file, {start: ident.start, end: ident.end});
          if (expr) {
            var av = infer.expressionType(expr);
            if (!av) throw new Error('!av');
            if (av.originNode) {
              var path = getPath(av.origin, av.originNode)
              if (path) return {path: path, file: av.origin};
            }
            if (av.origin) {
              target = {origin: av.origin};
              var type = av.getType(true);
              if (type) return {path: type.name, origin: av.origin};
            }
          }
        }

        Object.keys(state.types).forEach(function(path) {
          var data = state.types[path];
          if (data.span) setPath(data.span, path);
        });

        state.cx.parent.files.forEach(function(file) {
          idents.inspect(file.ast, function(ident) {
            var target = resolveIdent(file, ident);
            if (target) resolvedIdents.push({file: file.name, start: ident.start, end: ident.end, target: target})
            else unresolvedIdents.push({file: file.name, start: ident.start, name: ident.name});
          });
        });
      },
      postCondense: function(state) {
        state.output['!ref'] = resolvedIdents;
        state.output['!ref_unresolved'] = unresolvedIdents;
      },
    },
  };
});
<commit_msg>Check for findQueryExpr error<commit_after>var idents = require('javascript-idents'), infer = require('tern/lib/infer'), tern = require('tern');

tern.registerPlugin('ref', function(server, options) {
  var resolvedIdents = [], unresolvedIdents = [];
  return {
    passes: {
      preCondenseReach: function(state) {
        var prevGetSpan = state.getSpan;
        state.getSpan = function(node) {
          var span = prevGetSpan.apply(this, [node]);
          if (span) return node.origin + '@' + span;
        }
      },
      postCondenseReach: function(state) {
        var c = {};
        function getSpan(filename, node) {
          return filename + '@' + node.start + '-' + node.end;
        }
        function getPath(filename, node) {
          return c[getSpan(filename, node)];
        };
        function setPath(span, path) {
          span = span.replace(/\[\d+:\d+\]/g, '');
          if (c[span]) throw new Error('key "' + span + '" is already set to path "' + path + '"');
          return c[span] = path;
        };

        function resolveIdent(file, ident) {
          var target = getPath(file.name, ident);
          if (target) return {path: target, file: file.name};

          try { expr = tern.findQueryExpr(file, {start: ident.start, end: ident.end}); }
          catch (e) { console.error('warning: findQueryExpr failed:', e, 'at', ident.name, 'in', file.name, ident.start + '-' + ident.end); }
          if (expr) {
            var av = infer.expressionType(expr);
            if (!av) throw new Error('!av');
            if (av.originNode) {
              var path = getPath(av.origin, av.originNode)
              if (path) return {path: path, file: av.origin};
            }
            if (av.origin) {
              target = {origin: av.origin};
              var type = av.getType(true);
              if (type) return {path: type.name, origin: av.origin};
            }
          }
        }

        Object.keys(state.types).forEach(function(path) {
          var data = state.types[path];
          if (data.span) setPath(data.span, path);
        });

        state.cx.parent.files.forEach(function(file) {
          idents.inspect(file.ast, function(ident) {
            var target = resolveIdent(file, ident);
            if (target) resolvedIdents.push({file: file.name, start: ident.start, end: ident.end, target: target})
            else unresolvedIdents.push({file: file.name, start: ident.start, name: ident.name});
          });
        });
      },
      postCondense: function(state) {
        state.output['!ref'] = resolvedIdents;
        state.output['!ref_unresolved'] = unresolvedIdents;
      },
    },
  };
});

{'fraction_non_alphanumeric': 0.15065586419753085, 'fraction_numerical': 0.0, 'mean_word_length': 2.0410557184750733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '426877', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1508, 'n_words': 345}