feat(json): enforce StringLiteral-style property key (#4371)

* test: add tests

* feat(json): enforce StringLiteral-style property key
master
Ika 2018-04-26 00:00:07 +08:00 committed by GitHub
parent 317f3a3c56
commit 289b98c125
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -3068,6 +3068,23 @@ function printPropertyKey(path, options, print) {
const node = path.getNode();
const key = node.key;
if (
key.type === "Identifier" &&
!node.computed &&
options.parser === "json"
) {
// a -> "a"
return path.call(
keyPath =>
comments.printComments(
keyPath,
() => JSON.stringify(key.name),
options
),
"key"
);
}
if (
isStringLiteral(key) &&
isIdentifierName(key.value) &&

View File

@ -692,6 +692,50 @@ exports[`pass1.json 4`] = `
`;
exports[`propertyKey.json 1`] = `
{
a: 123
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
"a": 123
}
`;
exports[`propertyKey.json 2`] = `
{
a: 123
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
"a": 123
}
`;
exports[`propertyKey.json 3`] = `
{
a: 123
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
a: 123
}
`;
exports[`propertyKey.json 4`] = `
{
a: 123
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
a: 123,
}
`;
exports[`single-line.json 1`] = `
{"key1":[true,false,null],"key2":{"key3":[1,2,"3",1e10,1e-3]}}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -0,0 +1,3 @@
{
a: 123
}