Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typescript compiler React.FC & empty props components. #2097

Merged
merged 5 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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