@alloy-js/core 0.21.0-dev.0 → 0.21.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/src/components/Declaration.js +1 -1
  2. package/dist/src/components/Declaration.js.map +1 -1
  3. package/dist/src/components/MemberDeclaration.js +1 -1
  4. package/dist/src/components/MemberDeclaration.js.map +1 -1
  5. package/dist/src/name-policy.d.ts +2 -2
  6. package/dist/src/name-policy.d.ts.map +1 -1
  7. package/dist/src/name-policy.js +14 -1
  8. package/dist/src/name-policy.js.map +1 -1
  9. package/dist/src/pretty-string/pretty-string.d.ts +47 -0
  10. package/dist/src/pretty-string/pretty-string.d.ts.map +1 -0
  11. package/dist/src/pretty-string/pretty-string.js +100 -0
  12. package/dist/src/pretty-string/pretty-string.js.map +1 -0
  13. package/dist/src/pretty-string/pretty-string.test.d.ts +2 -0
  14. package/dist/src/pretty-string/pretty-string.test.d.ts.map +1 -0
  15. package/dist/src/pretty-string/pretty-string.test.js +38 -0
  16. package/dist/src/pretty-string/pretty-string.test.js.map +1 -0
  17. package/dist/src/refkey.d.ts +2 -0
  18. package/dist/src/refkey.d.ts.map +1 -1
  19. package/dist/src/refkey.js +7 -0
  20. package/dist/src/refkey.js.map +1 -1
  21. package/dist/src/symbols/output-scope.d.ts +1 -0
  22. package/dist/src/symbols/output-scope.d.ts.map +1 -1
  23. package/dist/src/symbols/output-scope.js +4 -0
  24. package/dist/src/symbols/output-scope.js.map +1 -1
  25. package/dist/src/symbols/output-symbol.d.ts +1 -0
  26. package/dist/src/symbols/output-symbol.d.ts.map +1 -1
  27. package/dist/src/symbols/output-symbol.js +3 -0
  28. package/dist/src/symbols/output-symbol.js.map +1 -1
  29. package/dist/src/tracer.d.ts.map +1 -1
  30. package/dist/src/tracer.js +2 -2
  31. package/dist/src/tracer.js.map +1 -1
  32. package/dist/tsconfig.tsbuildinfo +1 -1
  33. package/package.json +1 -1
  34. package/src/components/Declaration.tsx +1 -1
  35. package/src/components/MemberDeclaration.tsx +1 -1
  36. package/src/name-policy.ts +22 -3
  37. package/src/pretty-string/pretty-string.test.ts +47 -0
  38. package/src/pretty-string/pretty-string.ts +130 -0
  39. package/src/refkey.ts +13 -0
  40. package/src/symbols/output-scope.ts +7 -0
  41. package/src/symbols/output-symbol.ts +4 -0
  42. package/src/tracer.ts +2 -5
  43. package/temp/api.json +156 -2
@@ -6,22 +6,41 @@ export interface NamePolicy<TElements extends string> {
6
6
  * Apply the language policy to the provided name for the provided element
7
7
  * type.
8
8
  */
9
- getName(originalName: string, element: TElements): string;
9
+ getName(originalName: string, element: TElements | undefined): string;
10
10
  /**
11
11
  * Get a function that takes a name and applies the naming policy to it.
12
12
  */
13
- for(element: TElements): NamePolicyGetter;
13
+ for(element: TElements | undefined): NamePolicyGetter;
14
14
  }
15
15
 
16
16
  export function createNamePolicy<T extends string>(
17
17
  namer: (name: string, elements: T) => string,
18
18
  ): NamePolicy<T> {
19
+ const forCache = new Map<string, NamePolicyGetter>();
20
+ const noopGetter = (name: string) => name;
21
+
19
22
  return {
20
23
  getName(name, element) {
24
+ if (!element) {
25
+ return name;
26
+ }
27
+
21
28
  return namer(name, element);
22
29
  },
30
+
23
31
  for(element) {
24
- return (name) => namer(name, element);
32
+ if (!element) {
33
+ return noopGetter;
34
+ }
35
+
36
+ if (forCache.has(element)) {
37
+ return forCache.get(element)!;
38
+ }
39
+
40
+ const getter = (name: string) => namer(name, element);
41
+ forCache.set(element, getter);
42
+
43
+ return getter;
25
44
  },
26
45
  };
27
46
  }
