lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for yapf.comment_splicer."""
 
import textwrap
import unittest
 
from yapf.yapflib import comment_splicer
from yapf.yapflib import py3compat
from yapf.yapflib import pytree_utils
 
 
class CommentSplicerTest(unittest.TestCase):
 
  def _AssertNodeType(self, expected_type, node):
    self.assertEqual(expected_type, pytree_utils.NodeName(node))
 
  def _AssertNodeIsComment(self, node, text_in_comment=None):
    if pytree_utils.NodeName(node) == 'simple_stmt':
      self._AssertNodeType('COMMENT', node.children[0])
      node_value = node.children[0].value
    else:
      self._AssertNodeType('COMMENT', node)
      node_value = node.value
    if text_in_comment is not None:
      self.assertIn(text_in_comment, node_value)
 
  def _FindNthChildNamed(self, node, name, n=1):
    for i, child in enumerate(
        py3compat.ifilter(lambda c: pytree_utils.NodeName(c) == name,
                          node.pre_order())):
      if i == n - 1:
        return child
    raise RuntimeError('No Nth child for n={0}'.format(n))
 
  def testSimpleInline(self):
    code = 'foo = 1 # and a comment\n'
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    expr = tree.children[0].children[0]
    # Check that the expected node is still expr_stmt, but now it has 4 children
    # (before comment splicing it had 3), the last child being the comment.
    self._AssertNodeType('expr_stmt', expr)
    self.assertEqual(4, len(expr.children))
    comment_node = expr.children[3]
    self._AssertNodeIsComment(comment_node, '# and a comment')
 
  def testSimpleSeparateLine(self):
    code = textwrap.dedent(r'''
      foo = 1
      # first comment
      bar = 2
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # The comment should've been added to the root's children (now 4, including
    # the ENDMARKER in the end.
    self.assertEqual(4, len(tree.children))
    comment_node = tree.children[1]
    self._AssertNodeIsComment(comment_node)
 
  def testTwoLineComment(self):
    code = textwrap.dedent(r'''
      foo = 1
      # first comment
      # second comment
      bar = 2
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # This is similar to the single-line standalone comment.
    self.assertEqual(4, len(tree.children))
    self._AssertNodeIsComment(tree.children[1])
 
  def testCommentIsFirstChildInCompound(self):
    code = textwrap.dedent(r'''
      if x:
        # a comment
        foo = 1
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # Look into the suite node under the 'if'. We don't care about the NEWLINE
    # leaf but the new COMMENT must be a child of the suite and before the
    # actual code leaf.
    if_suite = tree.children[0].children[3]
    self._AssertNodeType('NEWLINE', if_suite.children[0])
    self._AssertNodeIsComment(if_suite.children[1])
 
  def testCommentIsLastChildInCompound(self):
    code = textwrap.dedent(r'''
      if x:
        foo = 1
        # a comment
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # Look into the suite node under the 'if'. We don't care about the DEDENT
    # leaf but the new COMMENT must be a child of the suite and after the
    # actual code leaf.
    if_suite = tree.children[0].children[3]
    self._AssertNodeType('DEDENT', if_suite.children[-1])
    self._AssertNodeIsComment(if_suite.children[-2])
 
  def testInlineAfterSeparateLine(self):
    code = textwrap.dedent(r'''
      bar = 1
      # line comment
      foo = 1 # inline comment
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # The separate line comment should become a child of the root, while
    # the inline comment remains within its simple_node.
    sep_comment_node = tree.children[1]
    self._AssertNodeIsComment(sep_comment_node, '# line comment')
 
    expr = tree.children[2].children[0]
    inline_comment_node = expr.children[-1]
    self._AssertNodeIsComment(inline_comment_node, '# inline comment')
 
  def testSeparateLineAfterInline(self):
    code = textwrap.dedent(r'''
      bar = 1
      foo = 1 # inline comment
      # line comment
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # The separate line comment should become a child of the root, while
    # the inline comment remains within its simple_node.
    sep_comment_node = tree.children[-2]
    self._AssertNodeIsComment(sep_comment_node, '# line comment')
 
    expr = tree.children[1].children[0]
    inline_comment_node = expr.children[-1]
    self._AssertNodeIsComment(inline_comment_node, '# inline comment')
 
  def testCommentBeforeDedent(self):
    code = textwrap.dedent(r'''
      if bar:
        z = 1
      # a comment
      j = 2
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # The comment should go under the tree root, not under the 'if'.
    self._AssertNodeIsComment(tree.children[1])
    if_suite = tree.children[0].children[3]
    self._AssertNodeType('DEDENT', if_suite.children[-1])
 
  def testCommentBeforeDedentTwoLevel(self):
    code = textwrap.dedent(r'''
      if foo:
        if bar:
          z = 1
        # a comment
      y = 1
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    if_suite = tree.children[0].children[3]
    # The comment is in the first if_suite, not the nested if under it. It's
    # right before the DEDENT
    self._AssertNodeIsComment(if_suite.children[-2])
    self._AssertNodeType('DEDENT', if_suite.children[-1])
 
  def testCommentBeforeDedentTwoLevelImproperlyIndented(self):
    code = textwrap.dedent(r'''
      if foo:
        if bar:
          z = 1
         # comment 2
      y = 1
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # The comment here is indented by 3 spaces, which is unlike any of the
    # surrounding statement indentation levels. The splicer attaches it to the
    # "closest" parent with smaller indentation.
    if_suite = tree.children[0].children[3]
    # The comment is in the first if_suite, not the nested if under it. It's
    # right before the DEDENT
    self._AssertNodeIsComment(if_suite.children[-2])
    self._AssertNodeType('DEDENT', if_suite.children[-1])
 
  def testCommentBeforeDedentThreeLevel(self):
    code = textwrap.dedent(r'''
      if foo:
        if bar:
          z = 1
          # comment 2
        # comment 1
      # comment 0
      j = 2
      ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    # comment 0 should go under the tree root
    self._AssertNodeIsComment(tree.children[1], '# comment 0')
 
    # comment 1 is in the first if_suite, right before the DEDENT
    if_suite_1 = self._FindNthChildNamed(tree, 'suite', n=1)
    self._AssertNodeIsComment(if_suite_1.children[-2], '# comment 1')
    self._AssertNodeType('DEDENT', if_suite_1.children[-1])
 
    # comment 2 is in if_suite nested under the first if suite,
    # right before the DEDENT
    if_suite_2 = self._FindNthChildNamed(tree, 'suite', n=2)
    self._AssertNodeIsComment(if_suite_2.children[-2], '# comment 2')
    self._AssertNodeType('DEDENT', if_suite_2.children[-1])
 
  def testCommentsInClass(self):
    code = textwrap.dedent(r'''
      class Foo:
        """docstring abc..."""
        # top-level comment
        def foo(): pass
        # another comment
      ''')
 
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    class_suite = tree.children[0].children[3]
    another_comment = class_suite.children[-2]
    self._AssertNodeIsComment(another_comment, '# another')
 
    # It's OK for the comment to be a child of funcdef, as long as it's
    # the first child and thus comes before the 'def'.
    funcdef = class_suite.children[3]
    toplevel_comment = funcdef.children[0]
    self._AssertNodeIsComment(toplevel_comment, '# top-level')
 
  def testMultipleBlockComments(self):
    code = textwrap.dedent(r'''
        # Block comment number 1
 
        # Block comment number 2
        def f():
          pass
        ''')
 
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    funcdef = tree.children[0]
    block_comment_1 = funcdef.children[0]
    self._AssertNodeIsComment(block_comment_1, '# Block comment number 1')
 
    block_comment_2 = funcdef.children[1]
    self._AssertNodeIsComment(block_comment_2, '# Block comment number 2')
 
  def testCommentsOnDedents(self):
    code = textwrap.dedent(r'''
        class Foo(object):
          # A comment for qux.
          def qux(self):
            pass
 
          # Interim comment.
 
          def mux(self):
            pass
        ''')
 
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    classdef = tree.children[0]
    class_suite = classdef.children[6]
    qux_comment = class_suite.children[1]
    self._AssertNodeIsComment(qux_comment, '# A comment for qux.')
 
    interim_comment = class_suite.children[4]
    self._AssertNodeIsComment(interim_comment, '# Interim comment.')
 
  def testExprComments(self):
    code = textwrap.dedent(r'''
      foo( # Request fractions of an hour.
        948.0/3600, 20)
    ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    trailer = self._FindNthChildNamed(tree, 'trailer', 1)
    comment = trailer.children[1]
    self._AssertNodeIsComment(comment, '# Request fractions of an hour.')
 
  def testMultipleCommentsInOneExpr(self):
    code = textwrap.dedent(r'''
      foo( # com 1
        948.0/3600, # com 2
        20 + 12 # com 3
        )
    ''')
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
 
    trailer = self._FindNthChildNamed(tree, 'trailer', 1)
    self._AssertNodeIsComment(trailer.children[1], '# com 1')
 
    arglist = self._FindNthChildNamed(tree, 'arglist', 1)
    self._AssertNodeIsComment(arglist.children[2], '# com 2')
 
    arith_expr = self._FindNthChildNamed(tree, 'arith_expr', 1)
    self._AssertNodeIsComment(arith_expr.children[-1], '# com 3')
 
 
if __name__ == '__main__':
  unittest.main()