This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
find_unused_strings.py
172 lines (140 loc) · 5.73 KB
/
find_unused_strings.py
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
import argparse
import logging
import os
import sys
import json
from typing import Set
from typing import Dict
from typing import List
from pathlib import Path
import re
import unittest
def main(argv):
logging.getLogger().setLevel(logging.INFO)
parser = argparse.ArgumentParser(
description='Find unused strings')
parser.add_argument('--strings-file', default='lib/l10n/app_en.arb')
parser.add_argument('--project-root', default='.')
parser.add_argument('--ignored-prefixes', nargs='*', default=['ios_NS', 'app_markets'])
parser.add_argument('--fail-if-found', action='store_true')
options = parser.parse_args()
strings_used_in_code = extract_strings_from_dir_recursive(options.project_root)
declared_strings = extract_declared_dart_strings_from_file(options.strings_file)
difference = declared_strings.difference(strings_used_in_code)
ignored_prefixes = list(options.ignored_prefixes) + ['@']
def should_ignore(string: str):
return any(map(lambda prefix: string.startswith(prefix), ignored_prefixes))
difference = set(filter(lambda string: should_ignore(string) is False, difference))
if difference:
print('Unused strings:')
for string in difference:
print(' {}'.format(string))
if options.fail_if_found:
sys.exit('ERROR: Found unused Flutter strings')
def extract_strings_from_str(string: str):
result = set()
result.update(re.findall('context\.strings\.(\w+)', string))
complex_result = re.findall('context\.strings( |\t)*\n( |\t)*\.(\w+)', string)
result.update(map(lambda tuple: tuple[2], complex_result))
complex_result = re.findall('context( |\t)*\n( |\t)*\.strings\.(\w+)', string)
result.update(map(lambda tuple: tuple[2], complex_result))
return result
def extract_strings_from_file(file_path: str):
file_content = None
with open(file_path, 'r', encoding='utf-8') as f:
file_content = f.read(file_content)
return extract_strings_from_str(file_content)
def extract_strings_from_dir_recursive(dir_path: str, acceptable_files='.*\.dart$'):
result = set()
for path, subdirs, files in os.walk(dir_path):
for name in files:
if not re.match(acceptable_files, name):
continue
result.update(extract_strings_from_file(os.path.join(path, name)))
return result
def extract_declared_dart_strings_from_file(file_path: str):
strings = None
with open(file_path, 'r', encoding='utf-8') as f:
strings = json.load(f)
return set(strings.keys())
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
class MainTests(unittest.TestCase):
def test_extract_strings_from_str(self):
string = '''
shopsManager.verity_createShop_called(times: 0);
await tester.superTap(find.text(context.strings.global_done));
expect(find.byType(MapPage), findsNothing);
...
Text(
context.strings
.display_product_page_veg_status_possible_explanation,
style: TextStyles.normal)
...
Text(
context
.strings.edit_user_data_widget_avatar_description,
style: TextStyles.normalColored
.copyWith(color: ColorsPlante.grey))
'''
result = extract_strings_from_str(string)
expected_result = {
'global_done',
'display_product_page_veg_status_possible_explanation',
'edit_user_data_widget_avatar_description',
}
self.assertEqual(expected_result, result)
def test_extract_strings_from_file(self):
file_content = '''
shopsManager.verity_createShop_called(times: 0);
await tester.superTap(find.text(context.strings.global_done));
expect(find.byType(MapPage), findsNothing);
'''
file_path = '/tmp/test_extract_strings_from_file'
with open(file_path, 'w', encoding='utf-8') as f:
f.write(file_content)
result = extract_strings_from_file(file_path)
expected_result = {
'global_done',
}
self.assertEqual(expected_result, result)
def test_extract_strings_from_dir_recursive(self):
Path('/tmp/test_extract_strings_from_dir_recursive/1/2').mkdir(parents=True, exist_ok=True)
file_content = '''
shopsManager.verity_createShop_called(times: 0);
await tester.superTap(find.text(context.strings.{}));
expect(find.byType(MapPage), findsNothing);
'''
with open('/tmp/test_extract_strings_from_dir_recursive/code.dart', 'w', encoding='utf-8') as f:
f.write(file_content.format('global_done1'))
with open('/tmp/test_extract_strings_from_dir_recursive/not_code.not_dart', 'w', encoding='utf-8') as f:
f.write(file_content.format('global_done2'))
with open('/tmp/test_extract_strings_from_dir_recursive/1/code.dart', 'w', encoding='utf-8') as f:
f.write(file_content.format('global_done3'))
with open('/tmp/test_extract_strings_from_dir_recursive/1/2/code.dart', 'w', encoding='utf-8') as f:
f.write(file_content.format('global_done4'))
result = extract_strings_from_dir_recursive('/tmp/test_extract_strings_from_dir_recursive/')
expected_result = {
'global_done1',
'global_done3',
'global_done4',
}
self.assertEqual(expected_result, result)
def test_extract_declared_dart_strings_from_file(self):
file_content = '''
{
"veg_status_selection_panel_dunno": "Not sure",
"veg_status_selection_panel_negative": "No",
"veg_status_selection_panel_positive": "Yes"
}
'''
file_path = '/tmp/test_extract_declared_dart_strings_from_file'
with open(file_path, 'w', encoding='utf-8') as f:
f.write(file_content)
result = extract_declared_dart_strings_from_file(file_path)
expected_result = {
'veg_status_selection_panel_dunno',
'veg_status_selection_panel_negative',
'veg_status_selection_panel_positive',
}
self.assertEqual(expected_result, result)