[javascriptlint-commit] SF.net SVN: javascriptlint:[257] trunk
Status: Beta
Brought to you by:
matthiasmiller
|
From: <mat...@us...> - 2009-10-03 17:39:51
|
Revision: 257
http://javascriptlint.svn.sourceforge.net/javascriptlint/?rev=257&view=rev
Author: matthiasmiller
Date: 2009-10-03 17:39:40 +0000 (Sat, 03 Oct 2009)
Log Message:
-----------
Break unreferenced_identifier into unreferenced_argument, unreferenced_variable, and unreferenced_function.
Modified Paths:
--------------
trunk/javascriptlint/lint.py
trunk/javascriptlint/warnings.py
trunk/test.py
trunk/tests/warnings/unreferenced_identifier.js
Modified: trunk/javascriptlint/lint.py
===================================================================
--- trunk/javascriptlint/lint.py 2009-10-03 17:14:49 UTC (rev 256)
+++ trunk/javascriptlint/lint.py 2009-10-03 17:39:40 UTC (rev 257)
@@ -88,21 +88,31 @@
self._kids[-1]._parent = self
self._kids[-1]._node = node
return self._kids[-1]
- def add_declaration(self, name, node):
- self._identifiers[name] = node
+ def add_declaration(self, name, node, type_):
+ assert type_ in ('arg', 'function', 'var'), \
+ 'Unrecognized identifier type: %s' % type_
+ self._identifiers[name] = {
+ 'node': node,
+ 'type': type_
+ }
def add_reference(self, name, node):
self._references.append((name, node))
def get_identifier(self, name):
if name in self._identifiers:
- return self._identifiers[name]
+ return self._identifiers[name]['node']
else:
return None
+ def get_identifier_type(self, name):
+ if name in self._identifiers:
+ return self._identifiers[name]['type']
+ else:
+ return None
def get_identifiers(self):
"returns a list of names"
return self._identifiers.keys()
def resolve_identifier(self, name):
if name in self._identifiers:
- return self, self._identifiers[name]
+ return self, self._identifiers[name]['node']
if self._parent:
return self._parent.resolve_identifier(name)
return None
@@ -139,8 +149,8 @@
# Add all identifiers as unreferenced. Children scopes will remove
# them if they are referenced. Variables need to be keyed by name
# instead of node, because function parameters share the same node.
- for name, node in self._identifiers.items():
- unreferenced[(self, name)] = node
+ for name, info in self._identifiers.items():
+ unreferenced[(self, name)] = info['node']
# Remove all declared variables from the "unreferenced" set; add all
# undeclared variables to the "undeclared" list.
@@ -382,7 +392,7 @@
if declare_scope.get_identifier(name):
report(node, 'redeclared_var', name=name)
else:
- declare_scope.add_declaration(name, node)
+ declare_scope.add_declaration(name, node, 'var')
def _lint_script_parts(script_parts, script_cache, lint_error, conf, import_callback):
def report_lint(node, errname, pos=None, **errargs):
@@ -424,7 +434,15 @@
for ref_scope, name, node in unreferenced:
# Ignore the outer scope.
if ref_scope != scope:
- report_lint(node, 'unreferenced_identifier', name=name)
+ type_ = ref_scope.get_identifier_type(name)
+ if type_ == 'arg':
+ report_lint(node, 'unreferenced_argument', name=name)
+ elif type_ == 'function':
+ report_lint(node, 'unreferenced_function', name=name)
+ elif type_ == 'var':
+ report_lint(node, 'unreferenced_variable', name=name)
+ else:
+ assert False, 'Unrecognized identifier type: %s' % type_
def _getreporter(visitor, report):
def onpush(node):
@@ -441,7 +459,7 @@
report(warning.node, visitor.warning, pos=pos, **warning.errargs)
return onpush
-def _warn_or_declare(scope, name, node, report):
+def _warn_or_declare(scope, name, type_, node, report):
parent_scope, other = scope.resolve_identifier(name) or (None, None)
if other and other.kind == tok.FUNCTION and name in other.fn_args:
report(node, 'var_hides_arg', name=name)
@@ -449,7 +467,7 @@
report(node, 'redeclared_var', name=name)
else:
# TODO: Warn when hiding a variable in a parent scope.
- scope.add_declaration(name, node)
+ scope.add_declaration(name, node, type_)
def _get_scope_checks(scope, report):
scopes = [scope]
@@ -461,19 +479,19 @@
if node.node_index == 0 and node.parent.kind == tok.COLON and node.parent.parent.kind == tok.RC:
return # left side of object literal
if node.parent.kind == tok.VAR:
- _warn_or_declare(scopes[-1], node.atom, node, report)
+ _warn_or_declare(scopes[-1], node.atom, 'var', node, report)
return
if node.parent.kind == tok.CATCH:
- scopes[-1].add_declaration(node.atom, node)
+ scopes[-1].add_declaration(node.atom, node, 'var')
scopes[-1].add_reference(node.atom, node)
@visitation.visit('push', tok.FUNCTION)
def _push_func(self, node):
if node.fn_name:
- _warn_or_declare(scopes[-1], node.fn_name, node, report)
+ _warn_or_declare(scopes[-1], node.fn_name, 'function', node, report)
self._push_scope(node)
for var_name in node.fn_args:
- scopes[-1].add_declaration(var_name, node)
+ scopes[-1].add_declaration(var_name, node, 'arg')
@visitation.visit('push', tok.LEXICALSCOPE, tok.WITH)
def _push_scope(self, node):
Modified: trunk/javascriptlint/warnings.py
===================================================================
--- trunk/javascriptlint/warnings.py 2009-10-03 17:14:49 UTC (rev 256)
+++ trunk/javascriptlint/warnings.py 2009-10-03 17:39:40 UTC (rev 257)
@@ -75,7 +75,9 @@
'mismatch_ctrl_comments': 'mismatched control comment; "ignore" and "end" control comments must have a one-to-one correspondence',
'redeclared_var': 'redeclaration of {name}',
'undeclared_identifier': 'undeclared identifier: {name}',
- 'unreferenced_identifier': 'identifier is declared but never referenced: {name}',
+ 'unreferenced_argument': 'argument declared but never referenced: {name}',
+ 'unreferenced_function': 'function is declared but never referenced: {name}',
+ 'unreferenced_variable': 'variable is declared but never referenced: {name}',
'jsl_cc_not_understood': 'couldn\'t understand control comment using /*jsl:keyword*/ syntax',
'nested_comment': 'nested comment',
'legacy_cc_not_understood': 'couldn\'t understand control comment using /*@keyword@*/ syntax',
Modified: trunk/test.py
===================================================================
--- trunk/test.py 2009-10-03 17:14:49 UTC (rev 256)
+++ trunk/test.py 2009-10-03 17:39:40 UTC (rev 257)
@@ -10,7 +10,9 @@
_DEFAULT_CONF = """
# This warning triggers a lot of warnings in many of the tests, so only enable
# it when specifically testing it.
--unreferenced_identifier
+-unreferenced_argument
+-unreferenced_function
+-unreferenced_variable
"""
class TestError(Exception):
Modified: trunk/tests/warnings/unreferenced_identifier.js
===================================================================
--- trunk/tests/warnings/unreferenced_identifier.js 2009-10-03 17:14:49 UTC (rev 256)
+++ trunk/tests/warnings/unreferenced_identifier.js 2009-10-03 17:39:40 UTC (rev 257)
@@ -1,11 +1,13 @@
/* The tests disable this warning by default becaues of noise. Enable it. */
-/*conf:+unreferenced_identifier*/
+/*conf:+unreferenced_argument*/
+/*conf:+unreferenced_function*/
+/*conf:+unreferenced_variable*/
/* outer-level functions shouldn't warn */
var unreferenced_global;
function unreferenced_identifier() {
/* Test an unreferenced function. */
- function unreferenced_func() { /*warning:unreferenced_identifier*/
+ function unreferenced_func() { /*warning:unreferenced_function*/
return true;
}
function referenced_func() {
@@ -14,20 +16,20 @@
referenced_var();
/* Test an unreferenced parameter. */
- var z = new function(unreferenced_parm) { /*warning:unreferenced_identifier*/
+ var z = new function(unreferenced_parm) { /*warning:unreferenced_argument*/
};
z.prop = 42;
/* Test an unreferenced variable. */
- var unreferenced_variable = 100; /*warning:unreferenced_identifier*/
+ var unreferenced_variable = 100; /*warning:unreferenced_variable*/
/* An unreferenced duplicate parameter should give one warning. */
- function func_with_dup(unref_dup_parm, unref_dup_parm) { /*warning:unreferenced_identifier*/ /*warning:duplicate_formal*/
+ function func_with_dup(unref_dup_parm, unref_dup_parm) { /*warning:unreferenced_argument*/ /*warning:duplicate_formal*/
}
func_with_dup();
/* An unreferenced duplicate variable should give one warning. */
- var unref_dup_var; /*warning:unreferenced_identifier*/
+ var unref_dup_var; /*warning:unreferenced_variable*/
var unref_dup_var; /*warning:redeclared_var*/
/* Test a try/catch. The error doesn't need to be referenced. */
@@ -51,7 +53,7 @@
}
/* Test assignments. */
- var assigned_but_unref; /*warning:unreferenced_identifier*/
+ var assigned_but_unref; /*warning:unreferenced_variable*/
assigned_but_unref = 42;
function callback() {
@@ -60,9 +62,9 @@
(assigned_but_ref = callback)();
/* Test increment and decrement. */
- var unref_inc; /*warning:unreferenced_identifier*/
+ var unref_inc; /*warning:unreferenced_variable*/
unref_inc++;
- var unref_dec; /*warning:unreferenced_identifier*/
+ var unref_dec; /*warning:unreferenced_variable*/
unref_dec--;
var tmp;
@@ -73,7 +75,7 @@
tmp = -tmp;
/* Test named functions as references. */
- var fn = function ref_func() { return 42; }; /*warning:unreferenced_identifier*/
+ var fn = function ref_func() { return 42; }; /*warning:unreferenced_function*/
fn();
/* Test nested scopes. */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|