1
+ import re
1
2
from .Slice import Slice
2
3
from .Literals import Language , LiteralFormat , LiteralValue
3
4
from .EscapeLeadingWhitespace import EscapeLeadingWhitespace
@@ -9,15 +10,15 @@ class SourceFile:
9
10
10
11
def __init__ (self , filename : str , content : str ) -> None :
11
12
self .__unix_newlines : bool = "\r " not in content
12
- self .__content_slice : Slice = Slice (content .replace ("\r \n " , "\n " ))
13
+ self ._content_slice : Slice = Slice (content .replace ("\r \n " , "\n " ))
13
14
self .__language : Language = Language .from_filename (filename )
14
15
self .__escape_leading_whitespace = EscapeLeadingWhitespace .appropriate_for (
15
- self .__content_slice .__str__ ()
16
+ self ._content_slice .__str__ ()
16
17
)
17
18
18
19
def remove_selfie_once_comments (self ):
19
20
# Split content into lines
20
- lines = self .__content_slice .__str__ ().split ("\n " )
21
+ lines = self ._content_slice .__str__ ().split ("\n " )
21
22
22
23
# Create a new list of lines, excluding lines containing '# selfieonce' or '#selfieonce'
23
24
new_lines = []
@@ -39,28 +40,30 @@ def remove_selfie_once_comments(self):
39
40
new_content = "\n " .join (new_lines )
40
41
41
42
# Update the content slice with new content
42
- self .__content_slice = Slice (new_content )
43
+ self ._content_slice = Slice (new_content )
43
44
44
45
if not self .__unix_newlines :
45
- self .__content_slice = Slice (new_content .replace ("\n " , "\r \n " ))
46
+ self ._content_slice = Slice (new_content .replace ("\n " , "\r \n " ))
46
47
47
48
@property
48
49
def as_string (self ) -> str :
49
50
return (
50
- self .__content_slice .__str__ ()
51
+ self ._content_slice .__str__ ()
51
52
if self .__unix_newlines
52
- else self .__content_slice .__str__ ().replace ("\n " , "\r \n " )
53
+ else self ._content_slice .__str__ ().replace ("\n " , "\r \n " )
53
54
)
54
55
55
56
class ToBeLiteral :
56
57
def __init__ (
57
58
self ,
59
+ parent : "SourceFile" ,
58
60
dot_fun_open_paren : str ,
59
61
function_call_plus_arg : Slice ,
60
62
arg : Slice ,
61
63
language : Language ,
62
64
escape_leading_whitespace : EscapeLeadingWhitespace ,
63
65
) -> None :
66
+ self .__parent = parent
64
67
self .__dot_fun_open_paren = dot_fun_open_paren
65
68
self .__function_call_plus_arg = function_call_plus_arg
66
69
self .__arg = arg
@@ -92,16 +95,19 @@ def set_literal_and_get_newline_delta(self, literal_value: LiteralValue) -> int:
92
95
)
93
96
existing_newlines = self .__function_call_plus_arg .count ("\n " )
94
97
new_newlines = encoded .count ("\n " )
95
- self .__content_slice = self .__function_call_plus_arg .replaceSelfWith (
96
- f"{ self .__dot_fun_open_paren } { encoded } )"
98
+ self .__parent ._content_slice = Slice (
99
+ self .__function_call_plus_arg .replaceSelfWith (
100
+ f"{ self .__dot_fun_open_paren } { encoded } )"
101
+ )
97
102
)
103
+
98
104
return new_newlines - existing_newlines
99
105
100
106
def parse_literal (self , literal_format : LiteralFormat ) -> Any :
101
107
return literal_format .parse (self .__arg .__str__ (), self .__language )
102
108
103
109
def find_on_line (self , to_find : str , line_one_indexed : int ) -> Slice :
104
- line_content = self .__content_slice .unixLine (line_one_indexed )
110
+ line_content = self ._content_slice .unixLine (line_one_indexed )
105
111
idx = line_content .indexOf (to_find )
106
112
if idx == - 1 :
107
113
raise AssertionError (
@@ -115,12 +121,12 @@ def find_on_line(self, to_find: str, line_one_indexed: int) -> Slice:
115
121
def replace_on_line (self , line_one_indexed : int , find : str , replace : str ) -> None :
116
122
assert "\n " not in find
117
123
assert "\n " not in replace
118
- line_content = self .__content_slice .unixLine (line_one_indexed ).__str__ ()
124
+ line_content = self ._content_slice .unixLine (line_one_indexed ).__str__ ()
119
125
new_content = line_content .replace (find , replace )
120
- self .__content_slice = Slice (self .__content_slice .replaceSelfWith (new_content ))
126
+ self ._content_slice = Slice (self ._content_slice .replaceSelfWith (new_content ))
121
127
122
128
def parse_to_be_like (self , line_one_indexed : int ) -> ToBeLiteral :
123
- line_content = self .__content_slice .unixLine (line_one_indexed )
129
+ line_content = self ._content_slice .unixLine (line_one_indexed )
124
130
dot_fun_open_paren = None
125
131
126
132
for to_be_like in TO_BE_LIKES :
@@ -137,24 +143,24 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
137
143
dot_function_call = dot_function_call_in_place + line_content .startIndex
138
144
arg_start = dot_function_call + len (dot_fun_open_paren )
139
145
140
- if self .__content_slice .__len__ () == arg_start :
146
+ if self ._content_slice .__len__ () == arg_start :
141
147
raise AssertionError (
142
148
f"Appears to be an unclosed function call `{ dot_fun_open_paren } ` "
143
149
f"on line { line_one_indexed } "
144
150
)
145
- while self .__content_slice [arg_start ].isspace ():
151
+ while self ._content_slice [arg_start ].isspace ():
146
152
arg_start += 1
147
- if self .__content_slice .__len__ () == arg_start :
153
+ if self ._content_slice .__len__ () == arg_start :
148
154
raise AssertionError (
149
155
f"Appears to be an unclosed function call `{ dot_fun_open_paren } ` "
150
156
f"on line { line_one_indexed } "
151
157
)
152
158
153
159
end_arg = - 1
154
160
end_paren = 0
155
- if self .__content_slice [arg_start ] == '"' :
156
- if self .__content_slice [arg_start ].startswith (self .TRIPLE_QUOTE ):
157
- end_arg = self .__content_slice .indexOf (
161
+ if self ._content_slice [arg_start ] == '"' :
162
+ if self ._content_slice [arg_start ].startswith (self .TRIPLE_QUOTE ):
163
+ end_arg = self ._content_slice .indexOf (
158
164
self .TRIPLE_QUOTE , arg_start + len (self .TRIPLE_QUOTE )
159
165
)
160
166
if end_arg == - 1 :
@@ -168,11 +174,11 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
168
174
else :
169
175
end_arg = arg_start + 1
170
176
while (
171
- self .__content_slice [end_arg ] != '"'
172
- or self .__content_slice [end_arg - 1 ] == "\\ "
177
+ self ._content_slice [end_arg ] != '"'
178
+ or self ._content_slice [end_arg - 1 ] == "\\ "
173
179
):
174
180
end_arg += 1
175
- if end_arg == self .__content_slice .__len__ ():
181
+ if end_arg == self ._content_slice .__len__ ():
176
182
raise AssertionError (
177
183
f'Appears to be an unclosed string literal `"` '
178
184
f"on line { line_one_indexed } "
@@ -181,37 +187,44 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
181
187
end_paren = end_arg
182
188
else :
183
189
end_arg = arg_start
184
- while not self .__content_slice [end_arg ].isspace ():
185
- if self .__content_slice [end_arg ] == ")" :
190
+ while not self ._content_slice [end_arg ].isspace ():
191
+ if self ._content_slice [end_arg ] == ")" :
186
192
break
187
193
end_arg += 1
188
- if end_arg == self .__content_slice .__len__ ():
194
+ if end_arg == self ._content_slice .__len__ ():
189
195
raise AssertionError (
190
196
f"Appears to be an unclosed numeric literal "
191
197
f"on line { line_one_indexed } "
192
198
)
193
199
end_paren = end_arg
194
- while self .__content_slice [end_paren ] != ")" :
195
- if not self .__content_slice [end_paren ].isspace ():
200
+ while self ._content_slice [end_paren ] != ")" :
201
+ if not self ._content_slice [end_paren ].isspace ():
196
202
raise AssertionError (
197
203
f"Non-primitive literal in `{ dot_fun_open_paren } ` starting at "
198
204
f"line { line_one_indexed } : error for character "
199
- f"`{ self .__content_slice [end_paren ]} ` on line "
200
- f"{ self .__content_slice .baseLineAtOffset (end_paren )} "
205
+ f"`{ self ._content_slice [end_paren ]} ` on line "
206
+ f"{ self ._content_slice .baseLineAtOffset (end_paren )} "
201
207
)
202
208
end_paren += 1
203
- if end_paren == self .__content_slice .__len__ ():
209
+ if end_paren == self ._content_slice .__len__ ():
204
210
raise AssertionError (
205
211
f"Appears to be an unclosed function call `{ dot_fun_open_paren } ` "
206
212
f"starting at line { line_one_indexed } "
207
213
)
208
214
return self .ToBeLiteral (
215
+ self ,
209
216
dot_fun_open_paren .replace ("_TODO" , "" ),
210
- self .__content_slice .subSequence (dot_function_call , end_paren + 1 ),
211
- self .__content_slice .subSequence (arg_start , end_arg ),
217
+ self ._content_slice .subSequence (dot_function_call , end_paren + 1 ),
218
+ self ._content_slice .subSequence (arg_start , end_arg ),
212
219
self .__language ,
213
220
self .__escape_leading_whitespace ,
214
221
)
215
222
216
223
217
- TO_BE_LIKES = [".toBe(" , ".toBe_TODO(" , ".toBeBase64(" , ".toBeBase64_TODO(" ]
224
+ TO_BE_LIKES = [
225
+ ".to_be(" ,
226
+ ".to_be_TODO(" ,
227
+ ".to_be_base64(" ,
228
+ ".to_be_base64_TODO(" ,
229
+ ".to_be_TODO(" ,
230
+ ]
0 commit comments