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kB,GAAsB,EAAE,CAAA;IAEhD,MAAM,KAAK,GAAc,CAAC,aAAa,CAAC,UAAU,CAAC,CAAA;IACnD,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,EAAG,CAAA;QAC3B,IAAI,kBAAkB,CAAC,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,EAAE,CAAC;YACvD,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,aAAa,EAAE,YAAY,CAAC,CAAA;YAC5D,IAAI,GAAG,EAAE,CAAC;gBACR,GAAG,CAAC,oBAAoB,GAAG,IAAI,CAAA;gBAC/B,kBAAkB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YAC9B,CAAC;QACH,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC,CAAA;IAC3D,CAAC;IAED,IAAI,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,WAAW,CAAC,0CAA0C,EAAE;YAChE,UAAU,EAAE,aAAa,CAAC,UAAU;YACpC,IAAI,EAAE,aAAa,CAAC,UAAU;SAC/B,CAAC,CAAA;IACJ,CAAC;IAED,OAAO,kBAAkB,CAAA;AAC3B,CAAC","sourcesContent":["import ts, { JsxExpression } from 'typescript'\nimport { FIGMA_CONNECT_CALL, PropMappings, valueToString } from './intrinsics'\nimport { highlight, reset } from '../common/logging'\nimport { error } from 'console'\nimport {\n assertIsObjectLiteralExpression,\n assertIsStringLiteral,\n convertObjectLiteralToJs,\n parsePropertyOfType,\n stripQuotesFromNode,\n} from '../typescript/compiler'\nimport { BaseCodeConnectConfig } from './project'\nimport { CodeConnectJSON } from './figma_connect'\n\ninterface ParserErrorContext {\n sourceFile: ts.SourceFile\n node: ts.Node | undefined\n}\n\nexport function getPositionInSourceFile(node: ts.Node, sourceFile: ts.SourceFile) {\n return sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile))\n}\n\nexport class ParserError extends Error {\n sourceFilePosition: ts.LineAndCharacter | null\n sourceFileName: string\n\n constructor(message: string, context?: ParserErrorContext) {\n super(message)\n this.name = 'ParserError'\n this.sourceFileName = context?.sourceFile.fileName || ''\n this.sourceFilePosition =\n context && context.node\n ? getPositionInSourceFile(context.node, context.sourceFile) || null\n : null\n }\n\n toString() {\n let msg = `${highlight(error(this.name))}: ${this.message}\\n`\n if (this.sourceFileName && this.sourceFilePosition) {\n msg += ` -> ${reset(this.sourceFileName)}:${this.sourceFilePosition.line}:${\n this.sourceFilePosition.character\n }\\n`\n }\n return msg\n }\n\n toDebugString() {\n return this.toString() + `\\n ${this.stack}`\n }\n}\n\nexport class InternalError extends ParserError {\n constructor(message: string) {\n super(message)\n this.name = 'InternalError'\n }\n}\n\nexport interface ParserContext {\n checker: ts.TypeChecker\n sourceFile: ts.SourceFile\n resolvedImports: Record\n config: any\n absPath: string\n}\n\n/**\n * Factory to create a function that is used to create `__PROP__(propName)`\n * function nodes, which are used to replace prop references and ultimately\n * replaced.\n *\n * @returns Function to create `__PROP__(propName)` function nodes\n */\nexport function makeCreatePropPlaceholder({\n propMappings,\n referencedProps,\n sourceFile,\n}: {\n /** The prop mappings object */\n propMappings?: PropMappings | undefined\n /** The set of referenced props in the current example */\n referencedProps: Set\n /** The source file */\n sourceFile: ts.SourceFile\n}) {\n return function ({\n name,\n node,\n wrapInJsxExpression = false,\n }: {\n /** The prop name */\n name: string\n /** The props node, used for error reporting */\n node: ts.Node\n /** Whether to wrap the placeholder in a JSX expression node */\n wrapInJsxExpression?: boolean\n }) {\n let propReferenceName = name\n // for nested prop references like `nested.prop`, we only want to look for\n // the prop mapping of `nested`, but include the full `nested.prop` in the\n // __PROP__ call\n if (name.includes('.')) {\n propReferenceName = name.split('.')[0]\n }\n\n // if prop mappings exist, check that the prop reference is in the mappings\n if (propMappings) {\n const mappedProp = propMappings[propReferenceName]\n if (!mappedProp) {\n throw new ParserError(\n `Could not find prop mapping for ${propReferenceName} in the props object`,\n {\n sourceFile,\n node,\n },\n )\n }\n }\n\n referencedProps.add(propReferenceName)\n const callExpression = ts.factory.createCallExpression(\n ts.factory.createIdentifier('__PROP__'),\n undefined,\n [ts.factory.createStringLiteral(name)],\n )\n\n if (wrapInJsxExpression) {\n return ts.factory.createJsxExpression(undefined, callExpression)\n } else {\n return callExpression\n }\n }\n}\n\n/**\n * TS AST visitor function for use with example functions, which replaces\n * references to the `props` argument in various forms in the example code with\n * `__PROP__(propName)` placeholders (created with a createPropPlaceholder\n * function).\n *\n * This is called when transforming the TS AST, and allows us to normalise the\n * different forms of supported prop references into a single representation,\n * which we can then handle consistently (currently we replace the placeholders,\n * using a regex).\n *\n * @returns Placeholder node, or undefined if the node is not a supported prop\n * reference (which results in no transformation)\n */\nexport function visitPropReferencingNode({\n propsParameter,\n node,\n createPropPlaceholder,\n useJsx,\n}: {\n /** The props function parameter node */\n propsParameter: ts.ParameterDeclaration\n /** The node to visit */\n node: ts.Node\n /**\n * The function to create `__PROP__(propName)` function nodes, created by\n * `baseCreatePropPlaceholder`\n * */\n createPropPlaceholder: ReturnType\n /** Whether to support JSX syntax or not */\n useJsx?: boolean\n}): ts.Expression | undefined {\n // `props.` notation\n if (\n ts.isIdentifier(propsParameter.name) &&\n ts.isPropertyAccessExpression(node) &&\n node.expression.getText().startsWith(propsParameter.name.getText())\n ) {\n // nested notation e.g `props.nested.prop`\n if (ts.isPropertyAccessExpression(node.expression)) {\n let current: ts.Node = node\n const parts: string[] = []\n\n // Build the property name by traversing up the chain\n while (ts.isPropertyAccessExpression(current)) {\n parts.unshift(current.name.getText())\n current = current.expression\n }\n\n // Join the parts together to form the full property name\n const name = parts.join('.')\n return createPropPlaceholder({ name, node })\n }\n const name = node.name.getText()\n return createPropPlaceholder({ name, node })\n }\n // `props[\"\"]` notation\n if (\n ts.isIdentifier(propsParameter.name) &&\n ts.isElementAccessExpression(node) &&\n node.expression.getText().startsWith(propsParameter.name.getText()) &&\n ts.isStringLiteral(node.argumentExpression)\n ) {\n const name = stripQuotesFromNode(node.argumentExpression)\n return createPropPlaceholder({ name, node })\n }\n // object destructuring references\n if (ts.isObjectBindingPattern(propsParameter.name)) {\n const isValidNode = useJsx\n ? ts.isJsxExpression(node)\n : ts.isPropertyAccessExpression(node) || ts.isIdentifier(node)\n const target = useJsx ? (node as JsxExpression).expression : node\n\n if (\n isValidNode &&\n target &&\n propsParameter.name.elements.find((el) => target?.getText().startsWith(el.name.getText()))\n ) {\n const name = target.getText()\n return createPropPlaceholder({ name, node, wrapInJsxExpression: useJsx })\n }\n }\n\n return undefined\n}\n\n/**\n * Get template code to create variables referencing the props in the prop\n * mappings. This converts the prop mappings into JS calls like `const propName\n * = figma.properties.string('Prop Name')`, which can then be prepended to the\n * template code.\n *\n * @returns Template code string\n */\nexport function getReferencedPropsForTemplate({\n propMappings = {},\n isForMigration,\n}: {\n /** The prop mappings object */\n propMappings: PropMappings | undefined\n /** The top level node, used for error reporting */\n exp: ts.Node\n /** The source file */\n sourceFile: ts.SourceFile\n isForMigration?: boolean\n}) {\n let templateCode = ''\n\n if (Object.keys(propMappings).length > 0) {\n for (const prop in propMappings) {\n const propMapping = propMappings[prop]\n templateCode += `const ${prop} = ${valueToString(propMapping, undefined, isForMigration)}\\n`\n }\n templateCode += `const __props = {}\\n`\n Object.keys(propMappings).forEach((prop) => {\n // If trying to render prop resulted in an error (e.g. layer was not found\n // because it was invisible), don't include it in the __props object as\n // this will result in a runtime error.\n //\n // TODO Note that this can also happen if there is a typo in the prop name\n // of a nested prop, because we don't validate these at publish time,\n // which would be confusing. Perhaps we should have a way to show a\n // warning but not an error to the user.\n templateCode += `if (${prop} && ${prop}.type !== 'ERROR') {\n __props[\"${prop}\"] = ${prop}\n}\\n`\n })\n templateCode += `\\n`\n }\n\n return templateCode\n}\n\n/**\n * Checks if a file contains Code Connect by looking for the `figma.connect()` function call\n */\nexport function isFigmaConnectFile(\n program: ts.Program,\n file: string,\n extension: string | string[],\n) {\n const allowedExtensions = Array.isArray(extension) ? extension : [extension]\n const fileExtension = file.split('.').pop()\n\n // If the file has no extension, we can't determine if it's a Code Connect file\n if (!fileExtension) {\n return false\n }\n\n // If the file extension is not in the list of supported extensions, it's not a Code Connect file\n if (!allowedExtensions.includes(fileExtension)) {\n return false\n }\n\n const sourceFile = program.getSourceFile(file)\n if (!sourceFile) {\n throw new InternalError(`Could not find source file for ${file}`)\n }\n\n return (\n findDescendants(sourceFile, (node: ts.Node) => {\n if (isFigmaConnectCall(node, sourceFile)) {\n return true\n }\n return false\n }).length > 0\n )\n}\n\n/**\n * Checks if an AST node is a `figma.connect()` call\n *\n * @param node AST node\n * @param sourceFile Source file\n * @returns True if the node is a `figma.connect()` call\n */\nexport function isFigmaConnectCall(\n node: ts.Node,\n sourceFile: ts.SourceFile,\n): node is ts.CallExpression {\n return (\n ts.isCallExpression(node) && node.expression.getText(sourceFile).includes(FIGMA_CONNECT_CALL)\n )\n}\n\nexport function findDescendants(node: ts.Node, cb: (node: ts.Node) => boolean): ts.Node[] {\n const matches: ts.Node[] = []\n function visit(node: ts.Node) {\n if (cb(node)) {\n matches.push(node)\n }\n ts.forEachChild(node, visit)\n }\n visit(node)\n return matches\n}\n\n/**\n * Parses the `links` field of a `figma.connect()` call\n *\n * @param linksArray an ArrayLiteralExpression\n * @param parserContext Parser context\n * @returns An array of link objects\n */\nexport function parseLinks(linksArray: ts.ArrayLiteralExpression, parserContext: ParserContext) {\n const { sourceFile } = parserContext\n const links: { name: string; url: string }[] = []\n for (const element of linksArray.elements) {\n assertIsObjectLiteralExpression(\n element,\n sourceFile,\n `'links' must be an array literal with objects of the format { name: string, url: string }`,\n )\n const name = parsePropertyOfType({\n objectLiteralNode: element,\n propertyName: 'name',\n predicate: ts.isStringLiteral,\n parserContext,\n required: true,\n errorMessage: \"The 'name' property must be a string literal\",\n })\n const url = parsePropertyOfType({\n objectLiteralNode: element,\n propertyName: 'url',\n predicate: ts.isStringLiteral,\n parserContext,\n required: true,\n errorMessage: \"The 'url' property must be a string literal\",\n })\n\n if (name && url) {\n links.push({ name: stripQuotesFromNode(name), url: stripQuotesFromNode(url) })\n }\n }\n\n return links\n}\n\nexport function parseVariant(\n variantMap: ts.ObjectLiteralExpression,\n sourceFile: ts.SourceFile,\n checker: ts.TypeChecker,\n) {\n return convertObjectLiteralToJs(variantMap, sourceFile, checker, (valueNode) => {\n if (\n !ts.isObjectLiteralElement(valueNode) &&\n !ts.isStringLiteral(valueNode) &&\n !ts.isNumericLiteral(valueNode) &&\n valueNode.kind !== ts.SyntaxKind.TrueKeyword &&\n valueNode.kind !== ts.SyntaxKind.FalseKeyword\n ) {\n throw new ParserError(`Invalid value for variant, got: ${valueNode.getText()}`, {\n node: valueNode,\n sourceFile,\n })\n }\n })\n}\n\n/**\n * Parses the `imports` field of a `figma.connect()` call\n *\n * @param importsArray an ArrayLiteralExpression\n * @param parserContext Parser context\n * @returns An array of link objects\n */\nexport function parseImports(\n importsArray: ts.ArrayLiteralExpression,\n parserContext: ParserContext,\n) {\n const { sourceFile } = parserContext\n const imports: string[] = []\n for (const element of importsArray.elements) {\n assertIsStringLiteral(element, sourceFile, `'imports' must be an array literal with strings`)\n imports.push(stripQuotesFromNode(element))\n }\n\n return imports\n}\n\nexport type ParseOptions = {\n repoUrl?: string\n debug?: boolean\n silent?: boolean\n isForMigration?: boolean\n}\n\nexport type ParseFn = (\n node: ts.CallExpression,\n parserContext: ParserContext,\n { repoUrl, silent }: ParseOptions,\n) => Promise\n\nexport type ResolveImportsFn = (\n program: ts.Program,\n sourceFile: ts.SourceFile,\n) => Record\n\nexport async function parseCodeConnect({\n program,\n file,\n config,\n absPath,\n parseFn,\n resolveImportsFn,\n parseOptions = {},\n}: {\n program: ts.Program\n file: string\n config: T\n absPath: string\n parseFn: ParseFn\n resolveImportsFn?: ResolveImportsFn\n parseOptions?: ParseOptions\n}): Promise {\n const sourceFile = program.getSourceFile(file)\n if (!sourceFile) {\n throw new InternalError(`Could not find source file for ${file}`)\n }\n\n const parserContext = {\n checker: program.getTypeChecker(),\n sourceFile,\n resolvedImports: resolveImportsFn ? resolveImportsFn(program, sourceFile) : {},\n config,\n absPath,\n }\n const codeConnectObjects: CodeConnectJSON[] = []\n\n const nodes: ts.Node[] = [parserContext.sourceFile]\n while (nodes.length > 0) {\n const node = nodes.shift()!\n if (isFigmaConnectCall(node, parserContext.sourceFile)) {\n const doc = await parseFn(node, parserContext, parseOptions)\n if (doc) {\n doc._codeConnectFilePath = file\n codeConnectObjects.push(doc)\n }\n }\n nodes.push(...node.getChildren(parserContext.sourceFile))\n }\n\n if (codeConnectObjects.length === 0) {\n throw new ParserError(`Didn't find any calls to figma.connect()`, {\n sourceFile: parserContext.sourceFile,\n node: parserContext.sourceFile,\n })\n }\n\n return codeConnectObjects\n}\n"]}