Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- #895 patchedast Starred and keyword now consumes their syntactically expected and ** (@lieryan)
- #896 patchedast cleanup and refactoring (@lieryan)
- #905 Ignore `node_modules` directories by default when scanning project files and source folders. (@marcoeangeli)
- #926, #929 Raise RefactoringError instead of IndexError/ModuleSyntaxError when extracting an empty or whitespace-only region (@aliassar)
- #933 Add type annotations to project.py (@lieryan)
- #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl)
- #921 Patch the else suite of try statements that also have a finally (@marlon-costa-dc)
Expand Down
4 changes: 4 additions & 0 deletions rope/refactor/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ def __call__(self, info):
self.multi_line_conditions(info)

def base_conditions(self, info):
if not info.extracted.strip():
raise RefactoringError(
"Extracted piece should contain at least one expression or statement."
)
if info.region[1] > info.scope_region[1]:
raise RefactoringError("Bad region selected for extract method")

Expand Down
30 changes: 30 additions & 0 deletions ropetest/refactor/extracttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,36 @@ class AClass(object):
with self.assertRaises(rope.base.exceptions.RefactoringError):
self.do_extract_method(code, start, end, "new_func")

def test_extract_method_empty_region(self):
code = dedent("""\
def a_func(arg):
var = arg + 1
return var
""")
start = code.index("arg + 1")
with self.assertRaises(rope.base.exceptions.RefactoringError):
self.do_extract_method(code, start, start, "new_func")

def test_extract_method_whitespace_only_region(self):
code = dedent("""\
def a_func(arg):
var = arg + 1
return var
""")
start = code.index(" var")
with self.assertRaises(rope.base.exceptions.RefactoringError):
self.do_extract_method(code, start, start + 4, "new_func")

def test_extract_variable_empty_region(self):
code = dedent("""\
def a_func(arg):
var = arg + 1
return var
""")
start = code.index("arg + 1")
with self.assertRaises(rope.base.exceptions.RefactoringError):
self.do_extract_variable(code, start, start, "new_var")

def test_extract_method_containing_return(self):
code = dedent("""\
def a_func(arg):
Expand Down
Loading