Skip to content

Commit 2bd83a9

Browse files
authored
Merge pull request #2257 from plotly/ts-tuple
Fix typescript tuple type.
2 parents 5335ec5 + a77dec6 commit 2bd83a9

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

Diff for: @plotly/dash-generator-test-component-typescript/generator.test.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function getMetadata() {
99
[
1010
path.resolve(__dirname, '..', '..', 'dash', 'extract-meta.js'),
1111
'""', // ignore pattern
12-
'""', // reserved keywords
12+
'^_.*$', // reserved keywords
1313
path.join(__dirname, 'src', 'components')
1414
],
1515
{
@@ -103,6 +103,10 @@ describe('Test Typescript component metadata generation', () => {
103103
`${componentName} setProps func`,
104104
testTypeFactory('setProps', 'func')
105105
);
106+
test(
107+
`${componentName} tuple tuple`,
108+
testTypeFactory('a_tuple', 'tuple')
109+
)
106110
});
107111

108112
describe('Test prop attributes', () => {
@@ -230,6 +234,24 @@ describe('Test Typescript component metadata generation', () => {
230234
'name'
231235
], metadata)).toBe('any')
232236
}
237+
);
238+
239+
test(
240+
'Tuple elements', () => {
241+
const tuplePath: (string|number)[] = [
242+
'TypeScriptComponent',
243+
'props',
244+
'a_tuple',
245+
'type',
246+
'elements'
247+
]
248+
expect(
249+
R.path(tuplePath.concat(0, 'name'), metadata)
250+
).toBe('number');
251+
expect(
252+
R.path(tuplePath.concat(1, 'name'), metadata)
253+
).toBe('string');
254+
}
233255
)
234256
});
235257

Diff for: @plotly/dash-generator-test-component-typescript/src/props.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export type TypescriptComponentProps = {
4040
setProps?: (props: Record<string, any>) => void;
4141
className?: string;
4242
style?: any;
43-
4443
nested?: Nested;
44+
45+
a_tuple?: [number, string];
4546
};
4647

4748
export type WrappedHTMLProps = {

Diff for: 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+
- [#2257](https://github.com/plotly/dash/pull/2257) Fix tuple types in the TypeScript component generator.
10+
511
## [2.6.2] - 2022-09-23
612

713
### Fixed

Diff for: dash/development/_py_components_generation.py

+5
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ def array_of():
570570
)
571571
return "list"
572572

573+
def tuple_of():
574+
elements = [js_to_py_type(element) for element in type_object["elements"]]
575+
return f"list of {len(elements)} elements: [{', '.join(elements)}]"
576+
573577
return dict(
574578
array=lambda: "list",
575579
bool=lambda: "boolean",
@@ -601,6 +605,7 @@ def array_of():
601605
shape=shape_or_exact,
602606
# React's PropTypes.exact
603607
exact=shape_or_exact,
608+
tuple=tuple_of,
604609
)
605610

606611

Diff for: dash/extract-meta.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ function gatherComponents(sources, components = {}) {
293293
const getPropType = (propType, propObj, parentType = null) => {
294294
// Types can get namespace prefixes or not.
295295
let name = checker.typeToString(propType).replace(/^React\./, '');
296-
let value;
296+
let value, elements;
297297
const raw = name;
298298

299299
const newParentType = (parentType || []).concat(raw)
@@ -311,13 +311,13 @@ function gatherComponents(sources, components = {}) {
311311
// Shapes & array support.
312312
if (!PRIMITIVES.concat('enum', 'func', 'union').includes(name)) {
313313
if (
314-
name.includes('[]') ||
315-
name.includes('Array') ||
316-
name === 'tuple'
314+
// Excluding object with arrays in the raw.
315+
(name.includes('[]') && name.endsWith("]")) ||
316+
name.includes('Array')
317317
) {
318318
name = 'arrayOf';
319319
const replaced = raw.replace('[]', '');
320-
if (unionSupport.includes('replaced')) {
320+
if (unionSupport.includes(replaced)) {
321321
// Simple types are easier.
322322
value = {
323323
name: getPropTypeName(replaced),
@@ -336,6 +336,14 @@ function gatherComponents(sources, components = {}) {
336336
name = 'array';
337337
}
338338
}
339+
} else if (
340+
name === 'tuple' ||
341+
(name.startsWith('[') && name.endsWith(']'))
342+
) {
343+
name = 'tuple';
344+
elements = propType.resolvedTypeArguments.map(
345+
t => getPropType(t, propObj, newParentType)
346+
);
339347
} else if (
340348
BANNED_TYPES.includes(name) ||
341349
(parentType && parentType.includes(name))
@@ -369,6 +377,7 @@ function gatherComponents(sources, components = {}) {
369377
return {
370378
name,
371379
value,
380+
elements,
372381
raw
373382
};
374383
};

0 commit comments

Comments
 (0)