[javascriptlint-commit] SF.net SVN: javascriptlint:[368] trunk
Status: Beta
Brought to you by:
matthiasmiller
From: <mat...@us...> - 2016-12-29 21:50:29
|
Revision: 368 http://sourceforge.net/p/javascriptlint/code/368 Author: matthiasmiller Date: 2016-12-29 21:50:27 +0000 (Thu, 29 Dec 2016) Log Message: ----------- Add warning against duplicate properties in object initializers Modified Paths: -------------- trunk/javascriptlint/lintwarnings.py trunk/tests/warnings/ambiguous_numeric_prop.js Added Paths: ----------- trunk/tests/warnings/duplicate_property.js Modified: trunk/javascriptlint/lintwarnings.py =================================================================== --- trunk/javascriptlint/lintwarnings.py 2016-12-29 21:46:51 UTC (rev 367) +++ trunk/javascriptlint/lintwarnings.py 2016-12-29 21:50:27 UTC (rev 368) @@ -105,6 +105,7 @@ 'trailing_whitespace': 'trailing whitespace', 'e4x_deprecated': 'e4x is deprecated', 'ambiguous_numeric_prop': 'numeric property should be normalized; use {normalized}', + 'duplicate_property': 'duplicate property in object initializer', } errors = { @@ -635,20 +636,47 @@ if not left.kind in (tok.VAR, tok.NAME): raise LintWarning(left) -@lookfor(tok.NUMBER) -def ambiguous_numeric_prop(node): +def _normalized_number(node): + assert node.kind == tok.NUMBER if node.atom.startswith('0x'): value = int(node.atom, 16) else: value = float(node.atom) if value.is_integer(): value = int(value) + return str(value) +@lookfor(tok.NUMBER) +def ambiguous_numeric_prop(node): + normalized = _normalized_number(node) if (node.node_index == 0 and node.parent.kind == tok.COLON) or \ (node.node_index == 1 and node.parent.kind == tok.LB): - if str(value) != node.atom: - raise LintWarning(node, normalized=str(value)) + if normalized != node.atom: + raise LintWarning(node, normalized=normalized) +def _object_property(node): + assert node.kind == tok.COLON + + left, right = node.kids + while left.kind == tok.RP: + left, = left.kids + if left.kind == tok.NUMBER: + return _normalized_number(left) + + assert left.kind in (tok.STRING, tok.NAME) + return left.atom + +@lookfor(tok.COLON) +def duplicate_property(node): + if not node.parent.kind == tok.RC: + return + + node_value = _object_property(node) + for sibling in node.parent.kids[:node.node_index]: + sibling_value = _object_property(sibling) + if node_value == sibling_value: + raise LintWarning(node) + @lookfor(tok.FUNCTION) def misplaced_function(node): # Ignore function statements. Modified: trunk/tests/warnings/ambiguous_numeric_prop.js =================================================================== --- trunk/tests/warnings/ambiguous_numeric_prop.js 2016-12-29 21:46:51 UTC (rev 367) +++ trunk/tests/warnings/ambiguous_numeric_prop.js 2016-12-29 21:50:27 UTC (rev 368) @@ -1,3 +1,4 @@ +/*conf:-duplicate_property*/ function ambiguous_numeric_prop() { var a = { 1: '', Added: trunk/tests/warnings/duplicate_property.js =================================================================== --- trunk/tests/warnings/duplicate_property.js (rev 0) +++ trunk/tests/warnings/duplicate_property.js 2016-12-29 21:50:27 UTC (rev 368) @@ -0,0 +1,13 @@ +/*conf:-useless_quotes*/ +/*conf:-ambiguous_numeric_prop*/ +function duplicate_property() { + var o = { + a: '', + "a": '', /*warning:duplicate_property*/ + 2.00: '', + 2: '', /*warning:duplicate_property*/ + 3.00: '', + '3': '', /*warning:duplicate_property*/ + 'other': '' + }; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |