feat(json): enforce StringLiteral-style property key (#4371)
* test: add tests * feat(json): enforce StringLiteral-style property keymaster
parent
317f3a3c56
commit
289b98c125
|
@ -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) &&
|
||||
|
|
|
@ -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]}}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
a: 123
|
||||
}
|
Loading…
Reference in New Issue