Skip to content

Commit 81bcf78

Browse files
committed
fix: type and undefined guard for highlight and rendered row
1 parent cab9475 commit 81bcf78

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/components/DiffViewer/utils/json-diff/get-inline-syntax-highlight.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export type InlineHighlightResult = {
99
};
1010

1111
function syntaxHighlightLine(enabled: boolean, text: string, offset: number): InlineHighlightResult[] {
12+
if (!text || typeof text !== "string") {
13+
return [{ token: "plain", start: offset, end: offset }];
14+
}
15+
1216
if (!enabled) {
1317
return [{ token: "plain", start: offset, end: text.length + offset }];
1418
}

src/components/DiffViewer/utils/json-diff/row-renderer-grid.tsx

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,41 @@ ListChildComponentProps<{
6868

6969
const [lDiff, rDiff]
7070
= leftPart.type === "modify" && rightPart.type === "modify"
71-
? getInlineDiff(leftPart.text, rightPart.text, inlineDiffOptions ?? { mode: "char" })
71+
? getInlineDiff(leftPart.text || "", rightPart.text || "", inlineDiffOptions ?? { mode: "char" })
7272
: [[], []];
7373

74-
const lTokens = syntaxHighlightLine(true, leftPart.text, 0);
75-
const rTokens = syntaxHighlightLine(true, rightPart.text, 0);
74+
const lTokens = syntaxHighlightLine(true, leftPart.text || "", 0);
75+
const rTokens = syntaxHighlightLine(true, rightPart.text || "", 0);
7676

7777
const lResult = mergeSegments(lTokens, lDiff);
7878
const rResult = mergeSegments(rTokens, rDiff);
7979

80-
const renderInlineResult = (text: string, result: typeof lResult, comma?: boolean) => (
81-
<>
82-
{result.map((item, idx) => {
83-
const frag = text.slice(item.start, item.end);
84-
const className = [
85-
item.type ? `inline-diff-${item.type}` : "",
86-
item.token ? `token ${item.token}` : "",
87-
]
88-
.filter(Boolean)
89-
.join(" ");
90-
return (
91-
<span key={`${idx}-${item.type}-${frag}`} className={className}>
92-
{frag}
93-
</span>
94-
);
95-
})}
96-
{comma && <span className="token punctuation">,</span>}
97-
</>
98-
);
80+
const renderInlineResult = (text: string, result: typeof lResult, comma?: boolean) => {
81+
// Guard against undefined or null text
82+
if (!text || typeof text !== "string") {
83+
return <span className="token plain"></span>;
84+
}
85+
86+
return (
87+
<>
88+
{result.map((item, idx) => {
89+
const frag = text.slice(item.start, item.end);
90+
const className = [
91+
item.type ? `inline-diff-${item.type}` : "",
92+
item.token ? `token ${item.token}` : "",
93+
]
94+
.filter(Boolean)
95+
.join(" ");
96+
return (
97+
<span key={`${idx}-${item.type}-${frag}`} className={className}>
98+
{frag}
99+
</span>
100+
);
101+
})}
102+
{comma && <span className="token punctuation">,</span>}
103+
</>
104+
);
105+
};
99106

100107
return (
101108
<div
@@ -115,7 +122,7 @@ ListChildComponentProps<{
115122
<div className={`cell line-${leftPart.type} ${equalEmptyLine(leftPart)}`} role="cell">
116123
<pre>
117124
{leftPart.text && indentChar.repeat(leftPart.level * indentSize)}
118-
{renderInlineResult(leftPart.text, lResult, leftPart.comma)}
125+
{renderInlineResult(leftPart.text || "", lResult, leftPart.comma)}
119126
</pre>
120127
</div>
121128

@@ -126,7 +133,7 @@ ListChildComponentProps<{
126133
<div className={`cell line-${rightPart.type} ${equalEmptyLine(rightPart)}`} role="cell">
127134
<pre>
128135
{rightPart.text && indentChar.repeat(rightPart.level * indentSize)}
129-
{renderInlineResult(rightPart.text, rResult, rightPart.comma)}
136+
{renderInlineResult(rightPart.text || "", rResult, rightPart.comma)}
130137
</pre>
131138
</div>
132139
</div>

0 commit comments

Comments
 (0)