summaryrefslogtreecommitdiff
path: root/tools/lint/lint.py
blob: 9c446b5bb9f95b2f7b3af464e5a59c0ce8480dcf (plain)
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
#!/usr/bin/env python
# Copyright (C) 2017 Mike Pennisi. All rights reserved.
# This code is governed by the BSD license found in the LICENSE file.

import argparse
import sys

from lib.collect_files import collect_files
from lib.checks.features import CheckFeatures
from lib.checks.harnessfeatures import CheckHarnessFeatures
from lib.checks.frontmatter import CheckFrontmatter
from lib.checks.license import CheckLicense
from lib.checks.negative import CheckNegative
from lib.eprint import eprint
import lib.frontmatter
import lib.whitelist

parser = argparse.ArgumentParser(description='Test262 linting tool')
parser.add_argument('--whitelist',
        type=argparse.FileType('r'),
        help='file containing expected linting errors')
parser.add_argument('path',
        nargs='+',
        help='file name or directory of files to lint')

checks = [
        CheckFrontmatter(),
        CheckFeatures('features.txt'),
        CheckHarnessFeatures(),
        CheckLicense(),
        CheckNegative()
    ]

def lint(file_names):
    errors = dict()

    for file_name in file_names:
        with open(file_name, 'r') as f:
            content = f.read()
        meta = lib.frontmatter.parse(content)
        for check in checks:
            error = check.run(file_name, meta, content)

            if error is not None:
                if file_name not in errors:
                    errors[file_name] = dict()
                errors[file_name][check.ID] = error

    return errors

if __name__ == '__main__':
    args = parser.parse_args()
    if args.whitelist:
        whitelist = lib.whitelist.parse(args.whitelist)
    else:
        whitelist = dict()

    files = [path for _path in args.path for path in collect_files(_path)]
    file_count = len(files)
    print 'Linting %s file%s.' % (file_count, 's' if file_count != 1 else '')

    all_errors = lint(files)
    unexpected_errors = dict(all_errors)

    for file_name, failures in all_errors.iteritems():
        if file_name not in whitelist:
            continue
        if set(failures.keys()) == whitelist[file_name]:
            del unexpected_errors[file_name]

    error_count = len(unexpected_errors)
    s = 's' if error_count != 1 else ''

    print 'Linting complete. %s error%s found.' % (error_count, s)

    if error_count == 0:
        sys.exit(0)

    for file_name, failures in unexpected_errors.iteritems():
        for ID, message in failures.iteritems():
            eprint('%s: %s - %s' % (file_name, ID, message))

    sys.exit(1)