JSXOpeningElement()

in src/rules/no-deprecated-colors.js [45:110]


      JSXOpeningElement(node) {
        // Skip if component was not imported from @primer/react
        if (!skipImportCheck && !isPrimerComponent(node.name, context.getScope(node))) {
          return
        }

        for (const attribute of node.attributes) {
          if (!attribute.name || !attribute.value) {
            continue
          }

          const propName = attribute.name.name
          const propValue = attribute.value.value

          // Check for the sx prop
          if (propName === 'sx' && attribute.value.expression.type === 'ObjectExpression') {
            // Search all properties of the sx object (even nested properties)
            traverse(context, attribute.value, path => {
              if (path.node.type === 'Property' && path.node.value.type === 'Literal') {
                const prop = path.node
                const propName = prop.key.name
                const propValue = prop.value.value

                if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
                  replaceDeprecatedColor(context, prop.value, propValue)
                  visitedStrings.add(prop.value)
                }
              }

              // Check functions passed to sx object properties
              // (e.g. boxShadow: theme => `0 1px 2px ${theme.colors.text.primary}` )
              if (path.node.type === 'Property' && path.node.value.type === 'ArrowFunctionExpression') {
                traverse(context, path.node.value.body, path => {
                  if (path.node.type === 'MemberExpression') {
                    // Convert MemberExpression AST to string
                    const code = context.getSourceCode().getText(path.node)

                    const [param, key, ...rest] = code.split('.')
                    const name = rest.join('.')

                    if (['colors', 'shadows'].includes(key) && Object.keys(deprecations).includes(name)) {
                      replaceDeprecatedColor(
                        context,
                        path.node,
                        name,
                        str => [param, key, str].join('.'),
                        str => str
                      )
                    }

                    // Don't traverse any nested member expressions.
                    // The root-level member expression gives us all the data we need.
                    return traverse.SKIP
                  }
                })
              }
            })
          }

          // Check if styled-system color prop is using a deprecated color
          if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
            replaceDeprecatedColor(context, attribute.value, propValue)
            visitedStrings.add(attribute.value)
          }
        }
      },