diff --git a/src/handlers/__tests__/componentMethodsHandler-test.js b/src/handlers/__tests__/componentMethodsHandler-test.js index 30051102dd8..a213494a25a 100644 --- a/src/handlers/__tests__/componentMethodsHandler-test.js +++ b/src/handlers/__tests__/componentMethodsHandler-test.js @@ -277,6 +277,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 = ` diff --git a/src/handlers/componentMethodsHandler.js b/src/handlers/componentMethodsHandler.js index df71c3c55b5..bf4f97e8cf5 100644 --- a/src/handlers/componentMethodsHandler.js +++ b/src/handlers/componentMethodsHandler.js @@ -18,6 +18,11 @@ import { traverseShallow } from '../utils/traverse'; import resolveToValue from '../utils/resolveToValue'; import type { Importer } from '../types'; +function isPublicClassProperty(path) { + return ( + t.ClassProperty.check(path.node) && !t.ClassPrivateProperty.check(path.node) + ); +} /** * The following values/constructs are considered methods: * @@ -29,7 +34,7 @@ import type { Importer } from '../types'; function isMethod(path, importer) { 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(resolveToValue(path.get('value'), importer).node)); return isProbablyMethod && !isReactComponentMethod(path, importer);