@tsplc/typegen 0.5.16 → 0.5.18
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/dist/cli.js +22 -15
- package/dist/cli.js.map +6 -5
- package/dist/codegen.js +17 -10
- package/dist/codegen.js.map +5 -4
- package/dist/hardware.js +13 -6
- package/dist/hardware.js.map +5 -4
- package/dist/index.js +23 -15
- package/dist/index.js.map +7 -6
- package/package.json +3 -3
- package/src/codegen/generate.ts +14 -9
- package/src/hardware/mapping-types.ts +3 -2
- package/src/index.ts +1 -0
- package/src/public-tag.ts +8 -0
package/src/codegen/generate.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
} from "@tsplc/iec";
|
|
11
11
|
import { identifierPattern } from "@tsplc/iec/st";
|
|
12
12
|
import { stFiles } from "../files";
|
|
13
|
+
import { publicTag } from "../public-tag";
|
|
13
14
|
import { isIntegerConstant } from "./constants";
|
|
14
15
|
import {
|
|
15
16
|
type Declaration,
|
|
@@ -81,6 +82,10 @@ const documentation = (value: string) =>
|
|
|
81
82
|
? `/** ${value.replace(/\*\//g, "* /").replace(/\r?\n/g, "\n * ")} */\n`
|
|
82
83
|
: "";
|
|
83
84
|
|
|
85
|
+
/** An export's documentation: its description, and the tag that keeps it out of unused-export reports. */
|
|
86
|
+
const exported = (description: string) =>
|
|
87
|
+
documentation(description ? `${description}\n${publicTag}` : publicTag);
|
|
88
|
+
|
|
84
89
|
function assertUniqueNames(items: readonly { name: string; source: string }[]) {
|
|
85
90
|
const seen = new Map<string, string>();
|
|
86
91
|
for (const { name, source } of items) {
|
|
@@ -123,7 +128,7 @@ export async function generateTypes(options: GenerateOptions): Promise<string> {
|
|
|
123
128
|
},
|
|
124
129
|
);
|
|
125
130
|
return publication
|
|
126
|
-
? `${contract}\
|
|
131
|
+
? `${contract}\n${exported("")}export const ${contractNames.publication} = ${JSON.stringify(publication, null, 2)} as const;\n`
|
|
127
132
|
: contract;
|
|
128
133
|
}
|
|
129
134
|
|
|
@@ -326,11 +331,11 @@ function emit(
|
|
|
326
331
|
const declare = (text: string) => (emitDeclarations ? [text] : []);
|
|
327
332
|
const output = [
|
|
328
333
|
"// Generated by @tsplc/typegen. Edit the PLC sources and regenerate.\n",
|
|
329
|
-
|
|
334
|
+
`${exported("")}export interface ${contractNames.field} { field: string; type: string; description: string; }\n`,
|
|
330
335
|
];
|
|
331
336
|
for (const [index, type] of ordered.entries()) {
|
|
332
337
|
try {
|
|
333
|
-
const doc =
|
|
338
|
+
const doc = exported(type.description);
|
|
334
339
|
if (type.kind === "struct") {
|
|
335
340
|
output.push(
|
|
336
341
|
...declare(
|
|
@@ -356,7 +361,7 @@ function emit(
|
|
|
356
361
|
const members = enumMembers(type);
|
|
357
362
|
output.push(
|
|
358
363
|
...declare(
|
|
359
|
-
`${doc}export const ${type.name} = { ${members.map(({ name, value }) => `${quote(name)}: ${value}`).join(", ")} } as const;\
|
|
364
|
+
`${doc}export const ${type.name} = { ${members.map(({ name, value }) => `${quote(name)}: ${value}`).join(", ")} } as const;\n${exported("")}export type ${type.name} = (typeof ${type.name})[keyof typeof ${type.name}];\n`,
|
|
360
365
|
),
|
|
361
366
|
);
|
|
362
367
|
output.push(
|
|
@@ -385,7 +390,7 @@ function emit(
|
|
|
385
390
|
]),
|
|
386
391
|
);
|
|
387
392
|
output.push(
|
|
388
|
-
|
|
393
|
+
`${exported("")}export const ${contractNames.typeFields} = {\n${[
|
|
389
394
|
...ordered.map(
|
|
390
395
|
(type, index) => `${quote(type.name)}: ${fieldList(index)},`,
|
|
391
396
|
),
|
|
@@ -396,19 +401,19 @@ function emit(
|
|
|
396
401
|
);
|
|
397
402
|
for (const list of emitDeclarations ? constantLists : []) {
|
|
398
403
|
output.push(
|
|
399
|
-
`${
|
|
404
|
+
`${exported(list.description)}export const ${list.name} = {\n${list.members.map((member) => `${documentation(member.description)}${quote(member.name)}: ${constant(`${list.name}.${member.name}`, null)},`).join("\n")}\n} as const;\n`,
|
|
400
405
|
);
|
|
401
406
|
}
|
|
402
407
|
output.push(
|
|
403
408
|
...declare(
|
|
404
|
-
|
|
409
|
+
`${exported("")}export interface ${contractNames.globals} {\n${variableLists.map((list) => `${documentation(list.description)}${quote(list.name)}: {\n${list.members.map((member) => `${documentation(member.description)}${quote(member.name)}: ${tsType(member.type)};`).join("\n")}\n};`).join("\n")}\n}\n`,
|
|
405
410
|
),
|
|
406
411
|
);
|
|
407
412
|
output.push(
|
|
408
|
-
|
|
413
|
+
`${exported("")}export const ${contractNames.globalVariableLists} = ${JSON.stringify(Object.fromEntries(variableLists.map((list) => [list.name, list.description])), null, "\t")} as const;\n`,
|
|
409
414
|
);
|
|
410
415
|
output.push(
|
|
411
|
-
|
|
416
|
+
`${exported("")}export const ${contractNames.symbols} = ${JSON.stringify(
|
|
412
417
|
variableLists.flatMap((list) =>
|
|
413
418
|
list.members.map((member) => ({
|
|
414
419
|
name: member.name,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// biome-ignore-all lint/suspicious/noTemplateCurlyInString: These strings are generated TypeScript template literal types, not JavaScript interpolation.
|
|
2
2
|
import { relative, sep } from "node:path";
|
|
3
3
|
import { sizePrefixes } from "@tsplc/iec";
|
|
4
|
+
import { publicComment } from "../public-tag";
|
|
4
5
|
import type { HardwareCatalog } from "./catalog";
|
|
5
6
|
import { addressSize, ioLayout, layoutBits } from "./process-image";
|
|
6
7
|
|
|
@@ -78,7 +79,7 @@ export function generateHardwareTypes(
|
|
|
78
79
|
targets?.publication
|
|
79
80
|
.map((selector) => JSON.stringify(selector))
|
|
80
81
|
.join(" | ") ?? "string";
|
|
81
|
-
const publicationTypes = `// Generated by tsPLC. Run tsplc typegen after changing EDS files, device selections or PLC storage.\
|
|
82
|
+
const publicationTypes = `// Generated by tsPLC. Run tsplc typegen after changing EDS files, device selections or PLC storage.\n${publicComment}export type PublicationSelector = ${selectors || "never"};\n${publicComment}export type PlcPublication = { readonly production: readonly PublicationSelector[]; readonly development: readonly PublicationSelector[] };\n`;
|
|
82
83
|
if (!ioTypes) {
|
|
83
84
|
return publicationTypes;
|
|
84
85
|
}
|
|
@@ -122,5 +123,5 @@ export function generateHardwareTypes(
|
|
|
122
123
|
})
|
|
123
124
|
.join(" & ") || "Readonly<Record<string, never>>"
|
|
124
125
|
: "Readonly<Record<string, HardwareIoBinding>>";
|
|
125
|
-
return `${publicationTypes}export type HardwareIoBinding = ${anyBinding};\
|
|
126
|
+
return `${publicationTypes}${publicComment}export type HardwareIoBinding = ${anyBinding};\n${publicComment}export type HardwareIoMapping = ${mapping};\n${publicComment}export const hardware = ${JSON.stringify(portableCatalog(catalog, root), null, 2)} as const;\n`;
|
|
126
127
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSDoc tag every export of a generated file carries. A host imports the exports it needs, and a
|
|
3
|
+
* check for unused exports, such as Knip's, skips a tagged one instead of reporting the rest.
|
|
4
|
+
*/
|
|
5
|
+
export const publicTag = "@public";
|
|
6
|
+
|
|
7
|
+
/** The tag as a comment of its own, for an export that has no description. */
|
|
8
|
+
export const publicComment = `/** ${publicTag} */\n`;
|