[javascriptlint-commit] SF.net SVN: javascriptlint:[352] trunk
Status: Beta
Brought to you by:
matthiasmiller
From: <mat...@us...> - 2014-10-27 19:43:07
|
Revision: 352 http://sourceforge.net/p/javascriptlint/code/352 Author: matthiasmiller Date: 2014-10-27 19:42:54 +0000 (Mon, 27 Oct 2014) Log Message: ----------- Add trailing_whitespace warning. Modified Paths: -------------- trunk/javascriptlint/conf.py trunk/javascriptlint/jsparse.py trunk/javascriptlint/lint.py trunk/javascriptlint/warnings.py trunk/jsengine/parser/_constants_kind.py Modified: trunk/javascriptlint/conf.py =================================================================== --- trunk/javascriptlint/conf.py 2014-10-27 19:11:53 UTC (rev 351) +++ trunk/javascriptlint/conf.py 2014-10-27 19:42:54 UTC (rev 352) @@ -11,6 +11,7 @@ 'block_without_braces', 'function_name_missing', 'function_name_mismatch', + 'trailing_whitespace', ) def _getwarningsconf(): Modified: trunk/javascriptlint/jsparse.py =================================================================== --- trunk/javascriptlint/jsparse.py 2014-10-27 19:11:53 UTC (rev 351) +++ trunk/javascriptlint/jsparse.py 2014-10-27 19:42:54 UTC (rev 352) @@ -87,6 +87,20 @@ possible_comments = findpossiblecomments(script, start_offset) return filtercomments(possible_comments, root_node) +def find_trailing_whitespace(script, script_offset): + nodes = [] + + trailing_whitespace = re.compile(r'\S(?P<whitespace>[^\S\r\n]+)([\r\n]|$)') + + for match in trailing_whitespace.finditer(script): + start = match.start('whitespace') + end = match.end('whitespace') + nodes.append(ParseNode(kind.WHITESPACE, None, + script_offset + start, + script_offset + end-1, + script[start:end], [])) + return nodes + def is_compilable_unit(script, jsversion): jsversion = jsversion or JSVersion.default() assert isvalidversion(jsversion) @@ -262,7 +276,22 @@ testcomment('%s' % comment, 7, 7) testcomment(' %s' % comment, 7, 8) testcomment('\n\n %s' % comment, 7, 10) + def testTrailingWhitespace(self): + def testwhitespace(text, expected_whitespace): + nodes = find_trailing_whitespace(text, 0) + if expected_whitespace: + node, = nodes + self.assertEquals(node.atom, expected_whitespace) + else: + self.assertEquals(nodes, []) + testwhitespace(' ', '') + testwhitespace(' \n', '') + testwhitespace('a \n', ' ') + testwhitespace('a\n ', '') + testwhitespace('a\n {} ', ' ') + testwhitespace('a\n {} \n', ' ') + if __name__ == '__main__': unittest.main() Modified: trunk/javascriptlint/lint.py =================================================================== --- trunk/javascriptlint/lint.py 2014-10-27 19:11:53 UTC (rev 351) +++ trunk/javascriptlint/lint.py 2014-10-27 19:42:54 UTC (rev 352) @@ -511,6 +511,9 @@ unused_scope = script_cache.scope.find_scope(node) unused_scope.set_unused(name, node) + for node in jsparse.find_trailing_whitespace(script, script_offset): + report(node, 'trailing_whitespace') + def _lint_script_parts(script_parts, script_cache, lint_error, conf, import_callback): def report_lint(node, errname, offset=0, **errargs): errdesc = warnings.format_error(errname, **errargs) Modified: trunk/javascriptlint/warnings.py =================================================================== --- trunk/javascriptlint/warnings.py 2014-10-27 19:11:53 UTC (rev 351) +++ trunk/javascriptlint/warnings.py 2014-10-27 19:42:54 UTC (rev 352) @@ -105,6 +105,7 @@ 'misplaced_function': 'unconventional use of function expression', 'function_name_missing': 'anonymous function should be named to match property name {name}', 'function_name_mismatch': 'function name {fn_name} does not match property name {prop_name}', + 'trailing_whitespace': 'trailing whitespace', } errors = { Modified: trunk/jsengine/parser/_constants_kind.py =================================================================== --- trunk/jsengine/parser/_constants_kind.py 2014-10-27 19:11:53 UTC (rev 351) +++ trunk/jsengine/parser/_constants_kind.py 2014-10-27 19:42:54 UTC (rev 352) @@ -64,6 +64,7 @@ 'RB', 'STRING', 'YIELD', # TODO + 'WHITESPACE', ] class _Kind(object): def __init__(self, name): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |