Skip to content

Commit 2fed8a4

Browse files
committed
Correcting the 'Before' stacking effect
Adding consideration for negative offset in graph_access.py
1 parent 3d1050c commit 2fed8a4

File tree

3 files changed

+479
-18
lines changed

3 files changed

+479
-18
lines changed

refactor/actions.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
__all__ = [
1818
"BaseAction",
1919
"InsertAfter",
20+
"InsertBefore",
2021
"LazyInsertAfter",
22+
"LazyInsertBefore",
2123
"LazyReplace",
2224
"Replace",
2325
"Erase",
@@ -195,7 +197,6 @@ def _stack_effect(self) -> tuple[ast.AST, int]:
195197
return (self.node, 1)
196198

197199

198-
@_hint("deprecated_alias", "NewStatementAction")
199200
@dataclass
200201
class LazyInsertBefore(_LazyActionMixin[ast.stmt, ast.stmt]):
201202
"""Inserts the re-synthesized version :py:meth:`LazyInsertBefore.build`'s
@@ -217,25 +218,18 @@ def apply(self, context: Context, source: str) -> str:
217218

218219
replacement = split_lines(context.unparse(self.build()))
219220
replacement.apply_indentation(indentation, start_prefix=start_prefix)
221+
replacement[-1] += lines._newline_type
220222

221223
original_node_start = cast(int, self.node.lineno)
222-
if lines[original_node_start].endswith(lines._newline_type):
223-
replacement[-1] += lines._newline_type
224-
else:
225-
# If the original anchor's last line doesn't end with a newline,
226-
# then we need to also prevent our new source from ending with
227-
# a newline.
228-
replacement[0] = lines._newline_type + replacement[0]
229-
230224
for line in reversed(replacement):
231225
lines.insert(original_node_start - 1, line)
232226

233227
return lines.join()
234228

235229
def _stack_effect(self) -> tuple[ast.AST, int]:
236-
# Adding a statement right after the node will need to be reflected
230+
# Adding a statement right before the node will need to be reflected
237231
# in the block.
238-
return (self.node, 1)
232+
return (self.node, -1)
239233

240234

241235
@dataclass
@@ -264,7 +258,6 @@ def build(self) -> ast.stmt:
264258
return self.target
265259

266260

267-
@_hint("deprecated_alias", "TargetedNewStatementBeforeAction")
268261
@dataclass
269262
class InsertBefore(LazyInsertBefore):
270263
"""Inserts the re-synthesized version of given `target` right after
@@ -341,7 +334,7 @@ def _resynthesize(self, context: Context) -> str:
341334
return ""
342335

343336
def _stack_effect(self) -> tuple[ast.AST, int]:
344-
# Erasing a single node mean positions of all the followinng statements will
337+
# Erasing a single node mean positions of all the following statements will
345338
# need to reduced by 1.
346339
return (self.node, -1)
347340

refactor/internal/graph_access.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ def shift(self, shifts: list[tuple[GraphPath, int]]) -> GraphPath:
135135
assert isinstance(target_access, IndexAccess)
136136

137137
# This change might affect the future nodes in this path
138-
# but not us.
139-
if shifter.index >= target_access.index:
138+
# When the requested shift_offset is negative, it does affect us
139+
# by the amount of offset
140+
if shifter.index + shift_offset >= target_access.index:
140141
continue
141142

142143
parts[parts.index(target_access)] = target_access.replace(
143-
index=target_access.index + shift_offset
144+
index=target_access.index + abs(shift_offset)
144145
)
145-
146146
return GraphPath(parts)
147147

148148
def execute(self, node: ast.AST) -> ast.AST:

0 commit comments

Comments
 (0)