Skip to content

Commit 41c666c

Browse files
fix: Ensure componentMethodsHandler ignores private properties (#440)
Co-authored-by: Daniel Tschinder <[email protected]> # Conflicts: # src/handlers/componentMethodsHandler.js
1 parent c931a39 commit 41c666c

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
@@ -156,6 +156,25 @@ describe('componentMethodsHandler', () => {
156156
expect(documentation.methods).toMatchSnapshot();
157157
});
158158

159+
it('should handle and ignore private properties', () => {
160+
const src = `
161+
class Test extends React.Component {
162+
#privateProperty = () => {
163+
console.log('Do something');
164+
}
165+
166+
componentDidMount() {}
167+
168+
render() {
169+
return null;
170+
}
171+
}
172+
`;
173+
174+
componentMethodsHandler(documentation, parse(src).get('body', 0));
175+
expect(documentation.methods.length).toBe(0);
176+
});
177+
159178
describe('function components', () => {
160179
it('no methods', () => {
161180
const src = `

src/handlers/componentMethodsHandler.js

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

20+
function isPublicClassProperty(path) {
21+
return (
22+
t.ClassProperty.check(path.node) && !t.ClassPrivateProperty.check(path.node)
23+
);
24+
}
2025
/**
2126
* The following values/constructs are considered methods:
2227
*
@@ -28,7 +33,7 @@ import resolveToValue from '../utils/resolveToValue';
2833
function isMethod(path) {
2934
const isProbablyMethod =
3035
(t.MethodDefinition.check(path.node) && path.node.kind !== 'constructor') ||
31-
((t.ClassProperty.check(path.node) || t.Property.check(path.node)) &&
36+
((isPublicClassProperty(path) || t.Property.check(path.node)) &&
3237
t.Function.check(path.get('value').node));
3338

3439
return isProbablyMethod && !isReactComponentMethod(path);

0 commit comments

Comments
 (0)