|
1 | | -import ts, { isTemplateExpression, SyntaxKind } from 'typescript' |
| 1 | +import ts, { isReturnStatement, isTemplateExpression, SyntaxKind } from 'typescript' |
2 | 2 | import { |
3 | 3 | stripQuotesFromNode, |
4 | 4 | parsePropertyOfType, |
@@ -218,6 +218,39 @@ export function parseExampleTemplate( |
218 | 218 | throw new ParserError(`Expected a body for the render function`, { sourceFile, node: exp }) |
219 | 219 | } |
220 | 220 |
|
| 221 | + // If the body is a string literal, we generate a `figma.value` statement instead, which just |
| 222 | + // renders the string as-is in code examples |
| 223 | + if (ts.isStringLiteral(exp.body)) { |
| 224 | + const printer = ts.createPrinter() |
| 225 | + if (!exp.body) { |
| 226 | + throw new ParserError('Expected a function body', { |
| 227 | + sourceFile: parserContext.sourceFile, |
| 228 | + node: exp, |
| 229 | + }) |
| 230 | + } |
| 231 | + let exampleCode = printer.printNode(ts.EmitHint.Unspecified, exp.body, sourceFile) |
| 232 | + |
| 233 | + let templateCode = getParsedTemplateHelpersString() + '\n\n' |
| 234 | + |
| 235 | + templateCode += `const figma = require('figma')\n\n` |
| 236 | + |
| 237 | + templateCode += getReferencedPropsForTemplate({ |
| 238 | + propMappings, |
| 239 | + exp, |
| 240 | + sourceFile, |
| 241 | + }) |
| 242 | + |
| 243 | + exampleCode = exampleCode.replace(/`/g, '\\`') |
| 244 | + |
| 245 | + // Body is a string literal, so there aren't any placeholders |
| 246 | + templateCode += `export default figma.value(${exampleCode})\n` |
| 247 | + |
| 248 | + return { |
| 249 | + code: templateCode, |
| 250 | + nestable: true, |
| 251 | + } |
| 252 | + } |
| 253 | + |
221 | 254 | const templateNode = getHtmlTaggedTemplateNode(exp.body) |
222 | 255 |
|
223 | 256 | if (!templateNode) { |
@@ -389,7 +422,6 @@ export function parseExampleTemplate( |
389 | 422 |
|
390 | 423 | templateCode += getReferencedPropsForTemplate({ |
391 | 424 | propMappings, |
392 | | - referencedProps, |
393 | 425 | exp, |
394 | 426 | sourceFile, |
395 | 427 | }) |
|
0 commit comments