From 159333f4c51e70a758160975a190646ff55bf66b Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Tue, 21 Aug 2012 11:53:19 +0300 Subject: [PATCH] warn about unreferenced symbols --- lib/output.js | 7 ++----- lib/scope.js | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/output.js b/lib/output.js index e6b2dd13..0d6518df 100644 --- a/lib/output.js +++ b/lib/output.js @@ -806,11 +806,8 @@ function OutputStream(options) { output.print_name(self.name); }); DEFPRINT(AST_SymbolDeclaration, function(self, output){ - if (self.uniq) { - self.uniq.print(output); - } else { - output.print_name(self.mangled_name || self.name); - } + var def = self.definition(); + output.print_name(def.mangled_name || def.name); }); DEFPRINT(AST_SymbolRef, function(self, output){ var def = self.symbol; diff --git a/lib/scope.js b/lib/scope.js index c6d655ee..00bdcb08 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -76,7 +76,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){ options = defaults(options, { - undeclared : false, + undeclared : false, // this makes a lot of noise + unreferenced : true, assign_to_global : true, eval : true }); @@ -114,6 +115,16 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){ && node.name == "eval") { AST_Node.warn("Eval is used [{line},{col}]", node.start); } + if (options.unreferenced + && node instanceof AST_SymbolDeclaration + && node.unreferenced()) { + AST_Node.warn("{type} {name} is declared but not referenced [{line},{col}]", { + type: node instanceof AST_Label ? "Label" : "Symbol", + name: node.name, + line: node.start.line, + col: node.start.col + }); + } }); this.walk(tw); }); @@ -207,6 +218,14 @@ AST_SymbolDeclaration.DEFMETHOD("mangle", function(){ } }); +AST_SymbolDeclaration.DEFMETHOD("unreferenced", function(){ + return this.definition().references.length == 0; +}); + +AST_SymbolDeclaration.DEFMETHOD("definition", function(){ + return this.uniq || this; +}); + AST_Toplevel.DEFMETHOD("mangle_names", function(){ var tw = new TreeWalker(function(node){ // We only need to mangle declarations. Special logic wired -- 2.34.1