Skip to content

Commit 5ba3421

Browse files
committed
fix(router): normalize unresolved dotted references
closes #1417
1 parent 9a1cc63 commit 5ba3421

2 files changed

Lines changed: 49 additions & 13 deletions

File tree

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,28 @@ export class RouterParserUtil {
148148
.replace(/"([^"]*)"\+([a-zA-Z_$][a-zA-Z0-9_$.]*)/g, '"$1$2"');
149149
} while (cleaned !== prev);
150150

151-
// NOTE: Property-access chains used as standalone values (e.g. path:SomeEnum.VALUE)
152-
// are intentionally left unquoted so that JSON parsing fails gracefully and the
153-
// existing enum-resolution fallback in constructRoutesTree() can handle them.
151+
// Step 3b: Normalize CodeGenerator dotted chains and unresolved enum/property refs
152+
// "Enum"."VALUE" -> "Enum.VALUE"
153+
// :Enum.VALUE -> :"Enum.VALUE"
154+
// [Enum.VALUE] -> ["Enum.VALUE"]
155+
// This keeps route parsing resilient even when symbols cannot be resolved earlier.
156+
do {
157+
prev = cleaned;
158+
cleaned = cleaned.replace(
159+
/"([a-zA-Z_$][a-zA-Z0-9_$]*)"\."([a-zA-Z_$][a-zA-Z0-9_$]*)"/g,
160+
'"$1.$2"',
161+
);
162+
} while (cleaned !== prev);
163+
164+
cleaned = cleaned
165+
.replace(
166+
/:([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)/g,
167+
':"$1"',
168+
)
169+
.replace(
170+
/([\[,])([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)(?=[,\]])/g,
171+
'$1"$2"',
172+
);
154173

155174
// Step 4: Replace non-block arrow function expressions in property values.
156175
// :(params) => simpleExpr → :"[Function]"

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,33 @@ describe("Utils - RouterParserUtil", () => {
8888
).to.equal('[{path:"USER/CREATE"}]');
8989
});
9090

91-
// ── Standalone property-access chains — preserved for enum fallback ──
92-
// Property-access chains as standalone values are intentionally left unquoted so
93-
// the enum-resolution fallback in constructRoutesTree() can resolve them.
91+
// ── Standalone property-access chains — normalized for safe parsing ──
9492

95-
it("should leave a standalone property-access chain unquoted (enum fallback handles it)", () => {
93+
it("should quote a standalone property-access chain to keep JSON5 parseable", () => {
9694
expect(
9795
routerParser.cleanRawRoute("[{path:AppRoutes.center}]"),
98-
).to.equal("[{path:AppRoutes.center}]");
96+
).to.equal('[{path:"AppRoutes.center"}]');
9997
});
10098

101-
it("should leave a deep property-access chain unquoted", () => {
99+
it("should quote a deep property-access chain", () => {
102100
expect(
103101
routerParser.cleanRawRoute("[{path:AppRoutes.shared.main}]"),
104-
).to.equal("[{path:AppRoutes.shared.main}]");
102+
).to.equal('[{path:"AppRoutes.shared.main"}]');
105103
});
106104

107-
it("should leave enum references in data objects unquoted", () => {
105+
it("should quote enum references in data objects", () => {
108106
const result = routerParser.cleanRawRoute(
109107
'[{path:"x",data:{mode:NemoViewerMode.PATIENT}}]',
110108
);
111-
expect(result).to.include("NemoViewerMode.PATIENT");
112-
expect(result).not.to.include(':"NemoViewerMode.PATIENT"');
109+
expect(result).to.include(':"NemoViewerMode.PATIENT"');
110+
});
111+
112+
it('should normalize CodeGenerator dotted output like "Enum"."VALUE" (issue #1417)', () => {
113+
expect(
114+
routerParser.cleanRawRoute(
115+
'[{"data":{"mode":"NemoViewerMode"."PATIENT"}}]',
116+
),
117+
).to.equal('[{"data":{"mode":"NemoViewerMode.PATIENT"}}]');
113118
});
114119

115120
it("should not quote JSON5 reserved identifiers (true/false/null)", () => {
@@ -262,6 +267,18 @@ describe("Utils - RouterParserUtil", () => {
262267
}).not.to.throw();
263268
expect(result[0].data.fn).to.equal("[Function]");
264269
});
270+
271+
it("should allow JSON5.parse to succeed with unresolved dotted references in values and arrays (issue #1417)", () => {
272+
const cleaned = routerParser.cleanRawRoute(
273+
'[{"data":{"mode":"NemoViewerMode"."PATIENT","allowed":["UserTypes"."UserType2"]}}]',
274+
);
275+
let result: any;
276+
expect(() => {
277+
result = require("json5").parse(cleaned);
278+
}).not.to.throw();
279+
expect(result[0].data.mode).to.equal("NemoViewerMode.PATIENT");
280+
expect(result[0].data.allowed[0]).to.equal("UserTypes.UserType2");
281+
});
265282
});
266283

267284
describe("cleanFileDynamics() — template literals in route path (issue #1493)", () => {

0 commit comments

Comments
 (0)