@@ -0,0 +1,47 @@
1
+ import { expect, it } from "vitest";
2
+ import { pret, PrettyString } from "./pretty-string.js";
3
+
4
+ function expectRender(
5
+ pretty: PrettyString,
6
+ expected: {
7
+ toString: string;
8
+ toAnsi: string;
9
+ },
10
+ ) {
11
+ expect(pretty.toString()).toBe(expected.toString);
12
+ expect(pretty.toAnsi()).toBe(expected.toAnsi);
13
+ }
14
+
15
+ it("interpolate basic string", () => {
16
+ const result = pret`foo ${"bar"} baz`;
17
+ expectRender(result, {
18
+ toString: "foo bar baz",
19
+ toAnsi: "foo bar baz",
20
+ });
21
+ });
22
+
23
+ // cspell:ignore mbar
24
+ it("interpolate pretty segment", () => {
25
+ const result = pret`foo ${pret.red("bar")} baz`;
26
+ expectRender(result, {
27
+ toString: "foo bar baz",
28
+ toAnsi: "foo \x1b[31mbar\x1b[39m baz",
29
+ });
30
+ });
31
+
32
+ it("interpolate another pretty string with more formatting", () => {
33
+ const a = pret`foo ${pret.red("bar")} baz`;
34
+ const result = pret`Hi, ${pret.bgGreen(a)}`;
35
+ expectRender(result, {
36
+ toString: "Hi, foo bar baz",
37
+ toAnsi: "Hi, \x1b[42mfoo \x1b[31mbar\x1b[39m baz\x1b[49m",
38
+ });
39
+ });
40
+
41
+ it("use full rgb", () => {
42
+ const result = pret`foo ${pret.rgb(123, 45, 67, "bar")} baz`;
43
+ expectRender(result, {
44
+ toString: "foo bar baz",
45
+ toAnsi: "foo \x1b[38;2;123;45;67mbar\x1b[39m baz",
46
+ });
47
+ });
@@ -0,0 +1,130 @@
1
+ import { inspect } from "../inspect.js";
2
+
3
+ export function pret(
4
+ strings: TemplateStringsArray,
5
+ ...keys: PrettyStringSegment[]
6
+ ): PrettyString {
7
+ const result: PrettyStringSegment[] = [strings[0]];
8
+ keys.forEach((key, i) => {
9
+ result.push(key);
10
+ result.push(strings[i + 1]);
11
+ });
12
+
13
+ return new PrettyString(result);
14
+ }
15
+
16
+ pret.black = (x: PrettyStringSegment) => new PrettyStringColored(x, "black");
17
+ pret.red = (x: PrettyStringSegment) => new PrettyStringColored(x, "red");
18
+ pret.green = (x: PrettyStringSegment) => new PrettyStringColored(x, "green");
19
+ pret.yellow = (x: PrettyStringSegment) => new PrettyStringColored(x, "yellow");
20
+ pret.blue = (x: PrettyStringSegment) => new PrettyStringColored(x, "blue");
21
+ pret.magenta = (x: PrettyStringSegment) =>
22
+ new PrettyStringColored(x, "magenta");
23
+ pret.cyan = (x: PrettyStringSegment) => new PrettyStringColored(x, "cyan");
24
+ pret.white = (x: PrettyStringSegment) => new PrettyStringColored(x, "white");
25
+ pret.gray = (x: PrettyStringSegment) => new PrettyStringColored(x, "gray");
26
+
27
+ pret.bgBlack = (x: PrettyStringSegment) =>
28
+ new PrettyStringColored(x, "bgBlack");
29
+ pret.bgRed = (x: PrettyStringSegment) => new PrettyStringColored(x, "bgRed");
30
+ pret.bgGreen = (x: PrettyStringSegment) =>
31
+ new PrettyStringColored(x, "bgGreen");
32
+ pret.bgYellow = (x: PrettyStringSegment) =>
33
+ new PrettyStringColored(x, "bgYellow");
34
+ pret.bgBlue = (x: PrettyStringSegment) => new PrettyStringColored(x, "bgBlue");
35
+ pret.bgMagenta = (x: PrettyStringSegment) =>
36
+ new PrettyStringColored(x, "bgMagenta");
37
+ pret.bgCyan = (x: PrettyStringSegment) => new PrettyStringColored(x, "bgCyan");
38
+ pret.bgWhite = (x: PrettyStringSegment) =>
39
+ new PrettyStringColored(x, "bgWhite");
40
+
41
+ pret.rgb = (r: number, g: number, b: number, x: PrettyStringSegment) =>
42
+ new PrettyStringColored(x, { kind: "rgb", r, g, b });
43
+
44
+ const ansiColors = {
45
+ reset: ["\x1b[0m", "\x1b[0m"],
46
+ bold: ["\x1b[1m", "\x1b[22m"],
47
+ dim: ["\x1b[2m", "\x1b[22m"],
48
+ italic: ["\x1b[3m", "\x1b[23m"],
49
+ underline: ["\x1b[4m", "\x1b[24m"],
50
+ inverse: ["\x1b[7m", "\x1b[27m"],
51
+ hidden: ["\x1b[8m", "\x1b[28m"],
52
+ strikethrough: ["\x1b[9m", "\x1b[29m"],
53
+ black: ["\x1b[30m", "\x1b[39m"],
54
+ red: ["\x1b[31m", "\x1b[39m"],
55
+ green: ["\x1b[32m", "\x1b[39m"],
56
+ yellow: ["\x1b[33m", "\x1b[39m"],
57
+ blue: ["\x1b[34m", "\x1b[39m"],
58
+ magenta: ["\x1b[35m", "\x1b[39m"],
59
+ cyan: ["\x1b[36m", "\x1b[39m"],
60
+ white: ["\x1b[37m", "\x1b[39m"],
61
+ gray: ["\x1b[90m", "\x1b[39m"],
62
+ bgBlack: ["\x1b[40m", "\x1b[49m"],
63
+ bgRed: ["\x1b[41m", "\x1b[49m"],
64
+ bgGreen: ["\x1b[42m", "\x1b[49m"],
65
+ bgYellow: ["\x1b[43m", "\x1b[49m"],
66
+ bgBlue: ["\x1b[44m", "\x1b[49m"],
67
+ bgMagenta: ["\x1b[45m", "\x1b[49m"],
68
+ bgCyan: ["\x1b[46m", "\x1b[49m"],
69
+ bgWhite: ["\x1b[47m", "\x1b[49m"],
70
+ } as Record<string, [string, string]>;
71
+
72
+ export type PrettyStringSegment = string | PrettyStringColored | PrettyString;
73
+
74
+ export interface RgbColor {
75
+ kind: "rgb";
76
+ r: number;
77
+ g: number;
78
+ b: number;
79
+ }
80
+
81
+ export type ColorCodes = keyof typeof ansiColors;
82
+ export type Color = ColorCodes | RgbColor;
83
+ export class PrettyStringColored {
84
+ #value: PrettyStringSegment;
85
+ #color: Color;
86
+
87
+ constructor(value: PrettyStringSegment, color: Color) {
88
+ this.#value = value;
89
+ this.#color = color;
90
+ }
91
+
92
+ toString(): string {
93
+ return this.#value.toString();
94
+ }
95
+
96
+ toAnsi(): string {
97
+ if (typeof this.#color === "string") {
98
+ const [start, end] = ansiColors[this.#color];
99
+ return `${start}${typeof this.#value === "string" ? this.#value : this.#value.toAnsi()}${end}`;
100
+ }
101
+ const { r, g, b } = this.#color;
102
+ const start = `\x1b[38;2;${r};${g};${b}m`;
103
+ const end = `\x1b[39m`;
104
+ return `${start}${typeof this.#value === "string" ? this.#value : this.#value.toAnsi()}${end}`;
105
+ }
106
+ }
107
+
108
+ export class PrettyString {
109
+ #segments: PrettyStringSegment[];
110
+
111
+ constructor(segments: PrettyStringSegment[]) {
112
+ this.#segments = segments;
113
+ }
114
+
115
+ toString(): string {
116
+ return this.#segments
117
+ .map((s) => (typeof s === "string" ? s : s.toString()))
118
+ .join("");
119
+ }
120
+
121
+ toAnsi(): string {
122
+ return this.#segments
123
+ .map((s) => (typeof s === "string" ? s : s.toAnsi()))
124
+ .join("");
125
+ }
126
+
127
+ [inspect.custom]() {
128
+ return this.toAnsi();
129
+ }
130
+ }
package/src/refkey.ts CHANGED
@@ -192,3 +192,16 @@ export function memberRefkey(
192
192
  [RefkeySym]: true,
193
193
  };
194
194
  }
