Skip to content

Commit 703ab7b

Browse files
authored
Support template literal types in docs generator (#3856)
1 parent f5f2631 commit 703ab7b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/dev/docs/src/types.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export function Type({type}) {
132132
return <IndexedAccess {...type} />;
133133
case 'keyof':
134134
return <Keyof {...type} />;
135+
case 'template':
136+
return <TemplateLiteral {...type} />;
135137
default:
136138
console.log('no render component for TYPE', type);
137139
return null;
@@ -602,3 +604,25 @@ function ConditionalType({checkType, extendsType, trueType, falseType}) {
602604
</>
603605
);
604606
}
607+
608+
function TemplateLiteral({elements}) {
609+
return (
610+
<>
611+
<span className="token hljs-string">{'`'}</span>
612+
{elements.map((element, i) => {
613+
if (element.type === 'string' && element.value) {
614+
return <span className="token hljs-string" key={i}>{element.value}</span>;
615+
}
616+
617+
return (
618+
<React.Fragment key={i}>
619+
<span className="token punctuation">{'${'}</span>
620+
<Type type={element} />
621+
<span className="token punctuation">{'}'}</span>
622+
</React.Fragment>
623+
);
624+
})}
625+
<span className="token hljs-string">{'`'}</span>
626+
</>
627+
);
628+
}

packages/dev/parcel-transformer-docs/DocsTransformer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,29 @@ module.exports = new Transformer({
483483
}
484484

485485
if (path.isTSLiteralType()) {
486+
if (t.isTemplateLiteral(path.node.literal)) {
487+
let expressions = path.get('literal.expressions').map(e => processExport(e));
488+
let elements = [];
489+
let i = 0;
490+
for (let q of path.node.literal.quasis) {
491+
if (q.value.raw) {
492+
elements.push({
493+
type: 'string',
494+
value: q.value.raw
495+
});
496+
}
497+
498+
if (!q.tail) {
499+
elements.push(expressions[i++]);
500+
}
501+
}
502+
503+
return Object.assign(node, {
504+
type: 'template',
505+
elements
506+
});
507+
}
508+
486509
return Object.assign(node, {
487510
type: typeof path.node.literal.value,
488511
value: path.node.literal.value

0 commit comments

Comments
 (0)