[javascriptlint-commit] SF.net SVN: javascriptlint:[348] trunk
Status: Beta
Brought to you by:
matthiasmiller
|
From: <mat...@us...> - 2014-01-08 22:59:05
|
Revision: 348
http://sourceforge.net/p/javascriptlint/code/348
Author: matthiasmiller
Date: 2014-01-08 22:59:01 +0000 (Wed, 08 Jan 2014)
Log Message:
-----------
Allow the function_name warnings to require name decoration.
Modified Paths:
--------------
trunk/javascriptlint/conf.py
trunk/javascriptlint/lint.py
trunk/javascriptlint/warnings.py
trunk/tests/warnings/function_name_mismatch.js
trunk/tests/warnings/function_name_missing.js
Added Paths:
-----------
trunk/tests/warnings/function_name_mismatch_decorated.js
Modified: trunk/javascriptlint/conf.py
===================================================================
--- trunk/javascriptlint/conf.py 2013-12-07 16:32:54 UTC (rev 347)
+++ trunk/javascriptlint/conf.py 2014-01-08 22:59:01 UTC (rev 348)
@@ -94,6 +94,12 @@
#+default-type text/javascript;version=1.5
#+default-type text/javascript;e4x=1
+###
+# Some browsers pollute the namespace when using the "function_name_missing"
+# or "function_name_mismatch" warning. Enable this option to require a
+# double-underscore prefix.
+#+decorate_function_name_warning
+
### Files
# Specify which files to lint
# Use "+recurse" to enable recursion (disabled by default).
@@ -186,7 +192,8 @@
# SpiderMonkey warnings
'no_return_value': BooleanSetting(True),
'equal_as_assign': BooleanSetting(True),
- 'anon_no_return_value': BooleanSetting(True)
+ 'anon_no_return_value': BooleanSetting(True),
+ 'decorate_function_name_warning': BooleanSetting(False),
}
for name in warnings.warnings:
self._settings[name] = BooleanSetting(True)
Modified: trunk/javascriptlint/lint.py
===================================================================
--- trunk/javascriptlint/lint.py 2013-12-07 16:32:54 UTC (rev 347)
+++ trunk/javascriptlint/lint.py 2014-01-08 22:59:01 UTC (rev 348)
@@ -482,7 +482,7 @@
# Find all visitors and convert them into "onpush" callbacks that call "report"
visitors = {
- 'push': warnings.make_visitors()
+ 'push': warnings.make_visitors(conf)
}
for event in visitors:
for kind, callbacks in visitors[event].items():
Modified: trunk/javascriptlint/warnings.py
===================================================================
--- trunk/javascriptlint/warnings.py 2013-12-07 16:32:54 UTC (rev 347)
+++ trunk/javascriptlint/warnings.py 2014-01-08 22:59:01 UTC (rev 348)
@@ -139,6 +139,20 @@
_visitors.append((arg, fn))
return decorate
+_visitor_classes = []
+def lookfor_class(*args):
+ def decorate(cls):
+ # Convert the class name to camel case
+ camelcase = re.sub('([A-Z])', r'_\1', cls.__name__).lower().lstrip('_')
+
+ cls.warning = camelcase.rstrip('_')
+ assert cls.warning in warnings, \
+ 'Missing warning description: %s' % cls.warning
+
+ for arg in args:
+ _visitor_classes.append((arg, cls))
+ return decorate
+
class LintWarning(Exception):
def __init__(self, node, **errargs):
self.node = node
@@ -638,7 +652,7 @@
return # Allow as constructors
raise LintWarning(node)
-def _get_expected_function_name(node):
+def _get_function_property_name(node):
# Ignore function statements.
if node.opcode in (None, op.CLOSURE):
return
@@ -671,27 +685,42 @@
if parent.kind == tok.COLON and parent.parent.kind == tok.RC:
return parent.kids[0].atom
-@lookfor(tok.FUNCTION)
-def function_name_missing(node):
- if node.fn_name:
- return
+def _get_expected_function_name(node, decorate):
+ name = _get_function_property_name(node)
+ if name and decorate:
+ return '__%s' % name
+ return name
- expected_name = _get_expected_function_name(node)
- if not expected_name is None:
- raise LintWarning(node, name=expected_name)
+@lookfor_class(tok.FUNCTION)
+class FunctionNameMissing(object):
+ def __init__(self, conf):
+ self._decorate = conf['decorate_function_name_warning']
-@lookfor(tok.FUNCTION)
-def function_name_mismatch(node):
- if not node.fn_name:
- return
+ def __call__(self, node):
+ if node.fn_name:
+ return
- expected_name = _get_expected_function_name(node)
- if expected_name is None:
- return
+ expected_name = _get_expected_function_name(node, self._decorate)
+ if not expected_name is None:
+ raise LintWarning(node, name=expected_name)
- if expected_name != node.fn_name:
- raise LintWarning(node, fn_name=node.fn_name, prop_name=expected_name)
+@lookfor_class(tok.FUNCTION)
+class FunctionNameMismatch(object):
+ def __init__(self, conf):
+ self._decorate = conf['decorate_function_name_warning']
+ def __call__(self, node):
+ if not node.fn_name:
+ return
+
+ expected_name = _get_expected_function_name(node, self._decorate)
+ if expected_name is None:
+ return
+
+ if expected_name != node.fn_name:
+ raise LintWarning(node, fn_name=node.fn_name,
+ prop_name=expected_name)
+
@lookfor()
def mismatch_ctrl_comments(node):
pass
@@ -755,9 +784,13 @@
def dup_option_explicit(node):
pass
-def make_visitors():
+def make_visitors(conf):
+ all_visitors = list(_visitors)
+ for kind, klass in _visitor_classes:
+ all_visitors.append((kind, klass(conf=conf)))
+
visitors = {}
- for kind, func in _visitors:
+ for kind, func in all_visitors:
try:
visitors[kind].append(func)
except KeyError:
Modified: trunk/tests/warnings/function_name_mismatch.js
===================================================================
--- trunk/tests/warnings/function_name_mismatch.js 2013-12-07 16:32:54 UTC (rev 347)
+++ trunk/tests/warnings/function_name_mismatch.js 2014-01-08 22:59:01 UTC (rev 348)
@@ -34,6 +34,12 @@
}
function Class() {
+ this.getStr = function getSt() { /*warning:function_name_mismatch*/
+ return this.str;
+ };
+ this.setStr = function setStr(s) {
+ this.str = s;
+ };
}
Class.prototype.get = function gt() { /*warning:function_name_mismatch*/
return this.value;
Copied: trunk/tests/warnings/function_name_mismatch_decorated.js (from rev 347, trunk/tests/warnings/function_name_mismatch.js)
===================================================================
--- trunk/tests/warnings/function_name_mismatch_decorated.js (rev 0)
+++ trunk/tests/warnings/function_name_mismatch_decorated.js 2014-01-08 22:59:01 UTC (rev 348)
@@ -0,0 +1,32 @@
+/*conf:+function_name_mismatch*/
+/*conf:+decorate_function_name_warning*/
+function function_name_mismatch_decorated() {
+ var f = function bogus() { /*warning:function_name_mismatch*/
+ };
+ var g = function g() { /*warning:function_name_mismatch*/
+ };
+ var h = function __h() {
+ };
+
+ f = new function bogus() {
+ };
+ f = new function() {
+ };
+
+ f = (function() {
+ return 10;
+ })();
+
+ function Class() {
+ }
+ Class.prototype.get = function gt() { /*warning:function_name_mismatch*/
+ return this.value;
+ };
+ Class.prototype.set = function set(value) { /*warning:function_name_mismatch*/
+ this.value = value;
+ };
+ Class.prototype.inc = function __inc(value) {
+ this.value++;
+ };
+}
+
Modified: trunk/tests/warnings/function_name_missing.js
===================================================================
--- trunk/tests/warnings/function_name_missing.js 2013-12-07 16:32:54 UTC (rev 347)
+++ trunk/tests/warnings/function_name_missing.js 2014-01-08 22:59:01 UTC (rev 348)
@@ -28,6 +28,12 @@
}
function Class() {
+ this.getStr = function () { /*warning:function_name_missing*/
+ return this.str;
+ };
+ this.setStr = function setStr(s) {
+ this.str = s;
+ };
}
Class.prototype.get = function () { /*warning:function_name_missing*/
return this.value;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|