195
+
196
+ export function inspectRefkey(refkey: Refkey): string {
197
+ const text =
198
+ isMemberRefkey(refkey) ?
199
+ `memberRefkey[${inspectRefkey(refkey.base)} -> ${inspectRefkey(refkey.member)}]`
200
+ : `refkey[${refkey.key}]`;
201
+
202
+ return text;
203
+ }
204
+
205
+ export function unresolvedRefkey(refkey: Refkey): string {
206
+ return `<Unresolved Symbol: ${inspectRefkey(refkey)}>`;
207
+ }
@@ -254,4 +254,11 @@ export abstract class OutputScope {
254
254
  () => `${this.constructor.name} ${this.name}[${this.id}]${ownerSymbol}`,
255
255
  );
256
256
  }
257
+
258
+ toString() {
259
+ const ownerSymbol = this.ownerSymbol ? ` for ${this.ownerSymbol}` : "";
260
+ return untrack(
261
+ () => `${this.constructor.name} ${this.name}[${this.id}]${ownerSymbol}`,
262
+ );
263
+ }
257
264
  }
@@ -630,6 +630,10 @@ export abstract class OutputSymbol {
630
630
  }
631
631
 
632
632
  [inspect.custom]() {
633
+ return this.toString();
634
+ }
635
+
636
+ toString() {
633
637
  return untrack(() => `${this.constructor.name} "${this.name}"[${this.id}]`);
634
638
  }
