Skip to content

Add support for TSAsExpressions when trying to stringify expressions #634

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

Merged
merged 2 commits into from
Jul 2, 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
38 changes: 38 additions & 0 deletions src/__tests__/__snapshots__/main-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,44 @@ Object {
}
`;

exports[`main fixtures processes component "component_43.tsx" without errors 1`] = `
Object {
"description": "",
"displayName": "MenuItem",
"methods": Array [],
"props": Object {
"children": Object {
"description": "Menu item contents.",
"required": false,
"type": Object {
"name": "node",
},
},
"classes": Object {
"description": "Override or extend the styles applied to the component. See CSS API below for more details.",
"required": false,
"type": Object {
"name": "object",
},
},
"component": Object {
"defaultValue": Object {
"computed": false,
"value": "'li'",
},
"required": false,
},
"disableGutters": Object {
"defaultValue": Object {
"computed": false,
"value": "false",
},
"required": false,
},
},
}
`;

exports[`main fixtures processes component "flow-export-type.js" without errors 1`] = `
Object {
"description": "This is a Flow class component",
Expand Down
40 changes: 40 additions & 0 deletions src/__tests__/fixtures/component_43.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import PropTypes from 'prop-types';
import MatMenuItem, {
MenuItemProps as MatMenuItemProps,
} from '@mui/material/MenuItem';

export type ComponentObjectOf<
T extends React.ElementType,
P = React.ComponentProps<T>
> = React.FunctionComponent<P>;

export type MenuItemProps<
D extends React.ElementType,
P = {}
> = MatMenuItemProps<D, P>;

const MenuItem = React.forwardRef<MenuItemProps<'li'>>(
<C extends React.ElementType>(
props: MenuItemProps<C, { component?: C }>,
ref: React.Ref<any>
) => {
return <MatMenuItem {...props} ref={ref} />;
}
) as unknown as <C extends React.ElementType = 'li'>(
props: MenuItemProps<C, { component?: C }>
) => React.ReactElement;

(MenuItem as ComponentObjectOf<typeof MenuItem>).propTypes = {
/** Menu item contents. */
children: PropTypes.node,
/** Override or extend the styles applied to the component. See CSS API below for more details. */
classes: PropTypes.object,
};

(MenuItem as ComponentObjectOf<typeof MenuItem>).defaultProps = {
component: 'li',
disableGutters: false,
};

export default MenuItem;
12 changes: 12 additions & 0 deletions src/utils/__tests__/expressionTo-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,17 @@ describe('expressionTo', () => {
expressionToArray(expression('foo[{ a(){} }].baz'), noopImporter),
).toEqual(['foo', '{a: <function>}', 'baz']);
});

it('with TSAsExpression', () => {
expect(
expressionToArray(
expression('(baz as X).prop', {
filename: 'file.ts',
parserOptions: { plugins: ['typescript'] },
}),
noopImporter,
),
).toEqual(['baz', 'prop']);
});
});
});
5 changes: 5 additions & 0 deletions src/utils/expressionTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ function toArray(path: NodePath, importer: Importer): string[] {
} else if (t.Identifier.check(node)) {
result.push(node.name);
continue;
} else if (t.TSAsExpression.check(node)) {
if (t.Identifier.check(node.expression)) {
result.push(node.expression.name);
}
continue;
} else if (t.Literal.check(node)) {
// @ts-ignore
result.push(node.raw);
Expand Down