mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
The CSS linter was pretty hard to reason about. It was pretty flexible about certain things, but then it would prevent seemingly innocuous code from getting checked in. This commit overhauls the pretty-printer to be more composable, where every object in the AST knows how to render itself. It also cleans up a little bit of the pre_fluff/post_fluff logic in the parser itself, so comments are more likely to be "attached" to the AST node that make sense. The linter is actually a bit more finicky about newlines, but this is mostly a good thing, as most of the variations before this commit were pretty arbitrary.
48 lines
1.4 KiB
Python
Executable File
48 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from lib.css_parser import parse, CssParserException
|
|
from typing import Iterable, Text
|
|
import sys
|
|
import glob
|
|
import subprocess
|
|
|
|
# check for the venv
|
|
from lib import sanity_check
|
|
sanity_check.check_venv(__file__)
|
|
|
|
def validate(fn):
|
|
# type: (str) -> None
|
|
text = open(fn).read()
|
|
section_list = parse(text)
|
|
if text != section_list.text():
|
|
print('%s seems to be broken:' % (fn,))
|
|
open('/var/tmp/pretty_css.txt', 'w').write(section_list.text())
|
|
subprocess.call(['diff', fn, '/var/tmp/pretty_css.txt'], stderr=subprocess.STDOUT)
|
|
sys.exit(1)
|
|
|
|
def check_our_files(filenames):
|
|
# type: (Iterable[str]) -> None
|
|
for filename in filenames:
|
|
if 'pygments.css' in filename:
|
|
# This just has really strange formatting that our
|
|
# parser doesn't like
|
|
continue
|
|
|
|
try:
|
|
validate(filename)
|
|
except CssParserException as e:
|
|
msg = '''
|
|
ERROR! Some CSS seems to be misformatted.
|
|
{}
|
|
See line {} in file {}
|
|
'''.format(e.msg, e.token.line, filename)
|
|
print(msg)
|
|
sys.exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
# If command arguments are provided, we only check those filenames.
|
|
# Otherwise, we check all possible filenames.
|
|
filenames = sys.argv[1:]
|
|
if not filenames:
|
|
filenames = glob.glob('static/styles/*.css')
|
|
check_our_files(filenames)
|