@fedify/vocab-tools 2.1.0-dev.405 → 2.1.0-dev.408

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.
package/src/class.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { generateDecoder, generateEncoder } from "./codec.ts";
2
2
  import { generateCloner, generateConstructor } from "./constructor.ts";
3
3
  import { generateFields } from "./field.ts";
4
- import { generateInspector } from "./inspector.ts";
4
+ import { generateInspector, generateInspectorPostClass } from "./inspector.ts";
5
5
  import { generateProperties } from "./property.ts";
6
6
  import type { TypeSchema } from "./schema.ts";
7
7
  import { emitOverride } from "./type.ts";
@@ -104,6 +104,9 @@ async function* generateClass(
104
104
  for await (const code of generateDecoder(typeUri, types)) yield code;
105
105
  for await (const code of generateInspector(typeUri, types)) yield code;
106
106
  yield "}\n\n";
107
+ for await (const code of generateInspectorPostClass(typeUri, types)) {
108
+ yield code;
109
+ }
107
110
  }
108
111
 
109
112
  /**
package/src/codec.ts CHANGED
@@ -125,7 +125,11 @@ export async function* generateEncoder(
125
125
  `;
126
126
  }
127
127
  yield `
128
- result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};
128
+ ${
129
+ type.typeless
130
+ ? ""
131
+ : `result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};`
132
+ }
129
133
  if (this.id != null) result["id"] = this.id.href;
130
134
  result["@context"] = ${JSON.stringify(type.defaultContext)};
131
135
  return result;
@@ -197,7 +201,7 @@ export async function* generateEncoder(
197
201
  `;
198
202
  }
199
203
  yield `
200
- values["@type"] = [${JSON.stringify(type.uri)}];
204
+ ${type.typeless ? "" : `values["@type"] = [${JSON.stringify(type.uri)}];`}
201
205
  if (this.id != null) values["@id"] = this.id.href;
202
206
  if (options.format === "expand") {
203
207
  return await jsonld.expand(
package/src/generate.ts CHANGED
@@ -5,7 +5,7 @@ import { loadSchemaFiles } from "./schema.ts";
5
5
  export default async function generateVocab(
6
6
  schemaDir: string,
7
7
  generatedPath: string,
8
- ) {
8
+ ): Promise<void> {
9
9
  const types = await loadSchemaFiles(schemaDir);
10
10
  const encoder = new TextEncoder();
11
11
 
package/src/inspector.ts CHANGED
@@ -73,24 +73,44 @@ export async function* generateInspector(
73
73
  yield `
74
74
  return proxy;
75
75
  }
76
+ `;
77
+ }
76
78
 
77
- // @ts-ignore: suppressing TS4127
78
- ${emitOverride(typeUri, types)} [Symbol.for("Deno.customInspect")](
79
+ /**
80
+ * Generates code that must appear *after* the class closing brace: prototype
81
+ * assignments for the Deno and Node.js custom-inspect hooks.
82
+ *
83
+ * These are emitted outside the class body because computed property names
84
+ * using `Symbol.for()` are incompatible with `isolatedDeclarations` mode.
85
+ */
86
+ export async function* generateInspectorPostClass(
87
+ typeUri: string,
88
+ types: Record<string, TypeSchema>,
89
+ ): AsyncIterable<string> {
90
+ const type = types[typeUri];
91
+ const className = type.name;
92
+ yield `
93
+ // deno-lint-ignore no-explicit-any
94
+ (${className}.prototype as any)[Symbol.for("Deno.customInspect")] =
95
+ function (
96
+ this: ${className},
79
97
  inspect: typeof Deno.inspect,
80
98
  options: Deno.InspectOptions,
81
99
  ): string {
82
100
  const proxy = this._getCustomInspectProxy();
83
101
  return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
84
- }
102
+ };
85
103
 
86
- // @ts-ignore: suppressing TS4127
87
- ${emitOverride(typeUri, types)} [Symbol.for("nodejs.util.inspect.custom")](
104
+ // deno-lint-ignore no-explicit-any
105
+ (${className}.prototype as any)[Symbol.for("nodejs.util.inspect.custom")] =
106
+ function (
107
+ this: ${className},
88
108
  _depth: number,
89
109
  options: unknown,
90
110
  inspect: (value: unknown, options: unknown) => string,
91
111
  ): string {
92
112
  const proxy = this._getCustomInspectProxy();
93
113
  return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
94
- }
114
+ };
95
115
  `;
96
116
  }
package/src/schema.ts CHANGED
@@ -47,6 +47,17 @@ export interface TypeSchema {
47
47
  */
48
48
  entity: boolean;
49
49
 
50
+ /**
51
+ * Whether the type omits `@type` in JSON-LD serialization. When `true`,
52
+ * the generated `toJsonLd()` method will not emit `@type` (or `type` in
53
+ * compact form) in the serialized JSON-LD. The generated `fromJsonLd()`
54
+ * method will still accept `@type` if present for backward compatibility.
55
+ *
56
+ * This is useful for types that are not real vocabulary types but rather
57
+ * anonymous object structures (e.g., `Endpoints`, `Source` in ActivityStreams).
58
+ */
59
+ typeless?: boolean;
60
+
50
61
  /**
51
62
  * The description of the type. It is used as the doc comment of
52
63
  * the generated class.
package/src/schema.yaml CHANGED
@@ -223,6 +223,17 @@ properties:
223
223
 
224
224
  The extended subtypes must have the consistent value of this flag.
225
225
  type: boolean
226
+ typeless:
227
+ description: >-
228
+ Whether the type omits @type in JSON-LD serialization. When true,
229
+ the generated toJsonLd() method will not emit @type (or type in
230
+ compact form) in the serialized JSON-LD. The generated fromJsonLd()
231
+ method will still accept @type if present for backward compatibility.
232
+
233
+ This is useful for types that are not real vocabulary types but rather
234
+ anonymous object structures (e.g., Endpoints, Source in
235
+ ActivityStreams).
236
+ type: boolean
226
237
  description:
227
238
  description: >-
228
239
  The description of the type. It is used as the doc comment of
package/tsdown.config.ts CHANGED
@@ -4,7 +4,7 @@ import { defineConfig } from "tsdown";
4
4
 
5
5
  export default defineConfig({
6
6
  entry: ["src/mod.ts"],
7
- dts: true,
7
+ dts: { compilerOptions: { isolatedDeclarations: true, declaration: true } },
8
8
  format: ["esm", "cjs"],
9
9
  platform: "neutral",
10
10
  external: [/^node:/],