Skip to content

Commit 62cc1c3

Browse files
committed
fix(router): support shorthand data properties
closes #1354
1 parent b75392a commit 62cc1c3

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ export class RouterParserUtil {
179179
'$1"$2"',
180180
);
181181

182+
// Step 3c: Expand object shorthand properties to key/value form.
183+
// data:{flag,delay:2500} -> data:{flag:"flag",delay:2500}
184+
// JSON5 cannot parse object shorthand, so we normalize it here.
185+
cleaned = cleaned.replace(
186+
/([{,])([a-zA-Z_$][a-zA-Z0-9_$]*)(?=[,}])/g,
187+
'$1$2:"$2"',
188+
);
189+
182190
// Step 4: Replace non-block arrow function expressions in property values.
183191
// :(params) => simpleExpr → :"[Function]"
184192
// loadChildren/loadComponent are already converted to strings by Step 0 above.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ describe("Utils - RouterParserUtil", () => {
124124
});
125125
});
126126

127+
it("should normalize shorthand object properties in route data for JSON5 parseability (issue #1354)", () => {
128+
const cleaned = routerParser.cleanRawRoute(
129+
'[{path:"x",data:{preload,delay:2500}}]',
130+
);
131+
132+
let parsed: any;
133+
expect(() => {
134+
parsed = require("json5").parse(cleaned);
135+
}).not.to.throw();
136+
expect(parsed[0].data.preload).to.equal("preload");
137+
expect(parsed[0].data.delay).to.equal(2500);
138+
});
139+
127140
it('should normalize CodeGenerator dotted output like "Enum"."VALUE" (issue #1417)', () => {
128141
expect(
129142
routerParser.cleanRawRoute(

0 commit comments

Comments
 (0)