Skip to content

Commit 9f814a2

Browse files
authored
fix: add explict key to Fragment in createHighlight to prevent React unique key warnings (#18)
* fix: add key to lineToken fragment createHighlight * test: add test covering code view with language highlight rules * chore: import highlight rules in test from lib
1 parent 82b17cf commit 9f814a2

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/code-view/__tests__/code-view.test.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { cleanup, render } from "@testing-library/react";
3+
import { cleanup, getByText, render } from "@testing-library/react";
44
import { afterEach, describe, expect, test } from "vitest";
55
import CodeView from "../../../lib/components/code-view";
6+
import typescriptHighlightRules from "../../../lib/components/code-view/highlight/typescript";
67
import styles from "../../../lib/components/code-view/styles.css.js";
78
import createWrapper from "../../../lib/components/test-utils/dom";
89

@@ -58,4 +59,16 @@ describe("CodeView", () => {
5859
const wrapper = createWrapper().findCodeView()!;
5960
expect(wrapper!.findContent().getElement().innerHTML).toContain('class="tokenized"');
6061
});
62+
63+
test("correctly tokenizes content if highlight is set to language rules", () => {
64+
render(<CodeView content={'const hello: string = "world";'} highlight={typescriptHighlightRules}></CodeView>);
65+
const wrapper = createWrapper().findCodeView()!;
66+
const element = wrapper!.findContent().getElement();
67+
68+
// Check that the content is tokenized following typescript rules.
69+
expect(getByText(element, "const")).toHaveClass("ace_type");
70+
expect(getByText(element, "hello")).toHaveClass("ace_identifier");
71+
expect(getByText(element, "string")).toHaveClass("ace_type");
72+
expect(getByText(element, '"world"')).toHaveClass("ace_string");
73+
});
6174
});

src/code-view/highlight/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import type { Ace } from "ace-code";
44
import { tokenize } from "ace-code/src/ext/simple_tokenizer";
5+
import { Fragment } from "react";
56
import "ace-code/styles/theme/cloud_editor.css";
67
import "ace-code/styles/theme/cloud_editor_dark.css";
78

@@ -10,8 +11,8 @@ export function createHighlight(rules: Ace.HighlightRules) {
1011
const tokens = tokenize(code, rules);
1112
return (
1213
<span>
13-
{tokens.map((lineTokens) => (
14-
<>
14+
{tokens.map((lineTokens, lineIndex) => (
15+
<Fragment key={lineIndex}>
1516
{lineTokens.map((token, tokenIndex) => {
1617
return token.className ? (
1718
<span className={token?.className} key={tokenIndex}>
@@ -22,7 +23,7 @@ export function createHighlight(rules: Ace.HighlightRules) {
2223
);
2324
})}
2425
{"\n"}
25-
</>
26+
</Fragment>
2627
))}
2728
</span>
2829
);

0 commit comments

Comments
 (0)