Skip to content

Commit

Permalink
fix: migrate partial patterns to path
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Alla authored and browniebroke committed Jan 18, 2021
1 parent e20415e commit 202a6b3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
6 changes: 0 additions & 6 deletions django_codemod/visitors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def update_call_to_path(self, updated_node: Call):
self.check_not_simple_string(first_arg)
# Extract the URL pattern from the first argument
pattern = first_arg.value.evaluated_value
self.check_missing_start(pattern)
# If we reach this point, we might be able to use `path()`
call = self.build_path_call(pattern, other_args)
AddImportsVisitor.add_needed_import(
Expand All @@ -64,11 +63,6 @@ def check_not_simple_string(self, first_arg: Arg):
if not m.matches(first_arg, m.Arg(value=m.SimpleString())):
raise PatternNotSupported()

def check_missing_start(self, pattern):
"""Patterns that do not match the start of the string with caret."""
if not pattern.startswith("^"):
raise PatternNotSupported()

def build_path_call(self, pattern, other_args):
"""Build the `Call` node using Django 2.0's `path()` function."""
route = self.build_route(pattern)
Expand Down
8 changes: 4 additions & 4 deletions tests/visitors/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def test_simple_substitution(self) -> None:
"""
self.assertCodemod(before, after)

def test_starting_caret(self) -> None:
"""Patterns not starting with '^' are migrated to re_path."""
def test_no_starting_caret(self) -> None:
"""Patterns not starting with '^' are also migrated to path."""
before = """
from django.conf.urls import url
Expand All @@ -62,10 +62,10 @@ def test_starting_caret(self) -> None:
]
"""
after = """
from django.urls import re_path
from django.urls import path
urlpatterns = [
re_path(r'about/$', views.about, name='about'),
path('about/', views.about, name='about'),
]
"""
self.assertCodemod(before, after)
Expand Down

0 comments on commit 202a6b3

Please sign in to comment.