mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-08 13:45:01 +08:00
Add error code for return outside function.
This commit is contained in:
@@ -27,9 +27,7 @@ class Context(object):
|
|||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def add_context(self, node):
|
def add_context(self, node):
|
||||||
self.blocks.append(node)
|
|
||||||
yield Context(node, parent_context=self)
|
yield Context(node, parent_context=self)
|
||||||
self.blocks.pop()
|
|
||||||
|
|
||||||
|
|
||||||
class ErrorFinder(Normalizer):
|
class ErrorFinder(Normalizer):
|
||||||
@@ -42,7 +40,11 @@ class ErrorFinder(Normalizer):
|
|||||||
|
|
||||||
def initialize(self, node):
|
def initialize(self, node):
|
||||||
from parso.python.tree import search_ancestor
|
from parso.python.tree import search_ancestor
|
||||||
parent_scope = search_ancestor(node, 'classdef', 'funcdef', 'file_input')
|
allowed = 'classdef', 'funcdef', 'file_input'
|
||||||
|
if node.type in allowed:
|
||||||
|
parent_scope = node
|
||||||
|
else:
|
||||||
|
parent_scope = search_ancestor(node, allowed)
|
||||||
self._context = Context(parent_scope)
|
self._context = Context(parent_scope)
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
@@ -105,6 +107,9 @@ class ErrorFinder(Normalizer):
|
|||||||
in_loop = True
|
in_loop = True
|
||||||
if not in_loop:
|
if not in_loop:
|
||||||
self._add_syntax_error("'break' outside loop", leaf)
|
self._add_syntax_error("'break' outside loop", leaf)
|
||||||
|
elif leaf.value == 'return':
|
||||||
|
if self._context.node.type != 'funcdef':
|
||||||
|
self._add_syntax_error("'return' outside function", leaf)
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def _add_indentation_error(self, message, spacing):
|
def _add_indentation_error(self, message, spacing):
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ if (foo == bar and baz == frop):
|
|||||||
#: E101+2 E101+3
|
#: E101+2 E101+3
|
||||||
if start[1] > end_col and not (
|
if start[1] > end_col and not (
|
||||||
over_indent == 4 and indent_next):
|
over_indent == 4 and indent_next):
|
||||||
return (0, "E121 continuation line over-"
|
assert (0, "E121 continuation line over-"
|
||||||
"indented for visual indent")
|
"indented for visual indent")
|
||||||
|
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ if ((row < 0 or self.moduleCount <= row or
|
|||||||
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
|
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
|
||||||
#: E101+1 E101+2 E101+3 E101+4 E101+5 E101+6
|
#: E101+1 E101+2 E101+3 E101+4 E101+5 E101+6
|
||||||
if bar:
|
if bar:
|
||||||
return (
|
assert (
|
||||||
start, 'E121 lines starting with a '
|
start, 'E121 lines starting with a '
|
||||||
'closing bracket should be indented '
|
'closing bracket should be indented '
|
||||||
"to match that of the opening "
|
"to match that of the opening "
|
||||||
@@ -72,14 +72,14 @@ if ((foo.bar("baz") and
|
|||||||
hello("yes")
|
hello("yes")
|
||||||
#: E101+1
|
#: E101+1
|
||||||
if (a == 2 or b == "abc def ghi" "jkl mno"):
|
if (a == 2 or b == "abc def ghi" "jkl mno"):
|
||||||
return True
|
assert True
|
||||||
#: E101+2
|
#: E101+2
|
||||||
if (a == 2 or b == """abc def ghi
|
if (a == 2 or b == """abc def ghi
|
||||||
jkl mno"""):
|
jkl mno"""):
|
||||||
return True
|
assert True
|
||||||
#: E101+1 E101+2
|
#: E101+1 E101+2
|
||||||
if length > options.max_line_length:
|
if length > options.max_line_length:
|
||||||
return options.max_line_length, \
|
assert options.max_line_length, \
|
||||||
"E501 line too long (%d characters)" % length
|
"E501 line too long (%d characters)" % length
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ a = (123,
|
|||||||
|
|
||||||
if start[1] > end_col and not (
|
if start[1] > end_col and not (
|
||||||
over_indent == 4 and indent_next):
|
over_indent == 4 and indent_next):
|
||||||
return (0, "E121 continuation line over-"
|
assert (0, "E121 continuation line over-"
|
||||||
"indented for visual indent")
|
"indented for visual indent")
|
||||||
|
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ fooff(aaaa,
|
|||||||
"visual indentation is not a multiple of four",)
|
"visual indentation is not a multiple of four",)
|
||||||
|
|
||||||
if bar:
|
if bar:
|
||||||
return (
|
assert (
|
||||||
start, 'E121 lines starting with a '
|
start, 'E121 lines starting with a '
|
||||||
'closing bracket should be indented '
|
'closing bracket should be indented '
|
||||||
"to match that of the opening "
|
"to match that of the opening "
|
||||||
@@ -207,16 +207,16 @@ if ((foo.bar("baz") and
|
|||||||
if (a == 2 or
|
if (a == 2 or
|
||||||
b == "abc def ghi"
|
b == "abc def ghi"
|
||||||
"jkl mno"):
|
"jkl mno"):
|
||||||
return True
|
assert True
|
||||||
|
|
||||||
#: E129+1:4
|
#: E129+1:4
|
||||||
if (a == 2 or
|
if (a == 2 or
|
||||||
b == """abc def ghi
|
b == """abc def ghi
|
||||||
jkl mno"""):
|
jkl mno"""):
|
||||||
return True
|
assert True
|
||||||
|
|
||||||
if length > options.max_line_length:
|
if length > options.max_line_length:
|
||||||
return options.max_line_length, \
|
assert options.max_line_length, \
|
||||||
"E501 line too long (%d characters)" % length
|
"E501 line too long (%d characters)" % length
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ def qualify_by_address(
|
|||||||
if (a == 2 or
|
if (a == 2 or
|
||||||
b == "abc def ghi"
|
b == "abc def ghi"
|
||||||
"jkl mno"):
|
"jkl mno"):
|
||||||
return True
|
True
|
||||||
|
|
||||||
my_list = [
|
my_list = [
|
||||||
1, 2, 3,
|
1, 2, 3,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#: E701:6
|
#: E701:6
|
||||||
if a: a = False
|
if a: a = False
|
||||||
#: E701:41
|
#: E701:41
|
||||||
if not header or header[:6] != 'bytes=': return
|
if not header or header[:6] != 'bytes=': pass
|
||||||
#: E702:9
|
#: E702:9
|
||||||
a = False; b = True
|
a = False; b = True
|
||||||
#: E702:16 E402
|
#: E702:16 E402
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ def test_indentation_errors(code, positions):
|
|||||||
'''), # 'continue' not supported inside 'finally' clause"
|
'''), # 'continue' not supported inside 'finally' clause"
|
||||||
'continue',
|
'continue',
|
||||||
'break',
|
'break',
|
||||||
|
'return',
|
||||||
|
|
||||||
# IndentationError
|
# IndentationError
|
||||||
' foo',
|
' foo',
|
||||||
|
|||||||
Reference in New Issue
Block a user