635
639
  }
package/src/tracer.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { effect, ReactiveEffectRunner } from "@vue/reactivity";
2
2
  import { untrack } from "./reactivity.js";
3
- import { isMemberRefkey, type Refkey } from "./refkey.js";
3
+ import { inspectRefkey, type Refkey } from "./refkey.js";
4
4
  import { scheduler } from "./scheduler.js";
5
5
  import { type OutputScope } from "./symbols/output-scope.js";
6
6
  import type {
@@ -429,10 +429,7 @@ export function formatRefkeys(refkeys: Refkey[] | Refkey | undefined) {
429
429
  }
430
430
 
431
431
  function formatRefkey(refkey: Refkey): string {
432
- const text =
433
- isMemberRefkey(refkey) ?
434
- `memberRefkey[${formatRefkey(refkey.base)} -> ${formatRefkey(refkey.member)}]`
435
- : `refkey[${refkey.key}]`;
432
+ const text = inspectRefkey(refkey);
436
433
 
437
434
  return colorText(text, {
438
435
  fg: {
package/temp/api.json CHANGED
@@ -7668,6 +7668,52 @@
7668
7668
  ],
7669
7669
  "extendsTokenRanges": []
7670
7670
  },
7671
+ {
7672
+ "kind": "Function",
7673
+ "canonicalReference": "@alloy-js/core!inspectRefkey:function(1)",
7674
+ "docComment": "",
7675
+ "excerptTokens": [
7676
+ {
7677
+ "kind": "Content",
7678
+ "text": "export declare function inspectRefkey(refkey: "
7679
+ },
7680
+ {
7681
+ "kind": "Reference",
7682
+ "text": "Refkey",
7683
+ "canonicalReference": "@alloy-js/core!Refkey:type"
7684
+ },
7685
+ {
7686
+ "kind": "Content",
7687
+ "text": "): "
7688
+ },
7689
+ {
7690
+ "kind": "Content",
7691
+ "text": "string"
7692
+ },
7693
+ {
7694
+ "kind": "Content",
7695
+ "text": ";"
7696
+ }
7697
+ ],
7698
+ "fileUrlPath": "src/refkey.ts",
7699
+ "returnTypeTokenRange": {
7700
+ "startIndex": 3,
7701
+ "endIndex": 4
7702
+ },
7703
+ "releaseTag": "Public",
7704
+ "overloadIndex": 1,
7705
+ "parameters": [
7706
+ {
7707
+ "parameterName": "refkey",
7708
+ "parameterTypeTokenRange": {
7709
+ "startIndex": 1,
7710
+ "endIndex": 2
7711
+ },
7712
+ "isOptional": false
7713
+ }
7714
+ ],
7715
+ "name": "inspectRefkey"
7716
+ },
7671
7717
  {
7672
7718
  "kind": "Function",
7673
7719
  "canonicalReference": "@alloy-js/core!instantiateTakenMembersTo:function(1)",
@@ -12491,7 +12537,7 @@
12491
12537
  },
12492
12538
  {
12493
12539
  "kind": "Content",
12494
- "text": "TElements"
12540
+ "text": "TElements | undefined"
12495
12541
  },
12496
12542
  {
12497
12543
  "kind": "Content",
@@ -12545,7 +12591,7 @@
12545
12591
  },
12546
12592
  {
12547
12593
  "kind": "Content",
12548
- "text": "TElements"
12594
+ "text": "TElements | undefined"
12549
12595
  },
12550
12596
  {
12551
12597
  "kind": "Content",
@@ -14200,6 +14246,37 @@
14200
14246
  "isStatic": false,
14201
14247
  "isProtected": false,
14202
14248
  "isAbstract": false
14249
+ },
14250
+ {
14251
+ "kind": "Method",
14252
+ "canonicalReference": "@alloy-js/core!OutputScope#toString:member(1)",
14253
+ "docComment": "",
14254
+ "excerptTokens": [
14255
+ {
14256
+ "kind": "Content",
14257
+ "text": "toString(): "
14258
+ },
14259
+ {
14260
+ "kind": "Content",
14261
+ "text": "string"
14262
+ },
14263
+ {
14264
+ "kind": "Content",
14265
+ "text": ";"
14266
+ }
14267
+ ],
14268
+ "isStatic": false,
14269
+ "returnTypeTokenRange": {
14270
+ "startIndex": 1,
14271
+ "endIndex": 2
14272
+ },
14273
+ "releaseTag": "Public",
14274
+ "isProtected": false,
14275
+ "overloadIndex": 1,
14276
+ "parameters": [],
14277
+ "isOptional": false,
14278
+ "isAbstract": false,
14279
+ "name": "toString"
14203
14280
  }
14204
14281
  ],
14205
14282
  "implementsTokenRanges": []
@@ -15674,6 +15751,37 @@
15674
15751
  "isProtected": false,
15675
15752
  "isAbstract": false
15676
15753
  },
15754
+ {
15755
+ "kind": "Method",
15756
+ "canonicalReference": "@alloy-js/core!OutputSymbol#toString:member(1)",
15757
+ "docComment": "",
15758
+ "excerptTokens": [
15759
+ {
15760
+ "kind": "Content",
15761
+ "text": "toString(): "
15762
+ },
15763
+ {
15764
+ "kind": "Content",
15765
+ "text": "string"
15766
+ },
15767
+ {
15768
+ "kind": "Content",
15769
+ "text": ";"
15770
+ }
15771
+ ],
15772
+ "isStatic": false,
15773
+ "returnTypeTokenRange": {
15774
+ "startIndex": 1,
15775
+ "endIndex": 2
15776
+ },
15777
+ "releaseTag": "Public",
15778
+ "isProtected": false,
15779
+ "overloadIndex": 1,
15780
+ "parameters": [],
15781
+ "isOptional": false,
15782
+ "isAbstract": false,
15783
+ "name": "toString"
15784
+ },
15677
15785
  {
15678
15786
  "kind": "Property",
15679
15787
  "canonicalReference": "@alloy-js/core!OutputSymbol#type:member",
@@ -23171,6 +23279,52 @@
23171
23279
  ],
23172
23280
  "name": "traverseOutput"
23173
23281
  },
