Skip to content

Commit

Permalink
Fix typescript tuple type.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Oct 4, 2022
1 parent 5335ec5 commit 1d7cd46
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function getMetadata() {
[
path.resolve(__dirname, '..', '..', 'dash', 'extract-meta.js'),
'""', // ignore pattern
'""', // reserved keywords
'^_.*$', // reserved keywords
path.join(__dirname, 'src', 'components')
],
{
Expand Down Expand Up @@ -103,6 +103,10 @@ describe('Test Typescript component metadata generation', () => {
`${componentName} setProps func`,
testTypeFactory('setProps', 'func')
);
test(
`${componentName} tuple tuple`,
testTypeFactory('a_tuple', 'tuple')
)
});

describe('Test prop attributes', () => {
Expand Down Expand Up @@ -230,6 +234,24 @@ describe('Test Typescript component metadata generation', () => {
'name'
], metadata)).toBe('any')
}
);

test(
'Tuple elements', () => {
const tuplePath: (string|number)[] = [
'TypeScriptComponent',
'props',
'a_tuple',
'type',
'elements'
]
expect(
R.path(tuplePath.concat(0, 'name'), metadata)
).toBe('number');
expect(
R.path(tuplePath.concat(1, 'name'), metadata)
).toBe('string');
}
)
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export type TypescriptComponentProps = {
setProps?: (props: Record<string, any>) => void;
className?: string;
style?: any;

nested?: Nested;

a_tuple?: [number, string];
};

export type WrappedHTMLProps = {
Expand Down
5 changes: 5 additions & 0 deletions dash/development/_py_components_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ def array_of():
)
return "list"

def tuple_of():
elements = [js_to_py_type(element) for element in type_object["elements"]]
return f"tuple of ({', '.join(elements)})"

return dict(
array=lambda: "list",
bool=lambda: "boolean",
Expand Down Expand Up @@ -601,6 +605,7 @@ def array_of():
shape=shape_or_exact,
# React's PropTypes.exact
exact=shape_or_exact,
tuple=tuple_of,
)


Expand Down
19 changes: 14 additions & 5 deletions dash/extract-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function gatherComponents(sources, components = {}) {
const getPropType = (propType, propObj, parentType = null) => {
// Types can get namespace prefixes or not.
let name = checker.typeToString(propType).replace(/^React\./, '');
let value;
let value, elements;
const raw = name;

const newParentType = (parentType || []).concat(raw)
Expand All @@ -311,13 +311,13 @@ function gatherComponents(sources, components = {}) {
// Shapes & array support.
if (!PRIMITIVES.concat('enum', 'func', 'union').includes(name)) {
if (
name.includes('[]') ||
name.includes('Array') ||
name === 'tuple'
// Excluding object with arrays in the raw.
(name.includes('[]') && name.endsWith("]")) ||
name.includes('Array')
) {
name = 'arrayOf';
const replaced = raw.replace('[]', '');
if (unionSupport.includes('replaced')) {
if (unionSupport.includes(replaced)) {
// Simple types are easier.
value = {
name: getPropTypeName(replaced),
Expand All @@ -336,6 +336,14 @@ function gatherComponents(sources, components = {}) {
name = 'array';
}
}
} else if (
name === 'tuple' ||
(name.startsWith('[') && name.endsWith(']'))
) {
name = 'tuple';
elements = propType.resolvedTypeArguments.map(
t => getPropType(t, propObj, newParentType)
);
} else if (
BANNED_TYPES.includes(name) ||
(parentType && parentType.includes(name))
Expand Down Expand Up @@ -369,6 +377,7 @@ function gatherComponents(sources, components = {}) {
return {
name,
value,
elements,
raw
};
};
Expand Down

0 comments on commit 1d7cd46

Please sign in to comment.