|
Lines 42-47
Link Here
|
| 42 |
* @returns The status if we should continue visiting |
42 |
* @returns The status if we should continue visiting |
| 43 |
*/ |
43 |
*/ |
| 44 |
enter: function(node) { |
44 |
enter: function(node) { |
|
|
45 |
var len, idx; |
| 45 |
switch(node.type) { |
46 |
switch(node.type) { |
| 46 |
case Estraverse.Syntax.Program: |
47 |
case Estraverse.Syntax.Program: |
| 47 |
this.occurrences = []; |
48 |
this.occurrences = []; |
|
Lines 54-61
Link Here
|
| 54 |
//we want the parent scope for a declaration, otherwise we leave it right away |
55 |
//we want the parent scope for a declaration, otherwise we leave it right away |
| 55 |
this._enterScope(node); |
56 |
this._enterScope(node); |
| 56 |
if (node.params) { |
57 |
if (node.params) { |
| 57 |
for (var i = 0; i < node.params.length; i++) { |
58 |
len = node.params.length; |
| 58 |
if(this.checkId(node.params[i], true)) { |
59 |
for (idx = 0; idx < len; idx++) { |
|
|
60 |
if(this.checkId(node.params[idx], true)) { |
| 59 |
return Estraverse.VisitorOption.Skip; |
61 |
return Estraverse.VisitorOption.Skip; |
| 60 |
} |
62 |
} |
| 61 |
} |
63 |
} |
|
Lines 66-73
Link Here
|
| 66 |
if(this._enterScope(node)) { |
68 |
if(this._enterScope(node)) { |
| 67 |
return Estraverse.VisitorOption.Skip; |
69 |
return Estraverse.VisitorOption.Skip; |
| 68 |
} |
70 |
} |
| 69 |
for (var j = 0; j < node.params.length; j++) { |
71 |
len = node.params.length; |
| 70 |
if(this.checkId(node.params[j], true)) { |
72 |
for (idx = 0; idx < len; idx++) { |
|
|
73 |
if(this.checkId(node.params[idx], true)) { |
| 71 |
return Estraverse.VisitorOption.Skip; |
74 |
return Estraverse.VisitorOption.Skip; |
| 72 |
} |
75 |
} |
| 73 |
} |
76 |
} |
|
Lines 79-86
Link Here
|
| 79 |
break; |
82 |
break; |
| 80 |
case Estraverse.Syntax.ArrayExpression: |
83 |
case Estraverse.Syntax.ArrayExpression: |
| 81 |
if (node.elements) { |
84 |
if (node.elements) { |
| 82 |
for (var k = 0; k < node.elements.length; k++) { |
85 |
len = node.elements.length; |
| 83 |
this.checkId(node.elements[k]); |
86 |
for (idx = 0; idx < len; idx++) { |
|
|
87 |
this.checkId(node.elements[idx]); |
| 84 |
} |
88 |
} |
| 85 |
} |
89 |
} |
| 86 |
break; |
90 |
break; |
|
Lines 114-121
Link Here
|
| 114 |
case Estraverse.Syntax.CallExpression: |
118 |
case Estraverse.Syntax.CallExpression: |
| 115 |
this.checkId(node.callee, false); |
119 |
this.checkId(node.callee, false); |
| 116 |
if (node.arguments) { |
120 |
if (node.arguments) { |
| 117 |
for (var l = 0; l < node.arguments.length; l++) { |
121 |
len = node.arguments.length; |
| 118 |
this.checkId(node.arguments[l]); |
122 |
for (idx = 0; idx < len; idx++) { |
|
|
123 |
this.checkId(node.arguments[idx]); |
| 119 |
} |
124 |
} |
| 120 |
} |
125 |
} |
| 121 |
break; |
126 |
break; |
|
Lines 127-135
Link Here
|
| 127 |
return Estraverse.VisitorOption.Skip; |
132 |
return Estraverse.VisitorOption.Skip; |
| 128 |
} |
133 |
} |
| 129 |
if(node.properties) { |
134 |
if(node.properties) { |
| 130 |
var len = node.properties.length; |
135 |
len = node.properties.length; |
| 131 |
for (var m = 0; m < len; m++) { |
136 |
for (idx = 0; idx < len; idx++) { |
| 132 |
var prop = node.properties[m]; |
137 |
var prop = node.properties[idx]; |
| 133 |
if(this.thisCheck && prop.value && prop.value.type === Estraverse.Syntax.FunctionExpression) { |
138 |
if(this.thisCheck && prop.value && prop.value.type === Estraverse.Syntax.FunctionExpression) { |
| 134 |
//tag it |
139 |
//tag it |
| 135 |
prop.value.isprop = true; |
140 |
prop.value.isprop = true; |
|
Lines 145-153
Link Here
|
| 145 |
case Estraverse.Syntax.NewExpression: |
150 |
case Estraverse.Syntax.NewExpression: |
| 146 |
this.checkId(node.callee, false); |
151 |
this.checkId(node.callee, false); |
| 147 |
if(node.arguments) { |
152 |
if(node.arguments) { |
| 148 |
var len = node.arguments.length; |
153 |
len = node.arguments.length; |
| 149 |
for(var m = 0; m < len; m++) { |
154 |
for(idx = 0; idx < len; idx++) { |
| 150 |
this.checkId(node.arguments[m]); |
155 |
this.checkId(node.arguments[idx]); |
| 151 |
} |
156 |
} |
| 152 |
} |
157 |
} |
| 153 |
break; |
158 |
break; |
|
Lines 163-169
Link Here
|
| 163 |
end: node.range[1] |
168 |
end: node.range[1] |
| 164 |
}); |
169 |
}); |
| 165 |
// if this node is the selected this we are in the right scope |
170 |
// if this node is the selected this we are in the right scope |
| 166 |
if (node.range[0] === this.context.node.range[0]){ |
171 |
if (node.range[0] === this.context.token.range[0]){ |
| 167 |
this.defscope = scope; |
172 |
this.defscope = scope; |
| 168 |
} |
173 |
} |
| 169 |
} |
174 |
} |
|
Lines 343-349
Link Here
|
| 343 |
this.visitor.enter = this.visitor.enter.bind(this.visitor); |
348 |
this.visitor.enter = this.visitor.enter.bind(this.visitor); |
| 344 |
this.visitor.leave = this.visitor.leave.bind(this.visitor); |
349 |
this.visitor.leave = this.visitor.leave.bind(this.visitor); |
| 345 |
} |
350 |
} |
| 346 |
this.visitor.thisCheck = context.node && context.node.type === Estraverse.Syntax.ThisExpression; |
351 |
this.visitor.thisCheck = context.token && context.token.type === 'Keyword' && context.token.value === 'this'; |
| 347 |
this.visitor.context = context; |
352 |
this.visitor.context = context; |
| 348 |
return this.visitor; |
353 |
return this.visitor; |
| 349 |
}, |
354 |
}, |
|
Lines 352-364
Link Here
|
| 352 |
* @description Computes the node name to use while searching |
357 |
* @description Computes the node name to use while searching |
| 353 |
* @function |
358 |
* @function |
| 354 |
* @private |
359 |
* @private |
| 355 |
* @param {Object} node The AST node |
360 |
* @param {Object} token The AST token |
| 356 |
* @returns {String} The node name to use while seraching |
361 |
* @returns {String} The node name to use while seraching |
| 357 |
*/ |
362 |
*/ |
| 358 |
_nameFromNode: function(node) { |
363 |
_nameFromNode: function(token) { |
| 359 |
switch(node.type) { |
364 |
switch(token.type) { |
| 360 |
case Estraverse.Syntax.Identifier: return node.name; |
365 |
case Estraverse.Syntax.Identifier: return token.value; |
| 361 |
case Estraverse.Syntax.ThisExpression: return 'this'; |
366 |
case 'Keyword': return 'this'; |
| 362 |
} |
367 |
} |
| 363 |
}, |
368 |
}, |
| 364 |
|
369 |
|
|
Lines 367-385
Link Here
|
| 367 |
* @function |
372 |
* @function |
| 368 |
* @private |
373 |
* @private |
| 369 |
* @param {Object} context The selection context from the editor |
374 |
* @param {Object} context The selection context from the editor |
| 370 |
* @param {Object} node The AST node |
375 |
* @param {Object} token The AST token |
| 371 |
* @param {Object} ast The AST |
376 |
* @param {Object} ast The AST |
| 372 |
* @returns {Boolean} True if we shoud skip computing occurrences |
377 |
* @returns {Boolean} True if we shoud skip computing occurrences |
| 373 |
*/ |
378 |
*/ |
| 374 |
_skip: function(context, node, ast) { |
379 |
_skip: function(context, token, ast) { |
| 375 |
if(!node || node.type === Estraverse.Syntax.Literal) { |
380 |
if(!token) { |
| 376 |
return true; |
|
|
| 377 |
} |
| 378 |
var comment = Finder.findComment(context.selection.start, ast); |
| 379 |
if(comment) { |
| 380 |
return true; |
381 |
return true; |
| 381 |
} |
382 |
} |
| 382 |
return false; |
383 |
if(token.type === 'Keyword') { |
|
|
384 |
return token.value !== 'this'; |
| 385 |
} |
| 386 |
return token.type !== Estraverse.Syntax.Identifier |
| 387 |
}, |
| 388 |
|
| 389 |
/** |
| 390 |
* @description Gets the token from the given offset or the proceeding token if the found token |
| 391 |
* is a punctuator |
| 392 |
* @function |
| 393 |
* @private |
| 394 |
* @param {Number} offset The offset into the source |
| 395 |
* @param {Object} ast The AST |
| 396 |
* @return {Object} The token for the given offset or null |
| 397 |
*/ |
| 398 |
_getToken: function(offset, ast) { |
| 399 |
if(ast.tokens && ast.tokens.length > 0) { |
| 400 |
var token = Finder.findToken(offset, ast.tokens); |
| 401 |
if(token) { |
| 402 |
if(token.type === 'Punctuator') { |
| 403 |
var index = token.index; |
| 404 |
//only check back if we are at the start of the punctuator i.e. here -> { |
| 405 |
if(offset === token.range[0] && index != null && index > 0) { |
| 406 |
var prev = ast.tokens[index-1]; |
| 407 |
if(prev.range[1] !== token.range[0]) { |
| 408 |
return null; |
| 409 |
} |
| 410 |
else { |
| 411 |
token = prev; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
if(token.type === 'Identifier' || (token.type === 'Keyword' && token.value === 'this')) { |
| 416 |
return token; |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
return null; |
| 383 |
}, |
421 |
}, |
| 384 |
|
422 |
|
| 385 |
/** |
423 |
/** |
|
Lines 395-407
Link Here
|
| 395 |
var that = this; |
433 |
var that = this; |
| 396 |
return this.astManager.getAST(editorContext).then(function(ast) { |
434 |
return this.astManager.getAST(editorContext).then(function(ast) { |
| 397 |
if(ast) { |
435 |
if(ast) { |
| 398 |
var node = Finder.findNode(ctxt.selection.start, ast); |
436 |
var token = that._getToken(ctxt.selection.start, ast); |
| 399 |
if(!that._skip(ctxt, node, ast)) { |
437 |
if(!that._skip(ctxt, token, ast)) { |
| 400 |
var context = { |
438 |
var context = { |
| 401 |
start: ctxt.selection.start, |
439 |
start: ctxt.selection.start, |
| 402 |
end: ctxt.selection.end, |
440 |
end: ctxt.selection.end, |
| 403 |
word: that._nameFromNode(node), |
441 |
word: that._nameFromNode(token), |
| 404 |
node: node, |
442 |
token: token, |
| 405 |
}; |
443 |
}; |
| 406 |
var visitor = that.getVisitor(context); |
444 |
var visitor = that.getVisitor(context); |
| 407 |
Estraverse.traverse(ast, visitor); |
445 |
Estraverse.traverse(ast, visitor); |