2016-08-07 17:56:15 +02:00
|
|
|
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import List, Set, Tuple
|
2016-08-07 17:56:15 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
GENERIC_KEYWORDS = [
|
|
|
|
'active',
|
|
|
|
'alert',
|
|
|
|
'danger',
|
|
|
|
'condensed',
|
|
|
|
'disabled',
|
2018-08-05 02:43:03 +02:00
|
|
|
'enabled',
|
2016-08-07 17:56:15 +02:00
|
|
|
'error',
|
|
|
|
'expanded',
|
2017-05-05 20:38:15 +02:00
|
|
|
'fade-out',
|
|
|
|
'first',
|
2016-08-07 17:56:15 +02:00
|
|
|
'hide',
|
2017-04-24 04:11:25 +02:00
|
|
|
'in',
|
2016-09-22 02:05:24 +02:00
|
|
|
'show',
|
2016-08-07 17:56:15 +02:00
|
|
|
'notdisplayed',
|
|
|
|
'popover',
|
2016-12-03 01:12:52 +01:00
|
|
|
'no-border',
|
2018-07-05 12:17:20 +02:00
|
|
|
'rtl',
|
2017-05-05 20:38:15 +02:00
|
|
|
'second',
|
|
|
|
'selected',
|
|
|
|
'slide-left',
|
2016-08-07 17:56:15 +02:00
|
|
|
'success',
|
|
|
|
'text-error',
|
|
|
|
'warning',
|
2016-11-11 12:33:51 +01:00
|
|
|
'zoom-in', # TODO: clean these up, they are confusing
|
|
|
|
'zoom-out',
|
2016-08-07 17:56:15 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def raise_error(fn, i, line):
|
|
|
|
# type: (str, int, str) -> None
|
|
|
|
error = '''
|
|
|
|
In %s line %d there is the following line of code:
|
|
|
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
Our tools want to be able to identify which modules
|
|
|
|
add which HTML/CSS classes, and we need two things to
|
|
|
|
happen:
|
|
|
|
|
|
|
|
- The code must explicitly name the class.
|
|
|
|
- Only one module can refer to that class (unless
|
|
|
|
it is something generic like an alert class).
|
|
|
|
|
|
|
|
If you get this error, you can usually address it by
|
|
|
|
refactoring your code to be more explicit, or you can
|
|
|
|
move the common code that sets the class to a library
|
|
|
|
module. If neither of those applies, you need to
|
|
|
|
modify %s
|
|
|
|
''' % (fn, i, line, __file__)
|
|
|
|
raise Exception(error)
|
|
|
|
|
|
|
|
def generic(html_class):
|
|
|
|
# type: (str) -> bool
|
|
|
|
for kw in GENERIC_KEYWORDS:
|
|
|
|
if kw in html_class:
|
|
|
|
return True
|
2017-03-03 20:30:49 +01:00
|
|
|
return False
|
2016-08-07 17:56:15 +02:00
|
|
|
|
|
|
|
def display(fns):
|
|
|
|
# type: (List[str]) -> None
|
|
|
|
for tup in find(fns):
|
|
|
|
# this format is for code generation purposes
|
|
|
|
print(' ' * 8 + repr(tup) + ',')
|
|
|
|
|
|
|
|
def find(fns):
|
|
|
|
# type: (List[str]) -> List[Tuple[str, str]]
|
2017-05-07 16:53:31 +02:00
|
|
|
encountered = set() # type: Set[str]
|
|
|
|
tups = [] # type: List[Tuple[str, str]]
|
2017-08-27 23:03:03 +02:00
|
|
|
for full_fn in fns:
|
2017-08-27 23:04:35 +02:00
|
|
|
# Don't check frontend tests, since they may do all sorts of
|
|
|
|
# extra hackery that isn't of interest to us.
|
|
|
|
if full_fn.startswith("frontend_tests"):
|
|
|
|
continue
|
2017-08-27 23:03:03 +02:00
|
|
|
lines = list(open(full_fn))
|
|
|
|
fn = os.path.basename(full_fn)
|
2017-05-07 16:53:31 +02:00
|
|
|
module_classes = set() # type: Set[str]
|
2016-08-07 17:56:15 +02:00
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if 'addClass' in line:
|
2017-05-07 16:53:31 +02:00
|
|
|
html_classes = [] # type: List[str]
|
2018-07-02 00:05:24 +02:00
|
|
|
m = re.search(r'''addClass\(['"](.*?)['"]''', line)
|
2016-08-07 17:56:15 +02:00
|
|
|
if m:
|
|
|
|
html_classes = [m.group(1)]
|
|
|
|
if not html_classes:
|
|
|
|
if 'bar-success' in line:
|
|
|
|
html_classes = ['bar-success', 'bar-danger']
|
2017-07-14 03:21:30 +02:00
|
|
|
elif fn == 'hotspots.js' and 'arrow_placement' in line:
|
|
|
|
html_classes = ['arrow-top', 'arrow-left', 'arrow-bottom', 'arrow-right']
|
2016-08-07 17:56:15 +02:00
|
|
|
elif 'color_class' in line:
|
|
|
|
continue
|
|
|
|
elif 'stream_dark' in line:
|
|
|
|
continue
|
2018-04-21 16:41:07 +02:00
|
|
|
elif 'opts.' in line:
|
|
|
|
continue
|
2016-08-07 17:56:15 +02:00
|
|
|
elif fn == 'signup.js' and 'class_to_add' in line:
|
|
|
|
html_classes = ['error', 'success']
|
2017-04-03 16:13:25 +02:00
|
|
|
elif fn == 'ui_report.js' and 'status_classes' in line:
|
2016-08-07 17:56:15 +02:00
|
|
|
html_classes = ['alert']
|
|
|
|
|
|
|
|
if not html_classes:
|
2017-08-27 23:03:03 +02:00
|
|
|
raise_error(full_fn, i, line)
|
2016-08-07 17:56:15 +02:00
|
|
|
for html_class in html_classes:
|
|
|
|
if generic(html_class):
|
|
|
|
continue
|
|
|
|
if html_class in module_classes:
|
|
|
|
continue
|
|
|
|
if html_class in encountered:
|
2017-08-27 23:03:03 +02:00
|
|
|
raise_error(full_fn, i, line)
|
2016-08-07 17:56:15 +02:00
|
|
|
tups.append((fn, html_class))
|
|
|
|
module_classes.add(html_class)
|
|
|
|
encountered.add(html_class)
|
|
|
|
return tups
|