Skip to content

Commit c124fdf

Browse files
authored
Merge pull request #2097 from plotly/fix-ts-fc
Fix typescript compiler React.FC & empty props components.
2 parents 760af72 + fa598b4 commit c124fdf

File tree

7 files changed

+71
-15
lines changed

7 files changed

+71
-15
lines changed

@plotly/dash-generator-test-component-typescript/generator.test.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ describe('Test Typescript component metadata generation', () => {
6161
describe.each([
6262
'TypeScriptComponent',
6363
'TypeScriptClassComponent',
64-
'MemoTypeScriptComponent'
64+
'MemoTypeScriptComponent',
65+
'FCComponent',
6566
])('Test prop type names', componentName => {
6667
const getPropTypeName = (name, data) =>
6768
R.path(propPath(componentName, name).concat('type', 'name'), data);
@@ -256,10 +257,16 @@ describe('Test Typescript component metadata generation', () => {
256257
test('Standard js component is parsed', () => {
257258
expect(R.path(['StandardComponent'], metadata)).toBeDefined();
258259
});
260+
test('Mixed component prop-type & typescript', () => {
261+
expect(R.path(['MixedComponent', 'props', 'prop', 'type', 'name'], metadata)).toBe('arrayOf')
262+
})
259263
});
260-
describe('Test namespace props', () => {
264+
describe('Test special cases', () => {
261265
test('Component with picked boolean prop', () => {
262266
expect(R.path(['WrappedHTML', "props", "autoFocus", "type", "name"], metadata)).toBe("bool");
263-
})
264-
})
267+
});
268+
test('Empty Component', () => {
269+
expect(R.path(['EmptyComponent', 'props'], metadata)).toBeDefined();
270+
});
271+
});
265272
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const EmptyComponent = () => <div>Empty</div>;
4+
5+
export default EmptyComponent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import { TypescriptComponentProps } from '../props';
3+
4+
const FCComponent: React.FC<TypescriptComponentProps> = (props) => (
5+
<div>{props.children}</div>
6+
);
7+
8+
export default FCComponent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import PropTypes from 'prop-types';
2+
import React from 'react';
3+
4+
type Props = {
5+
id?: string;
6+
prop: string[];
7+
}
8+
9+
const MixedComponent: React.FC<Props> = (props) => {
10+
return (
11+
<div id={props.id}>{props.children}</div>
12+
)
13+
}
14+
15+
MixedComponent.propTypes = {
16+
prop: PropTypes.arrayOf(PropTypes.string).isRequired
17+
}
18+
19+
export default MixedComponent;

@plotly/dash-generator-test-component-typescript/src/index.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import TypeScriptClassComponent from './components/TypeScriptClassComponent';
33
import MemoTypeScriptComponent from './components/MemoTypeScriptComponent';
44
import StandardComponent from './components/StandardComponent.react';
55
import WrappedHTML from './components/WrappedHTML';
6+
import FCComponent from './components/FCComponent';
7+
import EmptyComponent from './components/EmptyComponent';
8+
import MixedComponent from './components/MixedComponent';
69

710
export {
8-
TypeScriptComponent,
9-
TypeScriptClassComponent,
10-
MemoTypeScriptComponent,
11-
StandardComponent,
12-
WrappedHTML,
11+
TypeScriptComponent,
12+
TypeScriptClassComponent,
13+
MemoTypeScriptComponent,
14+
StandardComponent,
15+
WrappedHTML,
16+
FCComponent,
17+
EmptyComponent,
18+
MixedComponent,
1319
};

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to `dash` will be documented in this file.
33
This project adheres to [Semantic Versioning](https://semver.org/).
44

5+
## [UNRELEASED]
6+
7+
### Fixed
8+
9+
- [#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.
10+
511
## [2.5.1] - 2022-06-13
612

713
### Fixed

dash/extract-meta.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function checkDocstring(name, value) {
113113
function docstringWarning(doc) {
114114
checkDocstring(doc.displayName, doc.description);
115115

116-
Object.entries(doc.props).forEach(([name, p]) =>
116+
Object.entries(doc.props || {}).forEach(([name, p]) =>
117117
checkDocstring(`${doc.displayName}.${name}`, p.description)
118118
);
119119
}
@@ -672,14 +672,14 @@ function gatherComponents(sources, components = {}) {
672672
} else {
673673
// Function components.
674674
rootExp = typeSymbol;
675-
commentSource = rootExp.valueDeclaration;
675+
commentSource = rootExp.valueDeclaration || rootExp.declarations[0];
676676
if (
677-
rootExp.valueDeclaration &&
678-
rootExp.valueDeclaration.parent
677+
commentSource &&
678+
commentSource.parent
679679
) {
680680
// Function with export later like `const MyComponent = (props) => <></>;`
681681
commentSource = getParent(
682-
rootExp.valueDeclaration.parent
682+
commentSource.parent
683683
);
684684
}
685685
}
@@ -719,8 +719,13 @@ function gatherComponents(sources, components = {}) {
719719
);
720720
}
721721

722+
if (!props) {
723+
// Ensure empty components has props.
724+
props = {};
725+
}
726+
722727
const fullText = source.getFullText();
723-
let description;
728+
let description = '';
724729
const commentRanges = ts.getLeadingCommentRanges(
725730
fullText,
726731
commentSource.getFullStart()

0 commit comments

Comments
 (0)