-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
441 lines (392 loc) · 21.8 KB
/
index.html
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<!DOCTYPE HTML>
<html>
<head>
<title>Code Selectors - convention for referencing parts of code</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="index.css">
<link rel="stylesheet" type="text/css" href="assets/prismjs.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon" sizes="180x180" href="/assets/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon-16x16.png">
<link rel="manifest" href="/assets/site.webmanifest">
<link rel="mask-icon" href="/assets/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<header>
<div class="header-container">
<h1><img class="logo" src="/assets/android-chrome-192x192.png" /> <strong>Code Selectors</strong></h1>
<i>a convention for referencing parts of code</i>
</div>
</header>
<main>
<div class="markdown-body">
<h1 id="code-selectors">Code Selectors</h1>
<blockquote>
<p>Convention for referencing pieces of code</p>
</blockquote>
<p>⚡️ <strong><em>It's like CSS selectors, but for code!</em></strong></p>
<p>Have you ever wanted a consistent and concise way of referencing a specific part of a code-base?</p>
<p>For example, suppose you wanted to refer to a function named <code>login</code> or a constant named <code>color</code>. How would you reference them, apart from using a line number (e.g. myfile.ts:23) or plain English (e.g. "the login function in myfile.ts")?</p>
<p>And how would you deal with more complex nested structures, such as <code>r</code>, <code>g</code> and <code>b</code> fields inside an object assigned to a <code>color</code> constant, or an anonymous callback passed to a <code>then</code> call on a Promise?</p>
<p>Code selectors to the rescue!</p>
<p>Code selectors aim to provide a consistent and concise language allowing you to reference any structure in any code file.</p>
<p>With code selectors, you can now write something like: <code>myfile.ts/login</code> to reference your login function or <code>myfile.ts/colors.r</code> to reference the <code>r</code> field in your <code>color</code> constant.</p>
<h2 id="interactive-demo">Interactive demo</h2>
<p>
<style>
code.language-tsx {
position: relative;
}
ol.hotspot-legend {
counter-reset:li;
margin: 1rem 1rem 2rem 1rem;
padding-left:0;
}
ol.hotspot-legend > li {
position: relative;
padding: 0.5 1rem;
list-style: none;
}
ol.hotspot-legend > li:before {
content:counter(li); /* Use the counter as content */
counter-increment:li; /* Increment the counter by 1 */
display: inline-block;
text-align: center;
line-height: 0.95rem;
width: 1rem;
height: 1rem;
border-radius: 100%;
color: white;
background-color: black;
font-size: 0.7rem;
margin-top: -0.5rem;
}
ol.hotspot-legend > li code {
margin-left: 0.5rem;
}
.hotspot-number {
position: absolute;
display: inline-block;
text-align: center;
line-height: 0.95rem;
width: 1rem;
height: 1rem;
border-radius: 100%;
color: white;
background-color: black;
font-size: 0.7rem;
margin-top: -0.5rem;
}
.token.hotspot {
outline: dashed 1px gray;
padding: 0 0.125rem;
border-radius: 0.125rem;
cursor: pointer;
}
.token.hotspot:hover,
.token.hotspot:focus {
outline-color: black;
}
.token:focus:after,
.token:hover:after {
position: absolute;
margin-top: 1rem;
display: inline-block;
height: 1.5rem;
border: solid 1px black;
background-color: white;
border-radius: 0.5rem;
padding: 0.5rem 0.75rem;
box-shadow: silver 0 0 1rem;
}
.token.variable.Counter:focus:after,
.token.variable.Counter:hover:after {
content: 'counter.tsx/Counter';
}
.token.count:focus:after,
.token.count:hover:after {
content: 'counter.tsx/Counter/count';
}
.token.function.useState:focus:after,
.token.function.useState:hover:after {
content: 'counter.tsx/Counter/useState()@1';
}
.token.function.useEffect:focus:after,
.token.function.useEffect:hover:after {
content: 'counter.tsx/Counter/useEffect()@1';
}
.token.variable.interval:focus:after,
.token.variable.interval:hover:after {
content: 'counter.tsx/Counter/useEffect()@1/interval';
}
</style>
<p>Click or tap on any part of this code to see a code selector that targets it.</p>
<pre class="language-tsx" tabindex="0"><code class="language-tsx"><span class="token keyword">export</span> <span class="token keyword">const</span> <span class="token hotspot variable Counter" tabindex="0">Counter</span><b class="hotspot-number">1</b><span class="token operator">:</span> <span class="token function-variable function">VFC</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span><span class="token hotspot variable count" tabindex="0">count</span><b class="hotspot-number">2</b><span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token hotspot function useState" tabindex="0">useState</span><b class="hotspot-number">3</b><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>overLimit<span class="token punctuation">,</span> setOverLimit<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">useState</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token hotspot function useEffect" tabindex="0">useEffect</span><b class="hotspot-number">4</b><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token hotspot variable interval" tabindex="0">interval</span><b class="hotspot-number">5</b> <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token function">setCount</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>count <span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">setOverLimit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1_000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">clearInterval</span><span class="token punctuation">(</span>interval<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre>
<ol class="hotspot-legend">
<li><code>counter.tsx/Counter</code></li>
<li><code>counter.tsx/Counter/count</code></li>
<li><code>counter.tsx/Counter/useState()@1</code></li>
<li><code>counter.tsx/Counter/useEffect()@1</code></li>
<li><code>counter.tsx/Counter/useEffect()@1/interval</code></li>
</ol>
</p>
<h2 id="characteristics-of-code-selectors">Characteristics of code selectors</h2>
<h3 id="_concise_"><strong><em>Concise</em></strong></h3>
<p>Code selectors take as little space as possible. Brevity makes the selectors faster to type, easier to read and more convenient to copy/paste or add to links.</p>
<h3 id="_readable_"><strong><em>Readable</em></strong></h3>
<p>Code selectors use the real names of structures as much as possible, to optimise for readability.</p>
<p>Where names are not available, 1-based indeces are used, referencing the <em>n</em>th of the class of object being described. For example a reference to the 2nd parameter of a function call uses the index 2, even if it is preceded by previous function calls with their own parameters.</p>
<h3 id="_composable_"><strong><em>Composable</em></strong></h3>
<p>Complex references can be be built up from simpler, interchangeable parts.</p>
<p>For example, to refer to a constant in a function in a field on an object, we can compose "closure", "object field" and "structure" together, to create one complete reference.</p>
<h2 id="parts-of-code-selectors">Parts of code selectors</h2>
<ul>
<li><a href="#structure">Structure</a></li>
<li><a href="#anonymous-structure">Anonymous structure</a></li>
<li><a href="#object-field">Object field</a></li>
<li><a href="#array-element">Array element</a></li>
<li><a href="#function-call">Function call</a></li>
<li><a href="#function-call-argument">Function call argument</a></li>
<li><a href="#closure">Closure</a></li>
</ul>
<h3 id="structure">Structure</h3>
<p>Any named structure (function, constant, etc) declared in the main body of a file can be referenced by name of the file, followed by a "contains" accessor - <code>/</code>, followed by the name of the structure.</p>
<blockquote>
<p>[file.ext] / <strong><code>name</code></strong></p>
</blockquote>
<ul>
<li><strong><code>name</code></strong> is the name of the structure</li>
</ul>
<p>For example, to reference the function <code>bar</code> in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
function foo() {}
// * reference this
function bar() {}
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/bar</code></p>
</blockquote>
<hr />
<h3 id="anonymous-structure">Anonymous structure</h3>
<p>Not all structures have names. We might declare a self-calling function (IFFE) or immediately loop over an array declared inline. We can reference such a structure by its type, followed by an "indexing" accessor, followed by a numeric, 1-based index indicating the order of that structure within the set of all structures of the same type.</p>
<blockquote>
<p>[file.ext] / <strong><code>(type)</code></strong>@<strong><code>(index)</code></strong></p>
</blockquote>
<ul>
<li><strong><code>type</code></strong> is one of the following:</li>
<li><code>func</code> - function</li>
<li><code>arr</code> - array</li>
<li><code>obj</code> - object</li>
<li><code>clo</code> - anonymous closure</li>
<li><strong><code>index</code></strong> is an order index, 1 or greater.</li>
</ul>
<p>For example, to reference the second self-calling function declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
(function () {})();
// * reference this
(function () {})();
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/func@2</code></p>
</blockquote>
<p>Or, to reference the second array declared inline in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
(function () {})();
[1, 2, 3].map(console.log);
[4, 5, 6].map(console.log); // * reference this
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/arr@2</code></p>
</blockquote>
<hr />
<h3 id="object-field">Object field</h3>
<p>Any structure which is declared or set as a field on a containing object can be referenced by referencing the containing structure, followed by the "object field" accessor - <code>.</code>, followed by the name of the field itself.</p>
<blockquote>
<p>[file.ext] / <strong><code>(object)</code></strong>.<strong><code>(field)</code></strong></p>
</blockquote>
<ul>
<li><p><strong><code>object</code></strong> is the <em>name</em> of the containing structure</p></li>
<li><p><strong><code>field</code></strong> is the name of the contained field</p></li>
</ul>
<p>For example, to reference the field <code>bar</code>, declared in the object <code>foo</code>, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
const foo = {
bar: "1", // * reference this
};
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/foo.bar</code></p>
</blockquote>
<p>Or, to reference the field <code>bar</code>, set as a field on the object <code>foo</code>, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
const foo = {};
foo.bar = "1"; // * reference this
</code></pre>
<p>You can use the same selector as above:</p>
<blockquote>
<p><code>myfile.ts/foo.bar</code></p>
</blockquote>
<hr />
<h3 id="array-element">Array element</h3>
<p>Any structure which is declared as an element on a containing array can be referenced by referencing the containing structure, followed by an "array index" accessor and the index of the item - <code>[n]</code>.</p>
<blockquote>
<p>[file.ext] / <strong><code>(array)</code></strong> [<strong><code>(index)</code></strong>]</p>
</blockquote>
<ul>
<li><p><strong><code>array</code></strong> is the <em>name</em> of the containing structure</p></li>
<li><p><strong><code>index</code></strong> is the <em>index</em> of the contained element</p></li>
</ul>
<p>For example, to reference the string <code>bar</code>, held in the array <code>foo</code> at index 1, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
const foo = [
"baz",
"bar", // * reference this
];
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/foo[1]</code></p>
</blockquote>
<p>Or, to reference the string <code>bar</code>, set at index 1 of the array <code>foo</code>, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
const foo = ["baz"];
foo[1] = "bar"; // * reference this
</code></pre>
<p>You can use the same selector as above:</p>
<blockquote>
<p><code>myfile.ts/foo[1]</code></p>
</blockquote>
<hr />
<h3 id="function-call">Function call</h3>
<p>Function calls are one of the most common kinds of expressions in any Javascript/Typescript project. We can reference a call by referencing the structure being called, followed by an indicator that it is being called - <code>()</code>.</p>
<blockquote>
<p>[file.ext] / <strong><code>(call)</code></strong><code>()</code></p>
</blockquote>
<p>For example, to reference the call to the imported <code>foo</code> function, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
import { foo } from "foo.utils";
foo("hello");
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/foo()</code></p>
</blockquote>
<hr />
<h3 id="function-call-argument">Function call argument</h3>
<p>Suppose you want to reference an argument being passed to a parameter of a function call. This can be done by referencing the function call, followed by a "containment" indicator - simply <code>/</code>, followed by a numeric 1-based index, indicating the parameter's order within the function's parameter list.</p>
<blockquote>
<p>[file.ext] / <strong><code>(call)</code></strong> / @<strong><code>(index)</code></strong></p>
</blockquote>
<p>For example, to reference the anonymous function passed to the 2nd parameter to the imported <code>foo</code> function, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
import { foo } from "foo.utils";
foo("hello", () => "world");
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/foo()/@2</code></p>
</blockquote>
<hr />
<h3 id="closure">Closure</h3>
<p>Many of the structures that make up a Javascript/Typescript project are actually found inside a closure of some kind, rather than declared in the top-level scope. We can reference an entity declared inside of a closure by simply referencing that closure, followed by a "containment" indicator - simply <code>/</code>, followed by the structure itself.</p>
<blockquote>
<p>[file.ext] / <strong><code>(closure)</code></strong> / <strong><code>(structure)</code></strong></p>
</blockquote>
<p>For example, to reference the constant <code>bar</code> declared inside the top-level function <code>foo</code>, declared in file <code>myfile.ts</code>:</p>
<pre><code class="ts language-ts">// myfile.ts
function foo() {
const bar = "hello!"; // * reference this
}
</code></pre>
<p>You can use this selector:</p>
<blockquote>
<p><code>myfile.ts/foo/bar</code></p>
</blockquote>
<hr />
<h2 id="complex-examples">Complex examples</h2>
<h3 id="nodejsexpress-login-request-handler">NodeJS/Express login request handler</h3>
<pre><code class="ts language-ts">import express from "express";
import { getUser } from "users";
const app = express();
app.post("/login", async (req, res) => {
const user = await getUser(req.params.username, req.params.password);
if (user) {
res.send(200);
} else {
res.send(401);
}
});
</code></pre>
<ol>
<li>The <code>app.post</code> call: <code>index.ts/app.post()</code></li>
<li>The post handler: <code>index.ts/app.post()/@1</code></li>
<li>The call to <code>getUser</code>: <code>index.ts/app.post()/@1/getUser()</code></li>
<li>The constant <code>user</code>: <code>index.ts/app.post()/@1/user</code></li>
<li>The call to <code>res.send</code> with <code>200</code> passed: <code>index.ts/app.post()/@1/res.send()@1</code></li>
<li>The call to <code>res.send</code> with <code>401</code> passed: <code>index.ts/app.post()/@1/res.send()@2</code></li>
</ol>
<h3 id="reactjs-counter-component">ReactJS Counter component</h3>
<pre><code class="tsx language-tsx">export const Counter: VFC = () => {
const [count, setCount] = useState(0);
const [overLimit, setOverLimit] = useState(false);
useEffect(() => {
const interval = setInterval(() => {
setCount(() => count + 1);
if (count > 10) {
setOverLimit(() => true);
}
}, 1_000);
return () => clearInterval(interval);
});
return <span>{count}</span>;
};
</code></pre>
<ol>
<li>The <code>Counter</code> itself: <code>myfile.ts/Counter</code></li>
<li>The first call to <code>useState</code>: <code>myfile.ts/Counter/useState()@1</code></li>
<li>The destructured constant <code>count</code>: <code>myfile.ts/Counter/count</code></li>
<li>The destructured function <code>setCount</code>: <code>myfile.ts/Counter/setCount</code></li>
<li>The second call to <code>useState</code>: <code>myfile.ts/Counter/useState()@2</code></li>
<li>The call to <code>useEffect</code>: <code>myfile.ts/Counter/useEffect()</code></li>
<li>The function passed to <code>useEffect</code>: <code>myfile.ts/Counter/useEffect()/@1</code></li>
<li>The call to <code>setInterval</code>: <code>myfile.ts/Counter/useEffect()/@1/setInterval()</code></li>
<li>The function passed to <code>setInterval</code>: <code>myfile.ts/Counter/useEffect()/@1/setInterval()/@1</code></li>
<li>The call to <code>setCount</code>: <code>myfile.ts/Counter/useEffect()/@1/setInterval()/@1/setCount()</code></li>
<li>The function passed to <code>setCount</code>: <code>myfile.ts/Counter/useEffect()/@1/setInterval()/@1/setCount()/@1</code></li>
<li>The constant <code>interval</code>: <code>myfile.ts/Counter/useEffect()/@1/interval</code></li>
<li>The function returned by function passed to <code>useEffect</code>: <code>myfile.ts/Counter/useEffect()/@1/func@2</code></li>
</ol>
</div>
</main>
<footer>
<a class="license-badge" href="http://creativecommons.org/licenses/by-sa/3.0/">
<img alt="Creative Commons License" class="license-badge" src="https://jsdoc.app/images/cc-by-sa.svg" width="80" height="15">
</a>
<br> Copyright © 2023 the
<a href="https://github.com/jonathanconway/htmldoc/contributors">contributors</a> to the HTMLDoc documentation project.
<br> This website is <a href="https://github.com/jonathanconway/htmldoc">open source</a> and is licensed under the <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
</footer>
</body>
</html>