Skip to content

Commit 8c6b9c3

Browse files
authored
fix: Ensure numeric property keys are a syntax error (#156)
fixes #154
1 parent b4510f0 commit 8c6b9c3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

js/src/parse.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ export function parse(text, options) {
401401

402402
const token = tokenizer.token;
403403

404+
// check for -NaN, +NaN, -Infinity, +Infinity, and any number
405+
if (json5 && tokenType === tt.Number && /[+\-0-9]/.test(text[token.loc.start.offset])) {
406+
throw new UnexpectedToken(token);
407+
}
408+
404409
// TODO: Clean this up a bit
405410
let key = tokenType === tt.String
406411
? /** @type {StringNode} */ (createLiteralNode(tokenType))

js/tests/parse.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,45 @@ describe("parse()", () => {
154154
}).to.throw("Unexpected token Number found.");
155155
});
156156

157+
it("should throw an error when 123 is used as a property key", () => {
158+
const text = "{ 123: 1 }";
159+
160+
expect(() => {
161+
parse(text, { mode: "json5" });
162+
}).to.throw("Unexpected token Number found.");
163+
});
164+
165+
it("should throw an error when +NaN is used as a property key", () => {
166+
const text = "{ +NaN: 1 }";
167+
168+
expect(() => {
169+
parse(text, { mode: "json5" });
170+
}).to.throw("Unexpected token Number found.");
171+
});
172+
173+
it("should throw an error when -NaN is used as a property key", () => {
174+
const text = "{ -NaN: 1 }";
175+
176+
expect(() => {
177+
parse(text, { mode: "json5" });
178+
}).to.throw("Unexpected token Number found.");
179+
});
180+
181+
it("should throw an error when +Infinity is used as a property key", () => {
182+
const text = "{ +Infinity: 1 }";
183+
184+
expect(() => {
185+
parse(text, { mode: "json5" });
186+
}).to.throw("Unexpected token Number found.");
187+
});
188+
189+
it("should throw an error when -Infinity is used as a property key", () => {
190+
const text = "{ -Infinity: 1 }";
191+
192+
expect(() => {
193+
parse(text, { mode: "json5" });
194+
}).to.throw("Unexpected token Number found.");
195+
});
157196

158197
});
159198
});

0 commit comments

Comments
 (0)