Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/libcst/_parser/conversions/module.py: 41%

17 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:43 +0000

1# Copyright (c) Meta Platforms, Inc. and affiliates. 

2# 

3# This source code is licensed under the MIT license found in the 

4# LICENSE file in the root directory of this source tree. 

5# pyre-unsafe 

6 

7from typing import Any, Sequence 

8 

9from libcst._nodes.module import Module 

10from libcst._nodes.whitespace import NEWLINE_RE 

11from libcst._parser.production_decorator import with_production 

12from libcst._parser.types.config import ParserConfig 

13 

14 

15@with_production("file_input", "(NEWLINE | stmt)* ENDMARKER") 

16def convert_file_input(config: ParserConfig, children: Sequence[Any]) -> Any: 

17 *body, footer = children 

18 if len(body) == 0: 

19 # If there's no body, the header and footer are ambiguous. The header is more 

20 # important, and should own the EmptyLine nodes instead of the footer. 

21 header = footer 

22 footer = () 

23 if ( 

24 len(config.lines) == 2 

25 and NEWLINE_RE.fullmatch(config.lines[0]) 

26 and config.lines[1] == "" 

27 ): 

28 # This is an empty file (not even a comment), so special-case this to an 

29 # empty list instead of a single dummy EmptyLine (which is what we'd 

30 # normally parse). 

31 header = () 

32 else: 

33 # Steal the leading lines from the first statement, and move them into the 

34 # header. 

35 first_stmt = body[0] 

36 header = first_stmt.leading_lines 

37 body[0] = first_stmt.with_changes(leading_lines=()) 

38 return Module( 

39 header=header, 

40 body=body, 

41 footer=footer, 

42 encoding=config.encoding, 

43 default_indent=config.default_indent, 

44 default_newline=config.default_newline, 

45 has_trailing_newline=config.has_trailing_newline, 

46 )