Skip to content

Commit 307549c

Browse files
committed
Correctly remove 'import foo.bar as bar'.
1 parent 60725d1 commit 307549c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pyimport.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ on line number LINE, remove VAR (e.g. 'baz')."
245245

246246
(cond
247247
;; If it's just 'import foo' or 'import foo.bar', just remove it.
248-
((looking-at (rx "import " (1+ (not (any space))) line-end))
248+
((looking-at (rx "import"
249+
(+ space)
250+
(+ (or (syntax word) (syntax symbol) (syntax punctuation)))
251+
(0+ space)
252+
(? "as" (+ space) (+ (or (syntax word) (syntax symbol))))
253+
(0+ space)
254+
line-end))
249255
(pyimport--delete-current-line))
250256

251257
;; Otherwise, it's '... import foo' or '... import foo as bar'

test/pyimport-test.el

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
(should
4141
(equal (buffer-string) ""))))
4242

43+
(ert-deftest remove-import-as-alias ()
44+
"Ensure we remove imports correctly when there are aliases."
45+
(with-temp-buffer
46+
(insert "import foo.bar as bar")
47+
(pyimport--remove-import 1 "bar")
48+
(should
49+
(equal (buffer-string) ""))))
50+
4351
(ert-deftest remove-on-line-first ()
4452
"We should remove the first occurrence, if present."
4553
(with-temp-buffer

0 commit comments

Comments
 (0)