Skip to content

Commit

Permalink
Merge pull request #59650 from CyrusNajmabadi/jsonErrorMessage
Browse files Browse the repository at this point in the history
Improve json error message in particular case
  • Loading branch information
CyrusNajmabadi authored Feb 21, 2022
2 parents d8c18d4 + ad274b5 commit d82c81b
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ public void TestMissingColon()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""20"" Length=""3"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""20"" Length=""3"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Strings must start with &quot; not '"" Start=""12"" Length=""1"" />
Expand Down Expand Up @@ -1328,10 +1328,10 @@ public void TestNestedPropertyMissingColon()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""102"" Length=""8"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""102"" Length=""8"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""102"" Length=""8"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""102"" Length=""8"" />
</Diagnostics>");
}

Expand Down Expand Up @@ -1367,10 +1367,10 @@ public void TestMissingColon2()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""22"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""22"" Length=""5"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""22"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""22"" Length=""5"" />
</Diagnostics>");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3028,10 +3028,10 @@ public void n_object_comma_instead_of_colon_json()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>");
}

Expand Down Expand Up @@ -3188,10 +3188,10 @@ public void n_object_missing_colon_json()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>");
}

Expand Down Expand Up @@ -3250,10 +3250,10 @@ public void n_object_missing_semicolon_json()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>");
}

Expand Down Expand Up @@ -3308,10 +3308,10 @@ public void n_object_no_colon_json()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""5"" />
</Diagnostics>");
}

Expand Down Expand Up @@ -3762,10 +3762,10 @@ public void n_object_with_single_string_json()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""31"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""31"" Length=""5"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""31"" Length=""5"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""31"" Length=""5"" />
</Diagnostics>");
}

Expand Down Expand Up @@ -5299,10 +5299,10 @@ public void n_structure_open_object_string_with_apostrophes_json()
</CompilationUnit>
</Tree>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""3"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""3"" />
</Diagnostics>",
@"<Diagnostics>
<Diagnostic Message=""Only properties allowed in an object"" Start=""11"" Length=""3"" />
<Diagnostic Message=""Property name must be followed by a ':'"" Start=""11"" Length=""3"" />
</Diagnostics>");
}

Expand Down
18 changes: 17 additions & 1 deletion src/Features/Core/Portable/EmbeddedLanguages/Json/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private JsonTree ParseTree(JsonOptions options)
var diagnostic = node.Kind switch
{
JsonKind.Array => CheckArray((JsonArrayNode)node),
JsonKind.Object => CheckObject((JsonObjectNode)node),
_ => null,
};

Expand Down Expand Up @@ -246,7 +247,22 @@ private JsonTree ParseTree(JsonOptions options)
}
}

return CheckChildren(node);
return null;
}

static EmbeddedDiagnostic? CheckObject(JsonObjectNode node)
{
foreach (var child in node.Sequence)
{
if (child is JsonLiteralNode { LiteralToken.Kind: JsonKind.StringToken })
{
return new EmbeddedDiagnostic(
FeaturesResources.Property_name_must_be_followed_by_a_colon,
child.GetSpan());
}
}

return null;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Features/Core/Portable/FeaturesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3082,6 +3082,9 @@ Zero-width positive lookbehind assertions are typically used at the beginning of
<data name="Property_name_must_be_a_string" xml:space="preserve">
<value>Property name must be a string</value>
</data>
<data name="Property_name_must_be_followed_by_a_colon" xml:space="preserve">
<value>Property name must be followed by a ':'</value>
</data>
<data name="Strings_must_start_with_double_quote_not_single_quote" xml:space="preserve">
<value>Strings must start with " not '</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Features/Core/Portable/xlf/FeaturesResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d82c81b

Please sign in to comment.