Skip to content

Commit edd7348

Browse files
authored
Only indent Objective-C method declarations (swiftlang#860)
This prevents bugs where the indentation formatting code is mistakenly executed with declarations that are not Objective-C methods, like certain C++ functions. Resolves: rdar://129590639
1 parent 5801817 commit edd7348

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/utils/indentation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@
1010

1111
import Language from 'docc-render/constants/Language';
1212

13+
const ObjcMethodPrefix = {
14+
instance: '-',
15+
klass: '+',
16+
};
17+
1318
function indentObjcDeclaration(codeElement) {
19+
// only attempt to indent declarations for Objective-C instance/class methods
20+
const txt = codeElement.textContent ?? '';
21+
if (!txt.startsWith(ObjcMethodPrefix.instance) && !txt.startsWith(ObjcMethodPrefix.klass)) {
22+
return;
23+
}
24+
1425
// find all param name spans (which are tokenized as "token-identifier"s)
1526
const params = codeElement.getElementsByClassName('token-identifier');
1627
if (params.length < 2) {

tests/unit/utils/indentation.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,16 @@ describe('indentDeclaration', () => {
5959
expect(code.innerHTML).toEqual(originalCode);
6060
});
6161
});
62+
63+
describe('with a C++ function that has namespaced parameters', () => {
64+
it('should not add indentation', () => {
65+
const originalCode = '<a href="/documentation/mycppclass" class="type-identifier-link"><code><span>MyCPPClass</span></code></a><span class="token-identifier">operator+</span>(<span class="token-identifier">std</span>::<span class="type-identifier-link"><span>string</span></span> <span class="token-internalParam">other</span>);';
66+
67+
const code = prepare(originalCode);
68+
indentDeclaration(code, 'occ');
69+
70+
expect(code.innerHTML).toEqual(originalCode);
71+
});
72+
});
6273
});
6374
});

0 commit comments

Comments
 (0)