Fix duplicate mobDefinition import and guard against it.
Remove the second import in play main.js and extend smoke-boot tests to fail on duplicate import bindings in client modules and the HTTP graph.
@@ -40,7 +40,6 @@ import { ItemId, itemDefinition } from "/shared/definitions/items.js";
40
40
import { CraftingItemId, TINDERBOX_MAX_CHARGES } from "/shared/definitions/craftingItems.js";
41
41
import { ItemFamilyId } from "/shared/definitions/tierFamilies.js";
42
42
import { EquipmentKind } from "/shared/definitions/equipmentKinds.js";
43
import { mobDefinition } from "/shared/definitions/mobs.js";
44
43
import { countCoinsInSlots } from "/shared/items/inventory.js";
45
44
46
45
const game = new Game();
@@ -40,7 +40,6 @@ import { ItemId, itemDefinition } from "/shared/definitions/items.js";40
import { CraftingItemId, TINDERBOX_MAX_CHARGES } from "/shared/definitions/craftingItems.js";40
import { CraftingItemId, TINDERBOX_MAX_CHARGES } from "/shared/definitions/craftingItems.js";41
import { ItemFamilyId } from "/shared/definitions/tierFamilies.js";41
import { ItemFamilyId } from "/shared/definitions/tierFamilies.js";42
import { EquipmentKind } from "/shared/definitions/equipmentKinds.js";42
import { EquipmentKind } from "/shared/definitions/equipmentKinds.js";43
import { mobDefinition } from "/shared/definitions/mobs.js";44
import { countCoinsInSlots } from "/shared/items/inventory.js";43
import { countCoinsInSlots } from "/shared/items/inventory.js";45
44
46
const game = new Game();45
const game = new Game();@@ -26,6 +26,20 @@ test("client modules do not re-export unbound names", () =>
26
26
assert.deepEqual(offenders, []);
27
27
});
28
28
29
test("client modules do not declare duplicate import bindings", () =>
30
{
31
const offenders = [];
32
for (const file of listJsFiles(CLIENT_SRC))
33
{
34
const source = readFileSync(file, "utf8");
35
for (const name of duplicateImportBindings(source))
36
{
37
offenders.push(`${relative(ROOT, file)}: duplicate import binding '${name}'`);
38
}
39
}
40
assert.deepEqual(offenders, []);
41
});
42
29
43
test("HTTP smoke: play, editor, and module graph resolve", async () =>
30
44
{
31
45
const server = createHttpServer(ROOT);
@@ -146,6 +160,10 @@ async function crawlModuleGraph (base, urlPath, seen)
146
160
{
147
161
assert.fail(`${urlPath}: export { ${name} } is not defined in module`);
148
162
}
163
for (const name of duplicateImportBindings(source))
164
{
165
assert.fail(`${urlPath}: duplicate import binding '${name}'`);
166
}
149
167
for (const spec of staticImportSpecifiers(source))
150
168
{
151
169
const next = resolveBrowserSpecifier(urlPath, spec);
@@ -213,6 +231,49 @@ function unboundReexportLocals (source)
213
231
return missing;
214
232
}
215
233
234
/** Local names imported more than once in the same module (SyntaxError in browsers). */
235
function duplicateImportBindings (source)
236
{
237
const cleaned = stripComments(source);
238
const counts = new Map();
239
const note = (name) =>
240
{
241
if (!name || name === "default")
242
{ return; }
243
counts.set(name, (counts.get(name) ?? 0) + 1);
244
};
245
for (const match of cleaned.matchAll(/\bimport\s*\{([^}]+)\}\s*from\s*["']/g))
246
{
247
for (const part of match[1].split(","))
248
{
249
const trimmed = part.trim();
250
if (!trimmed)
251
{ continue; }
252
const bits = trimmed.split(/\s+as\s+/);
253
note(bits[bits.length - 1].trim());
254
}
255
}
256
for (const match of cleaned.matchAll(/\bimport\s+([A-Za-z_$][\w$]*)\s*,\s*\{([^}]+)\}\s*from\s*["']/g))
257
{
258
note(match[1]);
259
for (const part of match[2].split(","))
260
{
261
const trimmed = part.trim();
262
if (!trimmed)
263
{ continue; }
264
const bits = trimmed.split(/\s+as\s+/);
265
note(bits[bits.length - 1].trim());
266
}
267
}
268
for (const match of cleaned.matchAll(/\bimport\s+([A-Za-z_$][\w$]*)\s+from\s*["']/g))
269
{ note(match[1]); }
270
for (const match of cleaned.matchAll(/\bimport\s*\*\s*as\s+([A-Za-z_$][\w$]*)\s+from\s*["']/g))
271
{ note(match[1]); }
272
return [...counts.entries()]
273
.filter(([, count]) => count > 1)
274
.map(([name]) => name);
275
}
276
216
277
function importedBindings (source)
217
278
{
218
279
const names = new Set();
@@ -26,6 +26,20 @@ test("client modules do not re-export unbound names", () =>26
assert.deepEqual(offenders, []);26
assert.deepEqual(offenders, []);27
});27
});28
28
29
test("client modules do not declare duplicate import bindings", () =>30
{31
const offenders = [];32
for (const file of listJsFiles(CLIENT_SRC))33
{34
const source = readFileSync(file, "utf8");35
for (const name of duplicateImportBindings(source))36
{37
offenders.push(`${relative(ROOT, file)}: duplicate import binding '${name}'`);38
}39
}40
assert.deepEqual(offenders, []);41
});42
29
test("HTTP smoke: play, editor, and module graph resolve", async () =>43
test("HTTP smoke: play, editor, and module graph resolve", async () =>30
{44
{31
const server = createHttpServer(ROOT);45
const server = createHttpServer(ROOT);@@ -146,6 +160,10 @@ async function crawlModuleGraph (base, urlPath, seen)146
{160
{147
assert.fail(`${urlPath}: export { ${name} } is not defined in module`);161
assert.fail(`${urlPath}: export { ${name} } is not defined in module`);148
}162
}163
for (const name of duplicateImportBindings(source))164
{165
assert.fail(`${urlPath}: duplicate import binding '${name}'`);166
}149
for (const spec of staticImportSpecifiers(source))167
for (const spec of staticImportSpecifiers(source))150
{168
{151
const next = resolveBrowserSpecifier(urlPath, spec);169
const next = resolveBrowserSpecifier(urlPath, spec);@@ -213,6 +231,49 @@ function unboundReexportLocals (source)213
return missing;231
return missing;214
}232
}215
233
234
/** Local names imported more than once in the same module (SyntaxError in browsers). */235
function duplicateImportBindings (source)236
{237
const cleaned = stripComments(source);238
const counts = new Map();239
const note = (name) =>240
{241
if (!name || name === "default")242
{ return; }243
counts.set(name, (counts.get(name) ?? 0) + 1);244
};245
for (const match of cleaned.matchAll(/\bimport\s*\{([^}]+)\}\s*from\s*["']/g))246
{247
for (const part of match[1].split(","))248
{249
const trimmed = part.trim();250
if (!trimmed)251
{ continue; }252
const bits = trimmed.split(/\s+as\s+/);253
note(bits[bits.length - 1].trim());254
}255
}256
for (const match of cleaned.matchAll(/\bimport\s+([A-Za-z_$][\w$]*)\s*,\s*\{([^}]+)\}\s*from\s*["']/g))257
{258
note(match[1]);259
for (const part of match[2].split(","))260
{261
const trimmed = part.trim();262
if (!trimmed)263
{ continue; }264
const bits = trimmed.split(/\s+as\s+/);265
note(bits[bits.length - 1].trim());266
}267
}268
for (const match of cleaned.matchAll(/\bimport\s+([A-Za-z_$][\w$]*)\s+from\s*["']/g))269
{ note(match[1]); }270
for (const match of cleaned.matchAll(/\bimport\s*\*\s*as\s+([A-Za-z_$][\w$]*)\s+from\s*["']/g))271
{ note(match[1]); }272
return [...counts.entries()]273
.filter(([, count]) => count > 1)274
.map(([name]) => name);275
}276
216
function importedBindings (source)277
function importedBindings (source)217
{278
{218
const names = new Set();279
const names = new Set();