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

feat(schema/api-ref): spread support for inference nodes #7914

Merged
merged 8 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -1,8 +1,15 @@
.inferenceWrapper {
position: relative;
> pre {
> code {
// override syntax highlighter code font size
font-size: var(--bit-p-xxs, 12px) !important;
}
}
}

.isSpread {
> span {
font-style: italic;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import SyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-light';
import tsxSyntax from 'react-syntax-highlighter/dist/esm/languages/prism/tsx';
import defaultTheme from 'react-syntax-highlighter/dist/esm/styles/prism/vsc-dark-plus';
import classNames from 'classnames';
import { Tooltip } from '@teambit/design.ui.tooltip';

import styles from './inference-type.module.scss';

SyntaxHighlighter.registerLanguage('tsx', tsxSyntax);
Expand All @@ -24,8 +26,9 @@ function InferenceTypeComponent(props: APINodeRenderProps) {
} = props;

const inferenceTypeNode = api as InferenceTypeSchema;
const isSpread = inferenceTypeNode.isSpread;
const typeToRender = (inferenceTypeNode.type || inferenceTypeNode.name) ?? '';
// ugly - but we need to make an educated guess if this is an object or not to decide if we should highlight it or not

const maybeObject = typeToRender.includes('{');
const disableHighlight = metadata?.[inferenceTypeNode.__schema]?.disableHighlight || !maybeObject;

Expand All @@ -39,24 +42,48 @@ function InferenceTypeComponent(props: APINodeRenderProps) {
return (
<div
key={`inference-${inferenceTypeNode.name}`}
className={classNames(nodeStyles.node, className, styles.inferenceWrapper)}
className={classNames(nodeStyles.node, className, styles.inferenceWrapper, isSpread && styles.isSpread)}
>
{!disableHighlight && (
<SyntaxHighlighter
language={lang}
style={defaultTheme}
customStyle={{
borderRadius: '8px',
marginTop: '4px',
padding: '8px',
fontFamily: 'roboto mono',
fontSize: 12,
}}
{isSpread && (
<Tooltip
placement={'bottom-end'}
content={
<SyntaxHighlighter
language={lang}
style={defaultTheme}
customStyle={{
borderRadius: '8px',
margin: '0',
padding: '0',
fontFamily: 'roboto mono',
fontSize: 11,
}}
>
{typeToRender}
</SyntaxHighlighter>
}
>
{typeToRender}
</SyntaxHighlighter>
{`...rest`}
</Tooltip>
)}
{disableHighlight && (inferenceTypeNode.type || inferenceTypeNode.name)}
{!isSpread &&
(!disableHighlight ? (
<SyntaxHighlighter
language={lang}
style={defaultTheme}
customStyle={{
borderRadius: '8px',
marginTop: '4px',
padding: '8px',
fontFamily: 'roboto mono',
fontSize: 12,
}}
>
{typeToRender}
</SyntaxHighlighter>
) : (
inferenceTypeNode.type || inferenceTypeNode.name
))}
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export class InferenceTypeSchema extends SchemaNode {
readonly location: SchemaLocation,
readonly type: string,
readonly name?: string,
readonly defaultValue?: string
readonly defaultValue?: string,
readonly isSpread?: boolean
) {
super();
}
Expand All @@ -25,6 +26,7 @@ export class InferenceTypeSchema extends SchemaNode {
...super.toObject(),
type: this.type,
defaultValue: this.defaultValue,
isSpread: this.isSpread,
};
}

Expand All @@ -33,6 +35,7 @@ export class InferenceTypeSchema extends SchemaNode {
const type = obj.type;
const name = obj.name;
const defaultValue = obj.defaultValue;
return new InferenceTypeSchema(location, type, name, defaultValue);
const isSpread = obj.isSpread;
return new InferenceTypeSchema(location, type, name, defaultValue, isSpread);
}
}
8 changes: 7 additions & 1 deletion scopes/typescript/typescript/transformers/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ export class ParameterTransformer implements SchemaTransformer {
const info = await context.getQuickInfo(elem.name);
const parsed = info ? parseTypeFromQuickInfo(info) : elem.getText();
const defaultValue = elem.initializer ? elem.initializer.getText() : undefined;
return new InferenceTypeSchema(context.getLocation(elem.name), parsed, elem.name.getText(), defaultValue);
return new InferenceTypeSchema(
context.getLocation(elem.name),
parsed,
elem.name.getText(),
defaultValue,
Boolean(elem.dotDotDotToken)
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
text-overflow: ellipsis;
white-space: nowrap;
margin-right: 8px;
line-height: 16px;
line-height: 115%;
}
}

Expand Down