@creationix/rex 0.1.2 → 0.1.4

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/rex-compile.ts CHANGED
@@ -15,9 +15,11 @@ type CliOptions = {
15
15
  };
16
16
 
17
17
  type DomainSchema = {
18
- globals?: Record<string, unknown>;
18
+ globals?: Record<string, { ref?: unknown }>;
19
19
  };
20
20
 
21
+ const FIRST_NON_RESERVED_REF = 5;
22
+
21
23
  function parseArgs(argv: string[]): CliOptions {
22
24
  const options: CliOptions = {
23
25
  ir: false,
@@ -160,10 +162,26 @@ async function loadDomainRefsFromFolder(folderPath: string): Promise<Record<stri
160
162
  }
161
163
 
162
164
  const refs: Record<string, number> = {};
163
- let nextRef = 0;
164
- for (const name of Object.keys(parsed.globals)) {
165
- refs[name] = nextRef;
166
- nextRef += 1;
165
+ const seenRefIds = new Map<number, string>();
166
+ for (const [name, entry] of Object.entries(parsed.globals)) {
167
+ if (!entry || typeof entry !== "object") {
168
+ throw new Error(`Invalid rex-domain.json at ${schemaPath}: globals.${name} must be an object with a numeric ref`);
169
+ }
170
+ const ref = entry.ref;
171
+ if (!Number.isInteger(ref)) {
172
+ throw new Error(`Invalid rex-domain.json at ${schemaPath}: globals.${name}.ref must be an integer`);
173
+ }
174
+ if (ref < FIRST_NON_RESERVED_REF) {
175
+ throw new Error(
176
+ `Invalid rex-domain.json at ${schemaPath}: globals.${name}.ref must be >= ${FIRST_NON_RESERVED_REF} (0-4 are reserved built-ins)`,
177
+ );
178
+ }
179
+ const existing = seenRefIds.get(ref);
180
+ if (existing) {
181
+ throw new Error(`Invalid rex-domain.json at ${schemaPath}: duplicate ref ${ref} for globals.${existing} and globals.${name}`);
182
+ }
183
+ seenRefIds.set(ref, name);
184
+ refs[name] = ref;
167
185
  }
168
186
  return refs;
169
187
  }