Skip to content

Commit b5b44da

Browse files
committed
fix(router): unwrap typed data constructors
closes #661
1 parent 782acf0 commit b5b44da

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

‎src/utils/router-parser.util.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ export class RouterParserUtil {
180180
// :(params) => { ... } → :"[Function]"
181181
cleaned = this.replaceBlockArrowFunctions(cleaned);
182182

183+
// Step 6: Unwrap constructor-wrapped object literals used in route metadata.
184+
// data:newLazyRoutingOptions({preload:true}) -> data:{preload:true}
185+
// This keeps JSON5 parsing resilient for typed route data objects.
186+
cleaned = this.unwrapConstructorWrappedObjectValues(cleaned);
187+
183188
return cleaned;
184189
}
185190

@@ -274,6 +279,112 @@ export class RouterParserUtil {
274279
return result;
275280
}
276281

282+
private unwrapConstructorWrappedObjectValues(input: string): string {
283+
const isIdentifierStart = (ch: string | undefined): boolean =>
284+
!!ch && /[A-Za-z_$]/.test(ch);
285+
const isIdentifierPart = (ch: string | undefined): boolean =>
286+
!!ch && /[A-Za-z0-9_$]/.test(ch);
287+
288+
let result = "";
289+
let i = 0;
290+
let inDouble = false;
291+
let inSingle = false;
292+
293+
while (i < input.length) {
294+
const ch = input[i];
295+
296+
if (ch === '"' && !inSingle) {
297+
inDouble = !inDouble;
298+
result += ch;
299+
i++;
300+
continue;
301+
}
302+
if (ch === "'" && !inDouble) {
303+
inSingle = !inSingle;
304+
result += ch;
305+
i++;
306+
continue;
307+
}
308+
if (ch === "\\" && (inDouble || inSingle)) {
309+
result += ch + (input[i + 1] || "");
310+
i += 2;
311+
continue;
312+
}
313+
314+
if (
315+
!inDouble &&
316+
!inSingle &&
317+
ch === ":" &&
318+
input.slice(i + 1, i + 4) === "new"
319+
) {
320+
let nameStart = i + 4;
321+
if (!isIdentifierStart(input[nameStart])) {
322+
result += ch;
323+
i++;
324+
continue;
325+
}
326+
327+
while (isIdentifierPart(input[nameStart])) {
328+
nameStart++;
329+
}
330+
331+
if (input[nameStart] !== "(" || input[nameStart + 1] !== "{") {
332+
result += ch;
333+
i++;
334+
continue;
335+
}
336+
337+
let cursor = nameStart + 1; // points to '{'
338+
let braceDepth = 1;
339+
let objInDouble = false;
340+
let objInSingle = false;
341+
342+
cursor++;
343+
while (cursor < input.length && braceDepth > 0) {
344+
const c = input[cursor];
345+
if (c === "\\" && (objInDouble || objInSingle)) {
346+
cursor += 2;
347+
continue;
348+
}
349+
if (c === '"' && !objInSingle) {
350+
objInDouble = !objInDouble;
351+
cursor++;
352+
continue;
353+
}
354+
if (c === "'" && !objInDouble) {
355+
objInSingle = !objInSingle;
356+
cursor++;
357+
continue;
358+
}
359+
if (!objInDouble && !objInSingle) {
360+
if (c === "{") {
361+
braceDepth++;
362+
} else if (c === "}") {
363+
braceDepth--;
364+
}
365+
}
366+
cursor++;
367+
}
368+
369+
if (braceDepth !== 0 || input[cursor] !== ")") {
370+
result += ch;
371+
i++;
372+
continue;
373+
}
374+
375+
const objectLiteral = input.slice(nameStart + 1, cursor);
376+
result += ":" + objectLiteral;
377+
i = cursor + 1;
378+
continue;
379+
}
380+
381+
result += ch;
382+
i++;
383+
}
384+
385+
return result;
386+
}
387+
277388
public setRootModule(module: string): void {
278389
this.rootModule = module;
279390
}

‎test/src/utils/router-parser.util.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ describe("Utils - RouterParserUtil", () => {
109109
expect(result).to.include(':"NemoViewerMode.PATIENT"');
110110
});
111111

112+
it("should unwrap constructor-wrapped data objects for JSON5 parseability (issue #661)", () => {
113+
const cleaned = routerParser.cleanRawRoute(
114+
'[{path:"details/:id/:detailsSection",loadChildren:"../lazy/xx-yy-details.module#DetailsModule",data:newLazyRoutingOptions({preload:true,delay:2500})}]',
115+
);
116+
117+
let parsed: any;
118+
expect(() => {
119+
parsed = require("json5").parse(cleaned);
120+
}).not.to.throw();
121+
expect(parsed[0].data).to.deep.equal({
122+
preload: true,
123+
delay: 2500,
124+
});
125+
});
126+
112127
it('should normalize CodeGenerator dotted output like "Enum"."VALUE" (issue #1417)', () => {
113128
expect(
114129
routerParser.cleanRawRoute(

0 commit comments

Comments
 (0)