Skip to content

Commit 3cf127d

Browse files
authored
Merge pull request #1148 from nojaf/fix-paste-json
Only paste as json when root is object or array
2 parents b5cdc2b + 8ccd36c commit 3cf127d

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
> - :house: [Internal]
1111
> - :nail_care: [Polish]
1212
13+
## [Unreleased]
14+
15+
#### :bug: Bug fix
16+
17+
- Only paste objects/arrays are JSON.t https://github.com/rescript-lang/rescript-vscode/pull/1148
18+
1319
## 1.68.0
1420

1521
#### :rocket: New Feature

client/src/commands/paste_as_rescript_json.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,7 @@ const isLikelyJson = (text: string): boolean => {
1111
return false;
1212
}
1313
const first = trimmed[0];
14-
if (first === "{" || first === "[" || first === '"' || first === "-") {
15-
return true;
16-
}
17-
if (first >= "0" && first <= "9") {
18-
return true;
19-
}
20-
if (
21-
trimmed.startsWith("true") ||
22-
trimmed.startsWith("false") ||
23-
trimmed.startsWith("null")
24-
) {
25-
return true;
26-
}
27-
return false;
14+
return first === "{" || first === "[";
2815
};
2916

3017
const ensureFloatString = (value: number): string => {
@@ -99,6 +86,10 @@ export const convertPlainTextToJsonT = (text: string): JsonConversionResult => {
9986

10087
try {
10188
const parsed = JSON.parse(text);
89+
// Only convert objects and arrays, not primitive values
90+
if (typeof parsed !== "object" || parsed === null) {
91+
return { kind: "notJson" };
92+
}
10293
return { kind: "success", formatted: formatJsonValue(parsed) };
10394
} catch {
10495
return {

0 commit comments

Comments
 (0)