Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
A
add-trailing-comma
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
add-trailing-comma
Commits
48b79ddd
Kaydet (Commit)
48b79ddd
authored
Tem 17, 2017
tarafından
Anthony Sottile
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Special case handling tuples
üst
c7da498e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
15 deletions
+31
-15
add_trailing_comma.py
add_trailing_comma.py
+19
-15
add_trailing_comma_test.py
tests/add_trailing_comma_test.py
+12
-0
No files found.
add_trailing_comma.py
Dosyayı görüntüle @
48b79ddd
...
...
@@ -63,11 +63,12 @@ class FindNodes(ast.NodeVisitor):
self
.
calls
=
collections
.
defaultdict
(
list
)
self
.
funcs
=
{}
self
.
literals
=
{}
self
.
tuples
=
{}
def
_visit_literal
(
self
,
node
,
key
=
'elts'
,
**
kwargs
):
def
_visit_literal
(
self
,
node
,
key
=
'elts'
):
if
getattr
(
node
,
key
):
key
=
Offset
(
node
.
lineno
,
node
.
col_offset
)
self
.
literals
[
key
]
=
Literal
(
node
,
**
kwargs
)
self
.
literals
[
key
]
=
Literal
(
node
)
self
.
generic_visit
(
node
)
visit_Set
=
visit_List
=
_visit_literal
...
...
@@ -76,8 +77,11 @@ class FindNodes(ast.NodeVisitor):
self
.
_visit_literal
(
node
,
key
=
'values'
)
def
visit_Tuple
(
self
,
node
):
# tuples lie about things so we tell the later machiner to backtrack
self
.
_visit_literal
(
node
,
backtrack
=
True
)
if
node
.
elts
:
key
=
Offset
(
node
.
lineno
,
node
.
col_offset
)
# tuples lie about offset -- tell the later machinery to backtrack
self
.
tuples
[
key
]
=
Literal
(
node
,
backtrack
=
True
)
self
.
generic_visit
(
node
)
def
visit_Call
(
self
,
node
):
argnodes
=
node
.
args
+
node
.
keywords
...
...
@@ -200,16 +204,15 @@ def _find_call(call, i, tokens):
return
_find_simple
(
first_brace
,
tokens
)
def
_find_
literal
(
literal
,
i
,
tokens
):
def
_find_
tuple
(
i
,
tokens
):
# tuples are evil, we need to backtrack to find the opening paren
if
literal
.
backtrack
:
i
-=
1
while
tokens
[
i
]
.
name
in
NON_CODING_TOKENS
:
i
-=
1
while
tokens
[
i
]
.
name
in
NON_CODING_TOKENS
:
i
-=
1
# Sometimes tuples don't even have a paren!
# x = 1, 2, 3
if
tokens
[
i
]
.
src
!=
'('
:
return
# Sometimes tuples don't even have a paren!
# x = 1, 2, 3
if
tokens
[
i
]
.
src
!=
'('
:
return
return
_find_simple
(
i
,
tokens
)
...
...
@@ -326,13 +329,14 @@ def _fix_src(contents_text, py35_plus):
# Handle parenthesized things
elif
token
.
src
==
'('
:
fixes
.
append
((
False
,
_find_simple
(
i
,
tokens
)))
elif
key
in
visitor
.
literals
:
fixes
.
append
((
True
,
_find_simple
(
i
,
tokens
)))
# need to additionally handle literals afterwards as tuples report
# their starting index as the first element, which may be one of the
# above things.
if
key
in
visitor
.
literals
:
fix_data
=
_find_literal
(
visitor
.
literals
[
key
],
i
,
tokens
)
fixes
.
append
((
True
,
fix_data
))
if
key
in
visitor
.
tuples
:
fixes
.
append
((
True
,
_find_tuple
(
i
,
tokens
)))
for
add_comma
,
fix_data
in
fixes
:
if
fix_data
is
not
None
:
...
...
tests/add_trailing_comma_test.py
Dosyayı görüntüle @
48b79ddd
...
...
@@ -124,6 +124,18 @@ def test_py35_plus_rewrite():
' x,
\n
'
')'
,
),
# Regression test for #23
(
'(
\n
'
' {k: v},
\n
'
' ()
\n
'
')'
,
'(
\n
'
' {k: v},
\n
'
' (),
\n
'
')'
,
),
),
)
def
test_fixes_calls
(
src
,
expected
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment