@yahoo/uds 3.142.0 → 3.143.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/client/Popover/UDSPopoverConfigProvider.d.cts +1 -1
- package/dist/components/client/Popover/UDSPopoverConfigProvider.d.ts +1 -1
- package/dist/styles/styler.d.cts +3 -3
- package/dist/styles/styler.d.ts +3 -3
- package/dist/tailwind/dist/automated-config/dist/mapTextVariantFixtureToValue.js.map +1 -1
- package/dist/tailwind/dist/automated-config/dist/properties.js.map +1 -1
- package/dist/tailwind/dist/automated-config/dist/utils/assertUnreachable.js.map +1 -1
- package/dist/tailwind/dist/automated-config/dist/utils/generateDefaultClassName.js.map +1 -1
- package/dist/tailwind/dist/automated-config/dist/utils/generateSchemaKey.js.map +1 -1
- package/dist/tailwind/dist/automated-config/dist/utils/index.js.map +1 -1
- package/dist/tailwind/dist/automated-config/dist/utils/mapColorFixtureToValue.js.map +1 -1
- package/dist/tailwind/dist/cli/dist/lib/logger.js.map +1 -1
- package/dist/tailwind/dist/cli/dist/lib/print.js.map +1 -1
- package/dist/tailwind/dist/cli/dist/lib/spinner.js.map +1 -1
- package/dist/tailwind/dist/commands/css.helpers.js.map +1 -1
- package/dist/tailwind/dist/commands/css.js.map +1 -1
- package/dist/tailwind/dist/commands/generateComponentData.js.map +1 -1
- package/dist/tailwind/dist/commands/generatePurgeCSSData.js.map +1 -1
- package/dist/tailwind/dist/commands/purge.js.map +1 -1
- package/dist/tailwind/dist/css/generate.helpers.js.map +1 -1
- package/dist/tailwind/dist/css/generate.js.map +1 -1
- package/dist/tailwind/dist/css/nodeUtils.js.map +1 -1
- package/dist/tailwind/dist/css/perf.js.map +1 -1
- package/dist/tailwind/dist/css/postcss.helpers.js.map +1 -1
- package/dist/tailwind/dist/css/postcss.js.map +1 -1
- package/dist/tailwind/dist/css/purgeWorker.js.map +1 -1
- package/dist/tailwind/dist/css/runner.helpers.js.map +1 -1
- package/dist/tailwind/dist/css/runner.js.map +1 -1
- package/dist/tailwind/dist/css/utils.js.map +1 -1
- package/dist/tailwind/dist/css/workerPool.js.map +1 -1
- package/dist/tailwind/dist/css-tokens/dist/index.js.map +1 -1
- package/dist/tailwind/dist/fixtures/dist/index.cjs +0 -10
- package/dist/tailwind/dist/fixtures/dist/index.js +0 -10
- package/dist/tailwind/dist/fixtures/dist/index.js.map +1 -1
- package/dist/tailwind/dist/modes/dist/index.cjs +1 -0
- package/dist/tailwind/dist/modes/dist/index.js +1 -0
- package/dist/tailwind/dist/modes/dist/index.js.map +1 -1
- package/dist/tailwind/dist/plugin.js.map +1 -1
- package/dist/tailwind/dist/purger/legacy/purgeCSS.js.map +1 -1
- package/dist/tailwind/dist/purger/legacy/purgeFromCode.js.map +1 -1
- package/dist/tailwind/dist/purger/optimized/ast/expressions.js.map +1 -1
- package/dist/tailwind/dist/purger/optimized/purge.js.map +1 -1
- package/dist/tailwind/dist/purger/optimized/purgeFromCode.js.map +1 -1
- package/dist/tailwind/dist/purger/optimized/utils/componentAnalyzer.js.map +1 -1
- package/dist/tailwind/dist/purger/optimized/utils/files.js.map +1 -1
- package/dist/tailwind/dist/purger/optimized/utils/safelist.js.map +1 -1
- package/dist/tailwind/dist/runtimeConfig/hookMetadata.js.map +1 -1
- package/dist/tailwind/dist/tailwind/base/addColorModeVars.js.map +1 -1
- package/dist/tailwind/dist/tailwind/base/addColorModeVarsV2.js.map +1 -1
- package/dist/tailwind/dist/tailwind/base/addFontFaceDeclarations.js.map +1 -1
- package/dist/tailwind/dist/tailwind/base/addFontVars.js.map +1 -1
- package/dist/tailwind/dist/tailwind/base/addMotionVars.js.map +1 -1
- package/dist/tailwind/dist/tailwind/base/addScaleModeVars.js.map +1 -1
- package/dist/tailwind/dist/tailwind/components/getFocusRingStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/components/getHitTargetStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/components/getIconStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/components/getInputStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/components/getResponsiveTextStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/a11y.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/blurBgFallback.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/borderRadius.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/borderWidth.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/breakpoints.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/colors.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/components.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/elevation.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/getTailwindAsUdsColors.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/icons.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/input.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/motion.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/nestedBorderRadius.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/scaleMode.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/shadows.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/sizing.js.map +1 -1
- package/dist/tailwind/dist/tailwind/plugins/typography.js.map +1 -1
- package/dist/tailwind/dist/tailwind/theme/getFontFamilyTheme.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/addFontsPlugin.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/composeTailwindPlugins.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getColorModeStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getElevationStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getFontFaceDeclarations.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getFontStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getMotionStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getNestedBorderRadiusUtilities.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getScaleModeStyles.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getShadowPresetValues.js.map +1 -1
- package/dist/tailwind/dist/tailwind/utils/getShadowStyles.js.map +1 -1
- package/dist/tailwind/dist/utils/assertUnreachable.js.map +1 -1
- package/dist/tailwind/dist/utils/entries.js.map +1 -1
- package/dist/tailwind/dist/utils/entryPoints.js.map +1 -1
- package/dist/tailwind/dist/utils/fromEntries.js.map +1 -1
- package/dist/tailwind/dist/utils/generatePurgeCSSData.js.map +1 -1
- package/dist/tailwind/dist/utils/mapValues.js.map +1 -1
- package/dist/tailwind/dist/utils/opacity.js.map +1 -1
- package/dist/tailwind/dist/utils/optimizeCSS.js.map +1 -1
- package/dist/tailwind/dist/utils/parseTokens.js.map +1 -1
- package/dist/tailwind/dist/utils/postcssPreserveVars.js.map +1 -1
- package/dist/tailwind/dist/utils/tsMorph.js.map +1 -1
- package/dist/uds/generated/componentData.cjs +1311 -1311
- package/dist/uds/generated/componentData.js +1311 -1311
- package/dist/uds/generated/tailwindPurge.cjs +3 -0
- package/dist/uds/generated/tailwindPurge.js +3 -0
- package/dist/uds/package.cjs +2 -2
- package/dist/uds/package.js +2 -2
- package/generated/componentData.json +1692 -1692
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tsMorph.js","names":[],"sources":["../../src/utils/tsMorph.ts"],"sourcesContent":["import { camelCase, isEmpty } from 'lodash';\nimport type {\n ArrowFunction,\n CallExpression,\n ClassDeclaration,\n FunctionDeclaration,\n FunctionExpression,\n Identifier,\n JsxElement,\n JsxOpeningElement,\n JsxSelfClosingElement,\n VariableDeclaration,\n} from 'ts-morph';\nimport { Node, SyntaxKind } from 'ts-morph';\n\n/** Determines if an expression is using React.forwardRef. */\nexport function isForwardRefExpression(node: Node): node is CallExpression {\n if (Node.isCallExpression(node)) {\n const expression = node.getExpression();\n\n /**\n * forwardRef(() => <Component />)\n */\n if (Node.isIdentifier(expression) && expression.getText() === 'forwardRef') {\n return true;\n }\n\n /**\n * React.forwardRef(() => <Component />)\n */\n if (\n Node.isPropertyAccessExpression(expression) &&\n expression.getText() === 'React.forwardRef'\n ) {\n return true;\n }\n }\n\n return false;\n}\n\n/** Determines if a node is a JSX component. */\nexport function isJsxComponent(\n node: Node,\n): node is\n | VariableDeclaration\n | FunctionDeclaration\n | FunctionExpression\n | ArrowFunction\n | ClassDeclaration {\n let name: string | undefined;\n\n if (\n Node.isVariableDeclaration(node) ||\n Node.isFunctionDeclaration(node) ||\n Node.isClassDeclaration(node)\n ) {\n name = node.getName();\n } else if (Node.isFunctionExpression(node) || Node.isArrowFunction(node)) {\n const variableDeclaration = node.getFirstAncestorByKind(SyntaxKind.VariableDeclaration);\n name = variableDeclaration?.getName();\n }\n\n return name ? /[A-Z]/.test(name.charAt(0)) : false;\n}\n\n/** Returns a functional component declaration, unwrapping forwardRef if needed. */\nexport function getReactFunctionDeclaration(\n declaration: Node,\n): ArrowFunction | FunctionDeclaration | FunctionExpression | null {\n if (isJsxComponent(declaration)) {\n if (Node.isFunctionDeclaration(declaration)) {\n return declaration;\n }\n\n if (Node.isVariableDeclaration(declaration)) {\n const initializer = declaration.getInitializer();\n\n if (initializer) {\n if (isForwardRefExpression(initializer)) {\n const [initializerDeclaration] = initializer.getArguments();\n if (\n Node.isFunctionDeclaration(initializerDeclaration) ||\n Node.isFunctionExpression(initializerDeclaration) ||\n Node.isArrowFunction(initializerDeclaration)\n ) {\n return initializerDeclaration;\n }\n } else if (\n Node.isFunctionDeclaration(initializer) ||\n Node.isFunctionExpression(initializer) ||\n Node.isArrowFunction(initializer)\n ) {\n return initializer;\n }\n }\n }\n }\n\n return null;\n}\n\n/** Get the first descendant JsxElement based on the identifier. */\nexport function getJsxElement(node: Node, name: string) {\n return getJsxElements(node).find(\n (node) => node.getFirstDescendantByKindOrThrow(SyntaxKind.Identifier).getText() === name,\n );\n}\n\n/** Get all descendant JsxElement nodes. */\nexport function getJsxElements(node: Node) {\n return node\n .getDescendants()\n .filter((node) => Node.isJsxElement(node) || Node.isJsxSelfClosingElement(node)) as (\n | JsxElement\n | JsxSelfClosingElement\n )[];\n}\n\n/**\n * Traces component references.\n *\n * This is similar to `findReferencesAsNodes` but returns JsxSelfClosingElement and JsxElement nodes.\n * Note, this currently does not account for cases where the component is used as a prop or is renamed.\n */\nexport function findReferencesAsJsxElements(\n identifer: Identifier,\n): (JsxOpeningElement | JsxSelfClosingElement)[] {\n const jsxElements: Node[] = [];\n\n for (const reference of identifer.findReferencesAsNodes()) {\n const node = reference.getFirstAncestor((node) => {\n return Node.isJsxOpeningElement(node) || Node.isJsxSelfClosingElement(node);\n });\n\n if (node) {\n jsxElements.push(node);\n }\n }\n\n return jsxElements as (JsxOpeningElement | JsxSelfClosingElement)[];\n}\n\n/**\n * Given a Jsx Reference, this function finds the parent of that reference,\n * looks at the props, and returns the name used either for spread operator rest or\n * the prop name if there's no spread operator\n */\nexport const getParentSpreadOrParamIdentifier = (\n reference: JsxOpeningElement | JsxSelfClosingElement,\n): string | undefined => {\n // get the parent component where this reference is used\n const block = reference.getParentWhile((parent) => {\n return !getReactFunctionDeclaration(parent);\n })!;\n\n const blockParent = block.getParent();\n\n if (!blockParent) {\n return undefined;\n }\n\n // check if the parent is a react component\n const reactComponent = getReactFunctionDeclaration(blockParent);\n\n if (reactComponent) {\n const parameters = reactComponent.getParameters();\n\n for (let i = 0; i < parameters.length; i++) {\n const parameter = parameters[i];\n const objectBindingPattern = parameter.getChildrenOfKind(SyntaxKind.ObjectBindingPattern)[0]; // ({ param1, ...props }: Props)\n const identifier = parameter.getChildrenOfKind(SyntaxKind.Identifier)[0]; // (props: Props)\n\n if (objectBindingPattern) {\n const dotDotToken = objectBindingPattern.getFirstDescendantByKind(\n SyntaxKind.DotDotDotToken,\n );\n const isSpreadOperatorRest = dotDotToken !== undefined;\n if (isSpreadOperatorRest) {\n const spreadRestIdentifier = dotDotToken\n ?.getParent()\n ?.getFirstChildByKindOrThrow(SyntaxKind.Identifier)\n .getText();\n\n return spreadRestIdentifier;\n }\n } else if (identifier) {\n return identifier.getText();\n }\n }\n }\n};\n\n/**\n * Get used props on a reference.\n */\nexport const getUsedPropsInReference = (\n reference: JsxOpeningElement | JsxSelfClosingElement,\n variants: Record<string, Record<string, string>>,\n): Array<[string, string[]]> => {\n const attributes = reference.getAttributes();\n const res: Array<[string, string[]]> = [];\n\n attributes.forEach((attribute) => {\n if (Node.isJsxAttribute(attribute)) {\n const attributeName = attribute.getNameNode().getText();\n // TODO: try to resolve the expression (ex: on the Icon button we know that ony sm value is passed)\n // <Button display=\"flex\" /> or <Button display={\"flex\"} />\n // BORDER_WIDTH === 'thin' ? '0.5' : '1'\n const stringLiterals = attribute.getDescendantsOfKind(SyntaxKind.StringLiteral);\n const values: string[] = [];\n if (stringLiterals.length !== 0) {\n stringLiterals.forEach((stringLiteral) => values.push(stringLiteral.getLiteralText()));\n }\n // Sometimes the property name doesn't directly map to the variant,\n // but instead, the properties type name does. (Ex: `iconSize` is the corresponding variant for `size` prop on Icons)\n const typeName = attribute\n .getFirstChildByKindOrThrow(SyntaxKind.Identifier)\n .getDefinitions()\n .map((d) =>\n d.getDeclarationNode()?.getChildrenOfKind(SyntaxKind.TypeReference).pop()?.getText(),\n )\n .pop();\n\n if (!typeName && !isEmpty(attributeName)) {\n res.push([attributeName, values]);\n } else if (variants[camelCase(typeName) as keyof typeof variants] && !isEmpty(typeName)) {\n res.push([camelCase(typeName), values]);\n } else if (!isEmpty(attributeName)) {\n res.push([attributeName, values]);\n }\n }\n\n if (Node.isJsxSpreadAttribute(attribute)) {\n const componentPropsSpreadIdentifier = getParentSpreadOrParamIdentifier(reference);\n const children = attribute.getExpression().getChildren();\n\n // we need the last element in the spread operator\n // the goal here is to find if we are blindly just passing all the props from the parent\n const lastIdentifier = children.length === 0 ? attribute.getExpression() : children.pop();\n if (!lastIdentifier || lastIdentifier?.getText() === componentPropsSpreadIdentifier) {\n return [];\n }\n\n lastIdentifier\n .getType()\n .getProperties()\n // these are typescript properties\n // so we don't know which values have been used.\n .forEach((property) => {\n // Sometimes the property name doesn't directly map to the variant,\n // but instead, the properties type name does. (Ex: `iconSize` is the corresponding variant for `size` prop on Icons)\n const propertyNameFromType = property.getDeclarations().map((declaration) => {\n const typeName =\n declaration.getChildrenOfKind(SyntaxKind.TypeReference)[0]?.getText() ?? '';\n return camelCase(typeName);\n })[0];\n if (!isEmpty(property.getName())) {\n res.push([property.getName(), []]);\n }\n if (!isEmpty(propertyNameFromType)) {\n res.push([propertyNameFromType, []]);\n }\n });\n }\n\n return [];\n });\n\n return res;\n};\n"],"mappings":";;;;;AAgBA,SAAgB,uBAAuB,MAAoC;AACzE,KAAI,KAAK,iBAAiB,KAAK,EAAE;EAC/B,MAAM,aAAa,KAAK,eAAe;;;;AAKvC,MAAI,KAAK,aAAa,WAAW,IAAI,WAAW,SAAS,KAAK,aAC5D,QAAO;;;;AAMT,MACE,KAAK,2BAA2B,WAAW,IAC3C,WAAW,SAAS,KAAK,mBAEzB,QAAO;;AAIX,QAAO;;;AAIT,SAAgB,eACd,MAMmB;CACnB,IAAI;AAEJ,KACE,KAAK,sBAAsB,KAAK,IAChC,KAAK,sBAAsB,KAAK,IAChC,KAAK,mBAAmB,KAAI,CAE5B,QAAO,KAAK,SAAS;UACZ,KAAK,qBAAqB,KAAK,IAAI,KAAK,gBAAgB,KAAK,CAEtE,QAD4B,KAAK,uBAAuB,WAAW,oBACzC,EAAE,SAAS;AAGvC,QAAO,OAAO,QAAQ,KAAK,KAAK,OAAO,EAAE,CAAC,GAAG;;;AAI/C,SAAgB,4BACd,aACiE;AACjE,KAAI,eAAe,YAAY,EAAE;AAC/B,MAAI,KAAK,sBAAsB,YAAY,CACzC,QAAO;AAGT,MAAI,KAAK,sBAAsB,YAAY,EAAE;GAC3C,MAAM,cAAc,YAAY,gBAAgB;AAEhD,OAAI;QACE,uBAAu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,CAAC;AAEpC,QAAI,CAAC,QAAQ,qBAAqB,CAChC,KAAI,KAAK,CAAC,sBAAsB,EAAE,CAAC,CAAC;KAEtC;;AAGN,SAAO,EAAE;GACT;AAEF,QAAO"}
|
|
1
|
+
{"version":3,"file":"tsMorph.js","names":[],"sources":["../../src/utils/tsMorph.ts"],"sourcesContent":["import { camelCase, isEmpty } from 'lodash';\nimport type {\n ArrowFunction,\n CallExpression,\n ClassDeclaration,\n FunctionDeclaration,\n FunctionExpression,\n Identifier,\n JsxElement,\n JsxOpeningElement,\n JsxSelfClosingElement,\n VariableDeclaration,\n} from 'ts-morph';\nimport { Node, SyntaxKind } from 'ts-morph';\n\n/** Determines if an expression is using React.forwardRef. */\nexport function isForwardRefExpression(node: Node): node is CallExpression {\n if (Node.isCallExpression(node)) {\n const expression = node.getExpression();\n\n /**\n * forwardRef(() => <Component />)\n */\n if (Node.isIdentifier(expression) && expression.getText() === 'forwardRef') {\n return true;\n }\n\n /**\n * React.forwardRef(() => <Component />)\n */\n if (\n Node.isPropertyAccessExpression(expression) &&\n expression.getText() === 'React.forwardRef'\n ) {\n return true;\n }\n }\n\n return false;\n}\n\n/** Determines if a node is a JSX component. */\nexport function isJsxComponent(\n node: Node,\n): node is\n | VariableDeclaration\n | FunctionDeclaration\n | FunctionExpression\n | ArrowFunction\n | ClassDeclaration {\n let name: string | undefined;\n\n if (\n Node.isVariableDeclaration(node) ||\n Node.isFunctionDeclaration(node) ||\n Node.isClassDeclaration(node)\n ) {\n name = node.getName();\n } else if (Node.isFunctionExpression(node) || Node.isArrowFunction(node)) {\n const variableDeclaration = node.getFirstAncestorByKind(SyntaxKind.VariableDeclaration);\n name = variableDeclaration?.getName();\n }\n\n return name ? /[A-Z]/.test(name.charAt(0)) : false;\n}\n\n/** Returns a functional component declaration, unwrapping forwardRef if needed. */\nexport function getReactFunctionDeclaration(\n declaration: Node,\n): ArrowFunction | FunctionDeclaration | FunctionExpression | null {\n if (isJsxComponent(declaration)) {\n if (Node.isFunctionDeclaration(declaration)) {\n return declaration;\n }\n\n if (Node.isVariableDeclaration(declaration)) {\n const initializer = declaration.getInitializer();\n\n if (initializer) {\n if (isForwardRefExpression(initializer)) {\n const [initializerDeclaration] = initializer.getArguments();\n if (\n Node.isFunctionDeclaration(initializerDeclaration) ||\n Node.isFunctionExpression(initializerDeclaration) ||\n Node.isArrowFunction(initializerDeclaration)\n ) {\n return initializerDeclaration;\n }\n } else if (\n Node.isFunctionDeclaration(initializer) ||\n Node.isFunctionExpression(initializer) ||\n Node.isArrowFunction(initializer)\n ) {\n return initializer;\n }\n }\n }\n }\n\n return null;\n}\n\n/** Get the first descendant JsxElement based on the identifier. */\nexport function getJsxElement(node: Node, name: string) {\n return getJsxElements(node).find(\n (node) => node.getFirstDescendantByKindOrThrow(SyntaxKind.Identifier).getText() === name,\n );\n}\n\n/** Get all descendant JsxElement nodes. */\nexport function getJsxElements(node: Node) {\n return node\n .getDescendants()\n .filter((node) => Node.isJsxElement(node) || Node.isJsxSelfClosingElement(node)) as (\n | JsxElement\n | JsxSelfClosingElement\n )[];\n}\n\n/**\n * Traces component references.\n *\n * This is similar to `findReferencesAsNodes` but returns JsxSelfClosingElement and JsxElement nodes.\n * Note, this currently does not account for cases where the component is used as a prop or is renamed.\n */\nexport function findReferencesAsJsxElements(\n identifer: Identifier,\n): (JsxOpeningElement | JsxSelfClosingElement)[] {\n const jsxElements: Node[] = [];\n\n for (const reference of identifer.findReferencesAsNodes()) {\n const node = reference.getFirstAncestor((node) => {\n return Node.isJsxOpeningElement(node) || Node.isJsxSelfClosingElement(node);\n });\n\n if (node) {\n jsxElements.push(node);\n }\n }\n\n return jsxElements as (JsxOpeningElement | JsxSelfClosingElement)[];\n}\n\n/**\n * Given a Jsx Reference, this function finds the parent of that reference,\n * looks at the props, and returns the name used either for spread operator rest or\n * the prop name if there's no spread operator\n */\nexport const getParentSpreadOrParamIdentifier = (\n reference: JsxOpeningElement | JsxSelfClosingElement,\n): string | undefined => {\n // get the parent component where this reference is used\n const block = reference.getParentWhile((parent) => {\n return !getReactFunctionDeclaration(parent);\n })!;\n\n const blockParent = block.getParent();\n\n if (!blockParent) {\n return undefined;\n }\n\n // check if the parent is a react component\n const reactComponent = getReactFunctionDeclaration(blockParent);\n\n if (reactComponent) {\n const parameters = reactComponent.getParameters();\n\n for (let i = 0; i < parameters.length; i++) {\n const parameter = parameters[i];\n const objectBindingPattern = parameter.getChildrenOfKind(SyntaxKind.ObjectBindingPattern)[0]; // ({ param1, ...props }: Props)\n const identifier = parameter.getChildrenOfKind(SyntaxKind.Identifier)[0]; // (props: Props)\n\n if (objectBindingPattern) {\n const dotDotToken = objectBindingPattern.getFirstDescendantByKind(\n SyntaxKind.DotDotDotToken,\n );\n const isSpreadOperatorRest = dotDotToken !== undefined;\n if (isSpreadOperatorRest) {\n const spreadRestIdentifier = dotDotToken\n ?.getParent()\n ?.getFirstChildByKindOrThrow(SyntaxKind.Identifier)\n .getText();\n\n return spreadRestIdentifier;\n }\n } else if (identifier) {\n return identifier.getText();\n }\n }\n }\n};\n\n/**\n * Get used props on a reference.\n */\nexport const getUsedPropsInReference = (\n reference: JsxOpeningElement | JsxSelfClosingElement,\n variants: Record<string, Record<string, string>>,\n): Array<[string, string[]]> => {\n const attributes = reference.getAttributes();\n const res: Array<[string, string[]]> = [];\n\n attributes.forEach((attribute) => {\n if (Node.isJsxAttribute(attribute)) {\n const attributeName = attribute.getNameNode().getText();\n // TODO: try to resolve the expression (ex: on the Icon button we know that ony sm value is passed)\n // <Button display=\"flex\" /> or <Button display={\"flex\"} />\n // BORDER_WIDTH === 'thin' ? '0.5' : '1'\n const stringLiterals = attribute.getDescendantsOfKind(SyntaxKind.StringLiteral);\n const values: string[] = [];\n if (stringLiterals.length !== 0) {\n stringLiterals.forEach((stringLiteral) => values.push(stringLiteral.getLiteralText()));\n }\n // Sometimes the property name doesn't directly map to the variant,\n // but instead, the properties type name does. (Ex: `iconSize` is the corresponding variant for `size` prop on Icons)\n const typeName = attribute\n .getFirstChildByKindOrThrow(SyntaxKind.Identifier)\n .getDefinitions()\n .map((d) =>\n d.getDeclarationNode()?.getChildrenOfKind(SyntaxKind.TypeReference).pop()?.getText(),\n )\n .pop();\n\n if (!typeName && !isEmpty(attributeName)) {\n res.push([attributeName, values]);\n } else if (variants[camelCase(typeName) as keyof typeof variants] && !isEmpty(typeName)) {\n res.push([camelCase(typeName), values]);\n } else if (!isEmpty(attributeName)) {\n res.push([attributeName, values]);\n }\n }\n\n if (Node.isJsxSpreadAttribute(attribute)) {\n const componentPropsSpreadIdentifier = getParentSpreadOrParamIdentifier(reference);\n const children = attribute.getExpression().getChildren();\n\n // we need the last element in the spread operator\n // the goal here is to find if we are blindly just passing all the props from the parent\n const lastIdentifier = children.length === 0 ? attribute.getExpression() : children.pop();\n if (!lastIdentifier || lastIdentifier?.getText() === componentPropsSpreadIdentifier) {\n return [];\n }\n\n lastIdentifier\n .getType()\n .getProperties()\n // these are typescript properties\n // so we don't know which values have been used.\n .forEach((property) => {\n // Sometimes the property name doesn't directly map to the variant,\n // but instead, the properties type name does. (Ex: `iconSize` is the corresponding variant for `size` prop on Icons)\n const propertyNameFromType = property.getDeclarations().map((declaration) => {\n const typeName =\n declaration.getChildrenOfKind(SyntaxKind.TypeReference)[0]?.getText() ?? '';\n return camelCase(typeName);\n })[0];\n if (!isEmpty(property.getName())) {\n res.push([property.getName(), []]);\n }\n if (!isEmpty(propertyNameFromType)) {\n res.push([propertyNameFromType, []]);\n }\n });\n }\n\n return [];\n });\n\n return res;\n};\n"],"mappings":";;;;;AAgBA,SAAgB,uBAAuB,MAAoC;CACzE,IAAI,KAAK,iBAAiB,KAAK,EAAE;EAC/B,MAAM,aAAa,KAAK,eAAe;;;;EAKvC,IAAI,KAAK,aAAa,WAAW,IAAI,WAAW,SAAS,KAAK,cAC5D,OAAO;;;;EAMT,IACE,KAAK,2BAA2B,WAAW,IAC3C,WAAW,SAAS,KAAK,oBAEzB,OAAO;;CAIX,OAAO;;;AAIT,S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