Skip to content

Commit

Permalink
fix: Ensure componentMethodsHandler ignores private properties (#440)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Tschinder <[email protected]>
# Conflicts:
#	src/handlers/componentMethodsHandler.js
  • Loading branch information
GeorgeTaveras1231 authored and danez committed May 8, 2021
1 parent c931a39 commit 41c666c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/handlers/__tests__/componentMethodsHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ describe('componentMethodsHandler', () => {
expect(documentation.methods).toMatchSnapshot();
});

it('should handle and ignore private properties', () => {
const src = `
class Test extends React.Component {
#privateProperty = () => {
console.log('Do something');
}
componentDidMount() {}
render() {
return null;
}
}
`;

componentMethodsHandler(documentation, parse(src).get('body', 0));
expect(documentation.methods.length).toBe(0);
});

describe('function components', () => {
it('no methods', () => {
const src = `
Expand Down
7 changes: 6 additions & 1 deletion src/handlers/componentMethodsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import match from '../utils/match';
import { traverseShallow } from '../utils/traverse';
import resolveToValue from '../utils/resolveToValue';

function isPublicClassProperty(path) {
return (
t.ClassProperty.check(path.node) && !t.ClassPrivateProperty.check(path.node)
);
}
/**
* The following values/constructs are considered methods:
*
Expand All @@ -28,7 +33,7 @@ import resolveToValue from '../utils/resolveToValue';
function isMethod(path) {
const isProbablyMethod =
(t.MethodDefinition.check(path.node) && path.node.kind !== 'constructor') ||
((t.ClassProperty.check(path.node) || t.Property.check(path.node)) &&
((isPublicClassProperty(path) || t.Property.check(path.node)) &&
t.Function.check(path.get('value').node));

return isProbablyMethod && !isReactComponentMethod(path);
Expand Down

0 comments on commit 41c666c

Please sign in to comment.