Skip to content

Commit

Permalink
Fix bugs with tree, rand str from regex, JSONL
Browse files Browse the repository at this point in the history
See CHANGELOG changes
  • Loading branch information
molsonkiko committed Sep 2, 2024
1 parent db632d4 commit 18bf739
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 79 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

1. Fix issue where [random string from regex](/docs/README.md#random-strings-from-regex-added-in-v81) would incorrectly flag some valid regular expressions (e.g. `(?-i)(?:xy{1,2}){,2}`) as having two consecutive quantifiers.
1. Fix the following issues with [random string from regex](/docs/README.md#random-strings-from-regex-added-in-v81):
- It previously incorrectly flagged some valid regular expressions (e.g. `(?-i)(?:xy{1,2}){,2}`) as having two consecutive quantifiers.
- It previously did not correctly handle some character sets where the final character was `-` (for example, `[+-]` previously would only generate `+`, and now it correctly has a 50% chance of generating `-` or `+`)
2. Fix issue where RemesPath incorrectly inferred the type of (a [function](/docs/RemesPath.md#functions) `fun` followed by [indexers](/docs/RemesPath.md#indexing-and-selecting-keys)) to be the return type of `fun`. For example, running the query `sum(dict(items(@)).a)` on the JSON `{"a": [1]}` now correctly returns `1.0`, but RemesPath *used to raise an error because it assumed that `dict(items(@)).a` had the same type as `dict(items(@))`*
3. Fix very rare crash bug when using the `Value to clipboard` option of the [tree node right-click context menu](/docs/README.md#get-info-about-tree-nodes).
4. Fix bug where some invalid JSON Lines documents (for example, `[1, \n2][3]`) would be accepted by the [JSON Lines parser](/docs/README.md#json-lines-documents) despite having elements that span multiple lines.

## [8.1.0] - 2024-08-23

Expand Down
6 changes: 3 additions & 3 deletions JsonToolsNppPlugin/Forms/TreeViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ private static void JsonTreePopulate_FullRecursive(TreeView tree,

/// <summary>
/// On right click, throw up a context menu that lets you do the following:<br></br>
/// - Copy the current node's value to the clipboard<br></br>
/// - Copy the current node's value to the clipboard (unless it's an array or object, in which case do nothing)<br></br>
/// - Copy the node's path (Python style) to the clipboard<br></br>
/// - Copy the node's key/index (Python style) to the clipboard<br></br>
/// - Copy the node's path (JavaScript style) to the clipboard<br></br>
Expand All @@ -885,8 +885,8 @@ private void Tree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
valToClipboardHandler = new MouseEventHandler(
(object sender2, MouseEventArgs e2) =>
{
JNode jnode = pathsToJNodes[node.FullPath];
if (jnode is JObject || jnode is JArray)
if (!pathsToJNodes.TryGetValue(node.FullPath, out JNode jnode)
|| jnode is JObject || jnode is JArray)
return;
Npp.TryCopyToClipboard(jnode.ToString());
}
Expand Down
10 changes: 9 additions & 1 deletion JsonToolsNppPlugin/JSONTools/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,17 +1799,25 @@ public JNode ParseJsonLines(string inp)
while (ii < inp.Length)
{
json = ParseSomething(inp, 0);
int endOfLastJson = ii;
ConsumeInsignificantChars(inp);
children.Add(json);
if (fatal)
{
return arr;
}
int maxLastII = ii > inp.Length ? inp.Length : ii;
int maxLastII = ii > inp.Length ? inp.Length : ii;
for (; lastII < maxLastII; lastII++)
{
if (inp[lastII] == '\n')
{
if (lastII < endOfLastJson) // for example, "[1,\n2]" is invalid JSON Lines, but "[1,2]\n" is OK because the newline is trailing
{
HandleError(JsonLintType.FATAL_JSONL_NOT_ONE_DOC_PER_LINE, inp, lastII);
return arr;
}
lineNum++;
}
}
// make sure this document was all in one line
if (!(lineNum == arr.Length
Expand Down
4 changes: 2 additions & 2 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("8.1.0.2")]
[assembly: AssemblyFileVersion("8.1.0.2")]
[assembly: AssemblyVersion("8.1.0.3")]
[assembly: AssemblyFileVersion("8.1.0.3")]
58 changes: 36 additions & 22 deletions JsonToolsNppPlugin/Tests/JsonParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,34 +1138,43 @@ public static bool TestLinter()

public static bool TestJsonLines()
{
JsonParser parser = new JsonParser();
var testcases = new string[][]
JsonParser parser = new JsonParser(LoggerLevel.JSON5, false, false);
var testcases = new (string input, string desiredOutStr)[]
{
new string[]
{
(
"[\"a\", \"b\"]\r\n" +
"{\"a\": false, \"b\": 2}\r\n" +
"null\r\n" +
"1.5", // normal style
"[[\"a\", \"b\"], {\"a\": false, \"b\": 2}, null, 1.5]"
},
new string[]
{
),
(
"[1, 2]\n[3, 4]\n", // newline at end
"[[1, 2], [3, 4]]"
},
new string[]
{
),
(
"{\"a\": [1, 2], \"b\": -7.8}", // single document
"[{\"a\": [1, 2], \"b\": -7.8}]"
},
),
(
"[1, 2] // foo\r\n", // single doc with comment at end
"[[1, 2]]"
),
(
"{foo: {bar: 1., baz: [null, /* bunrmeme */ False]}}",
"[{\"foo\": {\"bar\": 1.0, \"baz\": [null, false]}}]"
),
(
// honestly this should probably be invalid b/c one of the line breaks is inside a comment, but JsonTools should accept it anyway
"\"nbn\" # py\r\n -.75 /* mul \n */undefined\n",
"[\"nbn\", -0.75, null]"
)
};
int testsFailed = 0;
int ii = 0;
foreach (string[] test in testcases)
foreach ((string input, string desiredOutStr) in testcases)
{
string input = test[0];
JNode desiredOutput = TryParse(test[1], parser);
JNode desiredOutput = TryParse(desiredOutStr, parser);
JNode json = TryParse(input, parser, true);
if (json == null)
{
Expand All @@ -1181,20 +1190,22 @@ public static bool TestJsonLines()
{1}
Got
{2} ",
ii + 1, test[1], json.ToString()));
ii + 1, desiredOutStr, json.ToString()));
}
}

string[] shouldThrowTestcases = new string[]
{
"[1,\n2]\n[3, 4]", // one doc covers multiple lines
"[1, 2]\n\n3", // two lines between docs
"[1, 2]\n // fjfjfj\n3", // two lines between docs
"[1, 2] [3, 4]", // two docs on same line
"", // empty input
"[1, 2]\n[3, 4", // final doc is invalid
"[1, 2\n[3, 4]", // first doc is invalid
"[1, 2]\nd", // bad char at EOF
"[1, 2]\n\n", // multiple newlines after last doc
"[1, 2]\n[3, 0xH", // final doc is invalid (0xH is not valid hex number)
"[1, fals\n[3, 4]", // first doc is invalid (invalid literal starting with 'f')
"[1, 2]\nd", // bad char 'd' where a new JSON line or EOF expected
"[1, 2]\n # foo\n", // multiple newlines after last doc
"[1,\n2][3, 4]", // doc covering multiple lines with no newline before next doc
"{\"a\": 1}/* bnkk\n */{\"b\": 2}\r\n[1.5,\n3]", // doc covering multiple lines with no newline before EOF
};

foreach (string test in shouldThrowTestcases)
Expand All @@ -1203,8 +1214,11 @@ public static bool TestJsonLines()
try
{
JNode json = parser.ParseJsonLines(test);
testsFailed++;
Npp.AddLine($"Expected JSON Lines parser to throw an error on input\n{test}\nbut instead returned {json.ToString()}");
if (!parser.fatal)
{
testsFailed++;
Npp.AddLine($"Expected JSON Lines parser to throw an error on input\n{test}\nbut instead returned {json.ToString()}");
}
}
catch { }
}
Expand Down
1 change: 1 addition & 0 deletions JsonToolsNppPlugin/Tests/RandomStringFromRegexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static bool Test()
"ba ba \tba", "ba ba \tbaba", "ba ba \tbababa" }),
(@"(?-i)(?:xy{1,2}){,2}", new HashSet<string>{"", "xy", "xyy", "xyxy", "xyxyy", "xyyxy", "xyyxyy"}),
(@"(b|g{3})?", new HashSet<string>{"", "b", "ggg"}),
("[a-][b-c-]|[hij-][-][+]", new HashSet<string>{"ab", "ac", "a-", "-b", "-c", "--", "h-+", "i-+", "j-+", "--+"}),

};
ii += simpleTestcases.Length * 2;
Expand Down
2 changes: 1 addition & 1 deletion JsonToolsNppPlugin/Utils/RandomStringFromRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ private RandomStringFromRegex(string regex)
tryCharRange = false;
precededByCharRange = true;
}
else if (d == '-' && jj > firstTokenInCurrentState && !precededByCharRange)
else if (d == '-' && jj > firstTokenInCurrentState && jj < tokens.Count - 1 && !precededByCharRange)
tryCharRange = true;
else
{
Expand Down
Loading

0 comments on commit 18bf739

Please sign in to comment.