import os import re for base, dirs, files in os.walk('src/app'): for file in files: path = os.path.join(base, file) if file.endswith('.js') || file.endswith('.jsx') || file.endswith('.ts') || file.endswith('.tsx'): lines = open(path).read().splitlines() binds = [(i, x) for i, x in enumerate(lines) if '.bind(this);' in x] if len(binds) == 0: continue if lines[binds[0][0]-1] == '': lines[binds[0][0]-1] = None for i, _ in binds: lines[i] = None lines = [x for x in lines if x is not None] for _, bind in binds: name_matches = re.search('this.(\w+) = this.\w+.bind\(this\);', bind) name = name_matches.group(1) matches = None for i, line in enumerate(lines): matches = re.search('^([ ]+)%s\((.*?)\) \{$' % name, line) if matches: break matches = re.search('^([ ]+)%s\((.*?)\): void \{$' % name, line) if matches: break matches = re.search('^([ ]+)private %s\((.*?)\) \{$' % name, line) if matches: break matches = re.search('^([ ]+)private %s\((.*?)\): void \{$' % name, line) if matches: break matches = re.search('^([ ]+)public %s\((.*?)\) \{$' % name, line) if matches: break matches = re.search('^([ ]+)public %s\((.*?)\): void \{$' % name, line) if matches: break if matches is not None: lines[i] = '%s%s = (%s) => {' % (matches.group(1), name, matches.group(2)) else: print 'Not found', path, name newcontent = '\n'.join(lines) newcontent.strip() newcontent += '\n' open(path, 'w').write(newcontent)