GitHubivarvong

ply

main
ply6 git calls268 ms21 KiB3 cachedprotocol v2
199 lines8.7 KiBRaw
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 }
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);
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 }
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`);
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 }
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); });
195if (problems.length) {
196 console.error(`exhaustive:\n ${problems.join('\n ')}`);
197 process.exit(1);
199console.log('exhaustive: switches and tag chains end in assertNever');
Diagnostics6 git calls · 268 ms
CallStatusSizeTime0268 ms
discover200191 B3 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=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB3 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=11990
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
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=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=10
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:0200831 B · hit10 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=249
git.response.bytes=831
git.response.cache=HIT
git.response.duration_ms=10
git.fetch.filter=tree:0
git.fetch.wants=b9290e370bd091fd3272931655a175dd1a7af82d
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=831
git.pkt.payload.max=804
git.sideband.band1.bytes=804
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=804
git.pack.trailer=81ddc1f73214935b4ee953ad84f959db955ce0d1
Open pack map
fetch2002.7 KiB249 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2754
git.response.cache=MISS
git.response.duration_ms=249
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2754
git.pkt.payload.max=2727
git.sideband.band1.bytes=2727
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://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e2cb30d8df7929
Request
63a34d2a…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format