Debian Codegraph @

test/common/typecheck

raw
1 #! /usr/bin/python3
2
3 import argparse
4 import os
5 import re
6 import subprocess
7 import sys
8 from collections import Counter
9 from functools import cache
10
11 # Run tsc as a type checker and throw away much of its output, in a
12 # controlled way.
13 #
14 # Briefly, this tool implements the missing "@ts-no-strict" annotation
15 # in a post-processing step, but also adds more fine grained
16 # control. We can tweaks this to behave exactly like we want.
17 #
18 # Being lax with some files makes it easy (if not trivial) to include
19 # them in useful typechecking. When a library file is fully typed, we
20 # don't get much benefit from that unless at least of its users also
21 # participate in typechecking. And to get them to participate, we need
22 # to make it possible to ignore most of their internal typing
23 # challenges. TypeScript is pretty good at this, actually, and
24 # willingly infer the "any" type for almost anything.
25 #
26 # But we don't want this for our fully typed files. We don't want
27 # "noImplicitAny: false" in our tsconfig.json.
28 #
29 # And we want to run TypeScript only once, with our ideal flavour of
30 # strictness. We don't want to run some files through tsc with a
31 # strict tsconfig.json, and other files with a more lax tsconfig.json,
32 # and then combine the results somehow.
33 #
34 # This makes it less confusing when using LSP in our editors. We will
35 # see the same errors in our editors that this tool sees. Inference
36 # rules seem to change depending on the configuration, so if this tool
37 # uses a different tsconfig than the LSP servers, things will not
38 # always make sense.
39
40 # This tool recognizes the following annotation:
41 #
42 # - @cockpit-ts-relaxed
43 #
44 # If this appears in a file, any error related to implicit 'any' types
45 # will be ignored.
46
47
48 # These errors are ignored for "relaxed" files. This is supposed to be
49 # roughly equivalent to "noImplicitAny = false".
50
51 ignored_codes = [
52 "TS2683", # 'this' implicitly has type 'any' because it does not have a type annotation.
53 "TS7005", # Variable '*' implicitly has an 'any[]' type.
54 "TS7006", # Parameter '*' implicitly has an 'any' type.
55 "TS7008", # Member '*' implicitly has an 'any[]' type.
56 "TS7010", # '*', which lacks return-type annotation, implicitly has an 'any' return type.
57 "TS7015", # Element implicitly has an 'any' type because index expression is not of type '*'.
58 "TS7016", # Could not find a declaration file for module '*'...
59 "TS7019", # Rest parameter '*' implicitly has an 'any[]' type
60 "TS7022", # '*' implicitly has type 'any'...
61 "TS7023", # '*' implicitly has return type 'any' because ...
62 "TS7024", # Function implicitly has return type 'any' because ...
63 "TS7031", # Binding element '*' implicitly has an 'any' type.
64 "TS7034", # Variable '*' implicitly has type 'any' in some locations where its type cannot be determined.
65 "TS7053", # Element implicitly has an 'any' type because expression of type 'any' can't be used to
66 # index type '*'.
67 ]
68
69 javascript_ignored_codes = [
70 "TS2683", # 'this' implicitly has type 'any' because it does not have a type annotation.
71 "TS7005", # Variable '*' implicitly has an 'any[]' type.
72 "TS7006", # Parameter '*' implicitly has an 'any' type.
73 "TS7008", # Member '*' implicitly has an 'any[]' type.
74 "TS7015", # Element implicitly has an 'any' type because index expression is not of type '*'.
75 "TS7016", # Could not find a declaration file for module '*'...
76 "TS7019", # Rest parameter '*' implicitly has an 'any[]' type
77 "TS7022", # '*' implicitly has type 'any'...
78 "TS7023", # '*' implicitly has return type 'any' because ...
79 "TS7031", # Binding element '*' implicitly has an 'any' type.
80 "TS7034", # Variable '*' implicitly has type 'any' in some locations where its type cannot be determined.
81 "TS7053", # Element implicitly has an 'any' type because expression of type 'any' can't be used to
82 # index type '*'.
83 "TS2531", # Object is possibly 'null'.
84 "TS2339", # Property X does not exist on type '{}'.
85 "TS2307", # Cannot find module 'Y' or its corresponding type declarations.
86 "TS18046", # 'X' is of type 'unknown'.
87 "TS2322", # Type 'X' is not assignable to type 'never'.
88 "TS2375", # Type 'X' is not assignable to type 'never'.
89 "TS18047", # 'Y' is possibly 'null'.
90 "TS2345", # Argument of type 'null' is not assignable to parameter of type '(Comparator<never> | undefined)[]'.
91 "TS2571", # Object is of type 'unknown'.
92 "TS2353", # Object literal may only specify known properties
93 "TS2533", # Object is possibly 'null' or 'undefined'.
94 "TS2532", # Object is possibly 'undefined'.
95 "TS18048", # 'X' is possibly 'undefined'.
96 "TS2741", # Property 'X' is missing in type
97 "TS2551", # Property 'X' does not exist on type
98 "TS2304", # Cannot find name 'X'
99 "TS2538", # Type 'null' cannot be used as an index type.
100 "TS2769", # No overload matches this call.
101 "TS2739", # Type '{ title: string; value: string; }' is missing the following properties from type
102 "TS2488", # Type 'X' must have a '[Symbol.iterator]()' method that returns an iterator.
103 "TS2349", # This expression is not callable.
104 "TS2554", # Expected 0 arguments, but got 1.
105 "TS2810", # Expected 1 argument, but got 0.
106 ]
107
108
109 in_core_project = os.path.exists("pkg/base1")
110
111
112 def should_ignore(path):
113 if path.startswith("node_modules/"):
114 return True
115 if not in_core_project and path.startswith("pkg/lib/"):
116 return True
117 return False
118
119
120 @cache
121 def is_relaxed(path):
122 with open(path) as fp:
123 return "@cockpit-ts-relaxed" in fp.read()
124
125
126 num_errors = 0
127
128
129 def show_error(lines):
130 global num_errors
131 num_errors += 1
132 for line in lines:
133 sys.stdout.write(line)
134
135
136 def consider(lines, js_error_codes):
137 m = re.match(r"([^:]*)\(.*\): error ([^:]*): .*", lines[0])
138 if m and not should_ignore(m[1]):
139 relaxed = is_relaxed(m[1])
140 is_js = m[1].endswith(('.js', '.jsx'))
141
142 if is_js:
143 if m[2] not in javascript_ignored_codes:
144 show_error(lines)
145 else:
146 js_error_codes[m[2]] += 1
147 if not is_js and (not relaxed or m[2] not in ignored_codes):
148 show_error(lines)
149
150
151 try:
152 proc = subprocess.Popen(["node_modules/.bin/tsc", "--pretty", "false"], stdout=subprocess.PIPE, text=True)
153 except FileNotFoundError:
154 print("no tsc")
155 sys.exit(77)
156
157 cur = []
158 js_error_codes: Counter = Counter()
159 parser = argparse.ArgumentParser(description="Run tsc as a typechecker with an allowlist of errors for JavaScript and TypeScript files")
160 parser.add_argument("--stats", action="store_true", help="Display stats of the most common TypeScript errors in JavaScript files")
161 args = parser.parse_args()
162
163 if proc.stdout:
164 for line in proc.stdout:
165 if line[0] == " ":
166 cur += [line]
167 else:
168 if len(cur) > 0:
169 consider(cur, js_error_codes)
170 cur = [line]
171 if len(cur) > 0:
172 consider(cur, js_error_codes)
173
174 if args.stats:
175 for ts_error, count in js_error_codes.most_common():
176 print(ts_error, count)
177 else:
178 # Fail on unused ignored JavaScript error codes, so accidental or intentional fixes don't get ignored.
179 # For now this only applies to cockpit itself, we need to see how much this makes sense in external projects.
180 if in_core_project and len(js_error_codes) != len(javascript_ignored_codes):
181 errors = set(javascript_ignored_codes) - set(js_error_codes)
182 print(f"Unused JavaScript ignored error codes found: {','.join(errors)}", file=sys.stderr)
183 sys.exit(1)
184
185
186 sys.exit(1 if num_errors > 0 else 0)