23282
+ {
23283
+ "kind": "Function",
23284
+ "canonicalReference": "@alloy-js/core!unresolvedRefkey:function(1)",
23285
+ "docComment": "",
23286
+ "excerptTokens": [
23287
+ {
23288
+ "kind": "Content",
23289
+ "text": "export declare function unresolvedRefkey(refkey: "
23290
+ },
23291
+ {
23292
+ "kind": "Reference",
23293
+ "text": "Refkey",
23294
+ "canonicalReference": "@alloy-js/core!Refkey:type"
23295
+ },
23296
+ {
23297
+ "kind": "Content",
23298
+ "text": "): "
23299
+ },
23300
+ {
23301
+ "kind": "Content",
23302
+ "text": "string"
23303
+ },
23304
+ {
23305
+ "kind": "Content",
23306
+ "text": ";"
23307
+ }
23308
+ ],
23309
+ "fileUrlPath": "src/refkey.ts",
23310
+ "returnTypeTokenRange": {
23311
+ "startIndex": 3,
23312
+ "endIndex": 4
23313
+ },
23314
+ "releaseTag": "Public",
23315
+ "overloadIndex": 1,
23316
+ "parameters": [
23317
+ {
23318
+ "parameterName": "refkey",
23319
+ "parameterTypeTokenRange": {
23320
+ "startIndex": 1,
23321
+ "endIndex": 2
23322
+ },
23323
+ "isOptional": false
23324
+ }
23325
+ ],
23326
+ "name": "unresolvedRefkey"
23327
+ },
23174
23328
  {
23175
23329
  "kind": "Function",
23176
23330
  "canonicalReference": "@alloy-js/core!untrack:function(1)",