Skip to content

Commit

Permalink
Merge pull request #2097 from plotly/fix-ts-fc
Browse files Browse the repository at this point in the history
Fix typescript compiler React.FC & empty props components.
  • Loading branch information
T4rk1n authored Jun 16, 2022
2 parents 760af72 + fa598b4 commit c124fdf
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 15 deletions.
15 changes: 11 additions & 4 deletions @plotly/dash-generator-test-component-typescript/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ describe('Test Typescript component metadata generation', () => {
describe.each([
'TypeScriptComponent',
'TypeScriptClassComponent',
'MemoTypeScriptComponent'
'MemoTypeScriptComponent',
'FCComponent',
])('Test prop type names', componentName => {
const getPropTypeName = (name, data) =>
R.path(propPath(componentName, name).concat('type', 'name'), data);
Expand Down Expand Up @@ -256,10 +257,16 @@ describe('Test Typescript component metadata generation', () => {
test('Standard js component is parsed', () => {
expect(R.path(['StandardComponent'], metadata)).toBeDefined();
});
test('Mixed component prop-type & typescript', () => {
expect(R.path(['MixedComponent', 'props', 'prop', 'type', 'name'], metadata)).toBe('arrayOf')
})
});
describe('Test namespace props', () => {
describe('Test special cases', () => {
test('Component with picked boolean prop', () => {
expect(R.path(['WrappedHTML', "props", "autoFocus", "type", "name"], metadata)).toBe("bool");
})
})
});
test('Empty Component', () => {
expect(R.path(['EmptyComponent', 'props'], metadata)).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

const EmptyComponent = () => <div>Empty</div>;

export default EmptyComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import { TypescriptComponentProps } from '../props';

const FCComponent: React.FC<TypescriptComponentProps> = (props) => (
<div>{props.children}</div>
);

export default FCComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import PropTypes from 'prop-types';
import React from 'react';

type Props = {
id?: string;
prop: string[];
}

const MixedComponent: React.FC<Props> = (props) => {
return (
<div id={props.id}>{props.children}</div>
)
}

MixedComponent.propTypes = {
prop: PropTypes.arrayOf(PropTypes.string).isRequired
}

export default MixedComponent;
16 changes: 11 additions & 5 deletions @plotly/dash-generator-test-component-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import TypeScriptClassComponent from './components/TypeScriptClassComponent';
import MemoTypeScriptComponent from './components/MemoTypeScriptComponent';
import StandardComponent from './components/StandardComponent.react';
import WrappedHTML from './components/WrappedHTML';
import FCComponent from './components/FCComponent';
import EmptyComponent from './components/EmptyComponent';
import MixedComponent from './components/MixedComponent';

export {
TypeScriptComponent,
TypeScriptClassComponent,
MemoTypeScriptComponent,
StandardComponent,
WrappedHTML,
TypeScriptComponent,
TypeScriptClassComponent,
MemoTypeScriptComponent,
StandardComponent,
WrappedHTML,
FCComponent,
EmptyComponent,
MixedComponent,
};
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED]

### Fixed

- [#2097](https://github.com/plotly/dash/pull/2097) Fix bug [#2095](https://github.com/plotly/dash/issues/2095) with TypeScript compiler and `React.FC` empty valueDeclaration error & support empty props components.

## [2.5.1] - 2022-06-13

### Fixed
Expand Down
17 changes: 11 additions & 6 deletions dash/extract-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function checkDocstring(name, value) {
function docstringWarning(doc) {
checkDocstring(doc.displayName, doc.description);

Object.entries(doc.props).forEach(([name, p]) =>
Object.entries(doc.props || {}).forEach(([name, p]) =>
checkDocstring(`${doc.displayName}.${name}`, p.description)
);
}
Expand Down Expand Up @@ -672,14 +672,14 @@ function gatherComponents(sources, components = {}) {
} else {
// Function components.
rootExp = typeSymbol;
commentSource = rootExp.valueDeclaration;
commentSource = rootExp.valueDeclaration || rootExp.declarations[0];
if (
rootExp.valueDeclaration &&
rootExp.valueDeclaration.parent
commentSource &&
commentSource.parent
) {
// Function with export later like `const MyComponent = (props) => <></>;`
commentSource = getParent(
rootExp.valueDeclaration.parent
commentSource.parent
);
}
}
Expand Down Expand Up @@ -719,8 +719,13 @@ function gatherComponents(sources, components = {}) {
);
}

if (!props) {
// Ensure empty components has props.
props = {};
}

const fullText = source.getFullText();
let description;
let description = '';
const commentRanges = ts.getLeadingCommentRanges(
fullText,
commentSource.getFullStart()
Expand Down

0 comments on commit c124fdf

Please sign in to comment.