diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..55462dc --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI + +on: + push: + branches: + - main + - master + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [18, 20] + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + cache: yarn + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - name: Run tests + run: yarn test --ci diff --git a/__tests__/handler.test.js b/__tests__/handler.test.js index 4e50882..87a06b7 100644 --- a/__tests__/handler.test.js +++ b/__tests__/handler.test.js @@ -39,11 +39,26 @@ describe('typescript-react-function-component-props-handler', () => { expect(doc.displayName).toBe('TooltipTarget'); }); - // Currently returns error for React.forwardRef(...) components test('handles React.forwardRef(...) components - part 2', () => { - expect(() => parseFixture('ForwardedButton.tsx')).toThrow( - "Cannot read properties of undefined (reading 'length')" - ); + const doc = parseFixture('ForwardedButton.tsx'); + + expect(doc.displayName).toBe('ForwardedButton'); + expect(doc).toHaveProperty('props'); + expect(doc.props).toHaveProperty('label'); + expect(doc.props.label.tsType).toMatchObject({ name: 'string' }); + expect(doc.props.label.required).toBe(true); + expect(doc.props.label).toHaveProperty('description'); + expect(doc.props.label.description).toBe('Text to show inside the button'); + + expect(doc.props).toHaveProperty('onClick'); + expect(doc.props.onClick.tsType).toMatchObject({ name: 'signature' }); + expect(doc.props.onClick.tsType.type).toBe('function'); + expect(doc.props.onClick.tsType.raw).toBe('() => void'); + expect(doc.props.onClick.tsType.signature).toMatchObject({ arguments: [], return: { name: 'void' } }); + expect(doc.props.onClick.required).toBe(false); + + expect(doc.props.onClick).toHaveProperty('description'); + expect(doc.props.onClick.description).toBe('Optional click handler'); }); test('handles React.FC components - AccessibleButton', () => { diff --git a/index.js b/index.js index 7cee338..5bbb137 100644 --- a/index.js +++ b/index.js @@ -48,14 +48,13 @@ function checkForProptypes(path, paramTypeName) { function setParamsTypeDefinitionFromFunctionType(documentation, path) { if ( - path.parentPath.node.init && + path.parentPath.node.init && Array.isArray(path.parentPath.node.init.params) && path.parentPath.node.init.params.length === 0 - ) - { + ) { return; - } - + } + if ( path.node.type === 'ArrowFunctionExpression' && (