main
1// Switches and if-chains over a closed union end in assertNever, with no default that hides a missing member.
2// Oxlint's switch-exhaustiveness-check covers typed switches (no default on an exhaustive union; default does not
3// count as a member). It does not require the never-check to be the next statement, and it does not see an if chain.
4// This script is the other half, run from `npm run lint`.
5import { parse } from '@babel/parser';
6import { readFileSync, readdirSync } from 'node:fs';
7import { join } from 'node:path';
9const problems = [];
10const typeOf = node => (node && node.type) || '';
11const ABSENT_NAME = 'undef' + 'ined';
12const isFn = node => {
13 const t = typeOf(node);
14 return t === 'FunctionDeclaration' || t === 'FunctionExpression' || t === 'ArrowFunctionExpression' || t === 'ObjectMethod' || t === 'ClassMethod';
15};
17const isNeverCall = node => typeOf(node) === 'CallExpression' && typeOf(node.callee) === 'Identifier' && node.callee.name === 'assertNever';
19// Babel's ReturnStatement uses `argument` (ESTree shape in this parser); ExpressionStatement uses `expression`.
20const stmtValue = stmt => stmt.expression || stmt.argument || null;
22const stmtIsNever = stmt => {
23 if (!stmt) return false;
24 const t = typeOf(stmt);
25 if (t === 'ReturnStatement' || t === 'ExpressionStatement') return isNeverCall(stmtValue(stmt));
26 if (t === 'BlockStatement') {
27 const body = stmt.body.filter(s => typeOf(s) !== 'EmptyStatement');
28 return body.length === 1 && stmtIsNever(body[0]);
29 }
30 return false;
31};
33const caseIsOnlyNever = consequent => {
34 const body = consequent.filter(s => typeOf(s) !== 'BreakStatement' && typeOf(s) !== 'EmptyStatement');
35 return body.length === 1 && stmtIsNever(body[0]);
36};
38const literalCase = test => {
39 const t = typeOf(test);
40 return t === 'StringLiteral' || t === 'NumericLiteral' || t === 'NullLiteral' || t === 'BooleanLiteral'
41 || (t === 'Identifier' && test.name === ABSENT_NAME)
42 || (t === 'TemplateLiteral' && test.expressions.length === 0);
43};
45const propName = node => (typeOf(node) === 'MemberExpression' && !node.computed && typeOf(node.property) === 'Identifier' ? node.property.name : null);
47const exprKey = node => {
48 if (!node) return null;
49 if (node.type === 'Identifier') return node.name;
50 if (node.type === 'MemberExpression' && !node.computed && typeOf(node.property) === 'Identifier') {
51 const o = exprKey(node.object);
52 return o === null ? null : `${o}.${node.property.name}`;
53 }
54 return null;
55};
57// x.tag === 'lit' / x.code === 'lit' (either side).
58const tagCompare = test => {
59 if (typeOf(test) !== 'BinaryExpression' || test.operator !== '===') return null;
60 const mem = test.left.type === 'MemberExpression' ? test.left : test.right.type === 'MemberExpression' ? test.right : null;
61 const lit = test.left.type === 'StringLiteral' || test.left.type === 'NullLiteral' || test.left.type === 'BooleanLiteral' ? test.left
62 : test.right.type === 'StringLiteral' || test.right.type === 'NullLiteral' || test.right.type === 'BooleanLiteral' ? test.right : null;
63 if (!mem || !lit) return null;
64 const prop = propName(mem);
65 if (prop !== 'tag' && prop !== 'code') return null;
66 const obj = exprKey(mem.object);
67 if (obj === null) return null;
68 return `${obj}.${prop}`;
69};
71const kindChainTest = test => {
72 if (typeOf(test) !== 'BinaryExpression' || test.operator !== '===') return null;
73 const mem = test.left.type === 'MemberExpression' ? test.left : test.right.type === 'MemberExpression' ? test.right : null;
74 const lit = test.left.type === 'StringLiteral' ? test.left : test.right.type === 'StringLiteral' ? test.right : null;
75 if (!mem || !lit) return null;
76 const prop = propName(mem);
77 if (prop !== 'tag' && prop !== 'code' && prop !== 'kind') return null;
78 const obj = exprKey(mem.object);
79 return obj === null ? null : `${obj}.${prop}`;
80};
82function walk(node, parent, visit) {
83 if (!node || typeof node !== 'object' || !node.type) return;
84 visit(node, parent);
85 for (const key of Object.keys(node)) {
86 if (key === 'loc' || key === 'range' || key === 'leadingComments' || key === 'trailingComments' || key === 'innerComments') continue;
87 const child = node[key];
88 if (Array.isArray(child)) { for (const c of child) if (c && typeof c.type === 'string') walk(c, node, visit); }
89 else if (child && typeof child.type === 'string') walk(child, node, visit);
90 }
91}
93// Visit fn's body but not nested functions.
94function walkOwn(fn, visit) {
95 const rec = (node, parent) => {
96 if (!node || typeof node !== 'object' || !node.type) return;
97 if (node !== fn && isFn(node)) return;
98 visit(node, parent);
99 for (const key of Object.keys(node)) {
100 if (key === 'loc' || key === 'range' || key === 'leadingComments' || key === 'trailingComments' || key === 'innerComments') continue;
101 const child = node[key];
102 if (Array.isArray(child)) { for (const c of child) if (c && typeof c.type === 'string') rec(c, node); }
103 else if (child && typeof child.type === 'string') rec(child, node);
104 }
105 };
106 rec(fn.body, fn);
107}
109const nextStmt = (node, parent) => {
110 if (typeOf(parent) !== 'BlockStatement') return null;
111 const i = parent.body.indexOf(node);
112 if (i < 0) return null;
113 const next = parent.body[i + 1];
114 return next ? next : null;
115};
117const lineOf = node => {
118 const loc = node && node.loc;
119 const start = loc && loc.start;
120 const line = start && start.line;
121 return line ? line : 0;
122};
124const at = (file, node) => `${file}:${lineOf(node)}`;
126function checkSwitch(file, node, parent) {
127 const def = node.cases.find(c => c.test === null);
128 if (def && caseIsOnlyNever(def.consequent)) problems.push(`${at(file, node)} default is only assertNever; the never-check is the statement after the switch`);
129 const prop = propName(node.discriminant);
130 const closed = prop === 'tag' || prop === 'code';
131 const literals = node.cases.length > 0 && node.cases.every(c => c.test === null || literalCase(c.test));
132 if (!def && (closed || literals) && !stmtIsNever(nextStmt(node, parent))) {
133 problems.push(`${at(file, node)} switch over a closed set has no assertNever as the next statement`);
134 }
135}
137function checkElseIf(file, node, parent) {
138 if (typeOf(parent) === 'IfStatement' && parent.alternate === node) return;
139 const tests = [];
140 let cur = node;
141 let terminal = null;
142 while (typeOf(cur) === 'IfStatement') {
143 tests.push(cur.test);
144 if (typeOf(cur.alternate) === 'IfStatement') { cur = cur.alternate; continue; }
145 terminal = cur.alternate;
146 break;
147 }
148 if (tests.length < 2) return;
149 const keys = tests.map(kindChainTest);
150 if (keys.some(k => k === null) || new Set(keys).size !== 1) return;
151 if (!stmtIsNever(terminal)) problems.push(`${at(file, node)} if/else-if chain on ${keys[0]} does not end in assertNever`);
152}
154function checkFunction(file, fn) {
155 const counts = new Map();
156 let never = false;
157 const switched = new Set();
158 walkOwn(fn, node => {
159 const key = (node.type === 'IfStatement' || node.type === 'WhileStatement' || node.type === 'ConditionalExpression' || node.type === 'DoWhileStatement')
160 ? tagCompare(node.test) : null;
161 if (key) counts.set(key, (counts.get(key) || 0) + 1);
162 if (node.type === 'SwitchStatement') {
163 const p = propName(node.discriminant);
164 const disc = node.discriminant;
165 const obj = exprKey(typeOf(disc) === 'MemberExpression' ? disc.object : null);
166 if ((p === 'tag' || p === 'code') && obj !== null) switched.add(`${obj}.${p}`);
167 }
168 if (stmtIsNever(node) || (node.type === 'ReturnStatement' && isNeverCall(stmtValue(node)))) never = true;
169 if (node.type === 'CallExpression' && isNeverCall(node)) {
170 const arg = exprKey(node.arguments[0]);
171 if (arg) switched.add(arg);
172 }
173 });
174 if (never) return;
175 for (const [key, n] of counts) {
176 if (n >= 2 && !switched.has(key) && ![...switched].some(s => key.startsWith(`${s}.`) || s.startsWith(`${key}.`) || s === key.split('.').slice(0, -1).join('.'))) {
177 problems.push(`${file}:${lineOf(fn)} ${n} comparisons of ${key} and no assertNever`);
178 }
179 }
180}
182const parseOpts = { sourceType: 'module', plugins: ['typescript'], errorRecovery: false, tokens: false };
183for (const name of readdirSync('src').filter(f => f.endsWith('.ts')).sort()) {
184 const file = join('src', name);
185 let ast;
186 try { ast = parse(readFileSync(file, 'utf8'), parseOpts); }
187 catch (e) { problems.push(`${file}: parse: ${e.message}`); continue; }
188 walk(ast, null, (node, parent) => {
189 if (node.type === 'SwitchStatement') checkSwitch(file, node, parent);
190 if (node.type === 'IfStatement') checkElseIf(file, node, parent);
191 });
192 walk(ast, null, node => { if (isFn(node)) checkFunction(file, node); });
193}
195if (problems.length) {
196 console.error(`exhaustive:\n ${problems.join('\n ')}`);
197 process.exit(1);
198}
199console.log('exhaustive: switches and tag chains end in assertNever');
Diagnostics4 git calls · 37 ms
CallStatusSizeTime037 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=30 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2002.7 KiB · hit30 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2740 git.response.cache=HIT git.response.duration_ms=30 git.fetch.wants=7833fa51fa6d33d91ea67c63af6b19258826df53 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2740 git.pkt.payload.max=2719 git.sideband.band1.bytes=2718 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e1cc3cb9f0c6f8
- Request
- 47b61367…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format