Use TreeWalker for more accurate @const results and update tests
This commit is contained in:
32
lib/scope.js
32
lib/scope.js
@@ -358,13 +358,31 @@ AST_Symbol.DEFMETHOD("global", function(){
|
||||
});
|
||||
|
||||
AST_Symbol.DEFMETHOD("has_const_pragma", function() {
|
||||
var token = this.scope.body[0] && this.scope.body[0].start;
|
||||
var comments = token && token.comments_before;
|
||||
if (comments && comments.length > 0) {
|
||||
var last = comments[comments.length - 1];
|
||||
return /@const/.test(last.value);
|
||||
}
|
||||
return false;
|
||||
var symbol = this;
|
||||
var symbol_has_pragma = false;
|
||||
var pragma_found = false;
|
||||
var found_symbol = false;
|
||||
// Walk the current scope, looking for a comment with the @const pragma.
|
||||
// If it exists, mark a bool that will remain true only for the next iteration.
|
||||
// If the next iteration is this symbol, then we return true.
|
||||
// Otherwise we stop descending and get out of here.
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
// This is our symbol. Was the pragma before this?
|
||||
if (node.name === symbol) {
|
||||
found_symbol = true;
|
||||
symbol_has_pragma = pragma_found;
|
||||
}
|
||||
|
||||
// Look for the /** @const */ pragma
|
||||
var comments_before = node.start && node.start.comments_before;
|
||||
var lastComment = comments_before && comments_before[comments_before.length - 1];
|
||||
pragma_found = lastComment && /@const/.test(lastComment.value);
|
||||
|
||||
// no need to descend after finding our node
|
||||
return found_symbol;
|
||||
});
|
||||
this.scope.walk(tw);
|
||||
return symbol_has_pragma;
|
||||
})
|
||||
|
||||
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
|
||||
|
||||
Reference in New Issue
Block a user