Skip to content

Commit 68cd178

Browse files
committed
Fix swift.spec.js
Since highlight.js 11.6+, the `distributed` keyword is built-in and no custom overrides are needed
1 parent c42a4d3 commit 68cd178

File tree

2 files changed

+7
-31
lines changed

2 files changed

+7
-31
lines changed

src/utils/custom-highlight-lang/swift.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,6 @@ import swift from 'highlight.js/lib/languages/swift';
1313
export default function swiftOverride(hljs) {
1414
const language = swift(hljs);
1515

16-
// Temporarily patch the Swift language syntax to recognize `distributed` as
17-
// a keyword until the next version of highlight.js (v11.6) is released, which
18-
// will have built-in support for this [1]
19-
//
20-
// [1]: https://github.com/highlightjs/highlight.js/pull/3523
21-
language.keywords.keyword = [
22-
...language.keywords.keyword,
23-
'distributed',
24-
];
25-
26-
const isClassMode = ({ beginKeywords = '' }) => beginKeywords
27-
.split(' ')
28-
.includes('class');
29-
const classModeIndex = language.contains.findIndex(isClassMode);
30-
if (classModeIndex >= 0) {
31-
const {
32-
beginKeywords, // purposefully strip this out
33-
...classMode
34-
} = language.contains[classModeIndex];
35-
// Update the existing "class" mode by replacing the `beginKeywords` with
36-
// a `begin` regular expression, which is careful not to mistakenly
37-
// recognize class function declarations as class declarations
38-
language.contains[classModeIndex] = {
39-
...classMode,
40-
begin: /\b(struct|protocol|extension|enum|actor|class\b(?!.*\bfunc))\b/,
41-
};
42-
}
43-
4416
// Checks if a given language sub-mode matches the "ESCAPED_NEWLINE" from the
4517
// built-in Swift parser from hljs
4618
const isEscapedNewlineMode = (mode) => {

tests/unit/utils/custom-highlight-lang/swift.spec.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ describe('swift', () => {
2727

2828
beforeEach(() => {
2929
mode = language.contains.find(m => (
30-
m.begin && m.begin.test && m.begin.test('class Foobar {')
30+
Array.isArray(m.begin)
31+
&& m.begin.length > 0
32+
&& m.begin[0].test('class Foobar {')
3133
));
3234
});
3335

3436
it('does not have a `beginKeywords` attribute anymore', () => {
37+
expect(mode).toBeDefined();
3538
expect(mode.beginKeywords).toBeFalsy();
3639
});
3740

3841
it('does have a new `begin` attribute', () => {
39-
expect(mode.begin)
40-
.toEqual(/\b(struct|protocol|extension|enum|actor|class\b(?!.*\bfunc))\b/);
42+
expect(mode).toBeDefined();
43+
expect(Array.isArray(mode.begin)).toBe(true);
44+
expect(mode.begin[0]).toEqual(/(struct|protocol|class|extension|enum|actor)/);
4145
});
4246
});
4347
});

0 commit comments

Comments
 (0)