@@ -152,22 +152,38 @@ def add_pyright_exclusion(stub_dir: str) -> None:
152
152
assert i < len (lines ), f"Error parsing { PYRIGHT_CONFIG } "
153
153
while not lines [i ].strip ().startswith ("]" ):
154
154
i += 1
155
- # Must use forward slash in the .json file
156
- line_to_add = f' "{ stub_dir } ",' .replace ("\\ " , "/" )
157
- initial = i - 1
158
- while lines [i ].lower () > line_to_add .lower ():
155
+ end = i
156
+
157
+ # We assume that all third-party excludes must be at the end of the list.
158
+ # This helps with skipping special entries, such as "stubs/**/@tests/test_cases".
159
+ while lines [i - 1 ].strip ().startswith ('"stubs/' ):
159
160
i -= 1
160
- if lines [i + 1 ].strip ().rstrip ("," ) == line_to_add .strip ().rstrip ("," ):
161
+ start = i
162
+
163
+ before_third_party_excludes = lines [:start ]
164
+ third_party_excludes = lines [start :end ]
165
+ after_third_party_excludes = lines [end :]
166
+
167
+ last_line = third_party_excludes [- 1 ].rstrip ()
168
+ if not last_line .endswith ("," ):
169
+ last_line += ","
170
+ third_party_excludes [- 1 ] = last_line + "\n "
171
+
172
+ # Must use forward slash in the .json file
173
+ line_to_add = f' "{ stub_dir } ",\n ' .replace ("\\ " , "/" )
174
+
175
+ if line_to_add in third_party_excludes :
161
176
print (f"{ PYRIGHT_CONFIG } already up-to-date" )
162
177
return
163
- if i == initial :
164
- # Special case: when adding to the end of the list, commas need tweaking
165
- line_to_add = line_to_add .rstrip ("," )
166
- lines [i ] = lines [i ].rstrip () + ",\n "
167
- lines .insert (i + 1 , line_to_add + "\n " )
178
+
179
+ third_party_excludes .append (line_to_add )
180
+ third_party_excludes .sort (key = str .lower )
181
+
168
182
print (f"Updating { PYRIGHT_CONFIG } " )
169
183
with open (PYRIGHT_CONFIG , "w" , encoding = "UTF-8" ) as f :
170
- f .writelines (lines )
184
+ f .writelines (before_third_party_excludes )
185
+ f .writelines (third_party_excludes )
186
+ f .writelines (after_third_party_excludes )
171
187
172
188
173
189
def main () -> None :
0 commit comments