Skip to content

Commit 9683e67

Browse files
fix: Ensure componentMethodsHandler ignores private properties (#440)
Co-authored-by: Daniel Tschinder <[email protected]>
1 parent 42b9c35 commit 9683e67

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/handlers/__tests__/componentMethodsHandler-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ describe('componentMethodsHandler', () => {
277277
expect(documentation.methods).toMatchSnapshot();
278278
});
279279

280+
it('should handle and ignore private properties', () => {
281+
const src = `
282+
class Test extends React.Component {
283+
#privateProperty = () => {
284+
console.log('Do something');
285+
}
286+
287+
componentDidMount() {}
288+
289+
render() {
290+
return null;
291+
}
292+
}
293+
`;
294+
295+
componentMethodsHandler(documentation, parse(src).get('body', 0));
296+
expect(documentation.methods.length).toBe(0);
297+
});
298+
280299
describe('function components', () => {
281300
it('no methods', () => {
282301
const src = `

src/handlers/componentMethodsHandler.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import { traverseShallow } from '../utils/traverse';
1818
import resolveToValue from '../utils/resolveToValue';
1919
import type { Importer } from '../types';
2020

21+
function isPublicClassProperty(path) {
22+
return (
23+
t.ClassProperty.check(path.node) && !t.ClassPrivateProperty.check(path.node)
24+
);
25+
}
2126
/**
2227
* The following values/constructs are considered methods:
2328
*
@@ -29,7 +34,7 @@ import type { Importer } from '../types';
2934
function isMethod(path, importer) {
3035
const isProbablyMethod =
3136
(t.MethodDefinition.check(path.node) && path.node.kind !== 'constructor') ||
32-
((t.ClassProperty.check(path.node) || t.Property.check(path.node)) &&
37+
((isPublicClassProperty(path) || t.Property.check(path.node)) &&
3338
t.Function.check(resolveToValue(path.get('value'), importer).node));
3439

3540
return isProbablyMethod && !isReactComponentMethod(path, importer);

0 commit comments

Comments
 (0)