* Extract component names referenced in the `plugins` prop of a devtools element.
(opening: JSXOpeningElement)
| 8 | * Extract component names referenced in the `plugins` prop of a devtools element. |
| 9 | */ |
| 10 | function getPluginReferences(opening: JSXOpeningElement): Array<string> { |
| 11 | const refs: Array<string> = [] |
| 12 | |
| 13 | for (const attr of opening.attributes) { |
| 14 | if (attr.type !== 'JSXAttribute') continue |
| 15 | if (attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'plugins') |
| 16 | continue |
| 17 | if ( |
| 18 | !attr.value || |
| 19 | attr.value.type !== 'JSXExpressionContainer' || |
| 20 | attr.value.expression.type !== 'ArrayExpression' |
| 21 | ) |
| 22 | continue |
| 23 | |
| 24 | for (const el of attr.value.expression.elements) { |
| 25 | if (!el || el.type !== 'ObjectExpression') continue |
| 26 | |
| 27 | for (const prop of el.properties) { |
| 28 | if ( |
| 29 | prop.type !== 'Property' || |
| 30 | prop.key.type !== 'Identifier' || |
| 31 | prop.key.name !== 'render' |
| 32 | ) |
| 33 | continue |
| 34 | |
| 35 | const value = prop.value |
| 36 | |
| 37 | // handle <ReactRouterPanel /> |
| 38 | if ( |
| 39 | value.type === 'JSXElement' && |
| 40 | value.openingElement.name.type === 'JSXIdentifier' |
| 41 | ) { |
| 42 | refs.push(value.openingElement.name.name) |
| 43 | } |
| 44 | // handle () => <ReactRouterPanel /> |
| 45 | else if ( |
| 46 | value.type === 'ArrowFunctionExpression' || |
| 47 | value.type === 'FunctionExpression' |
| 48 | ) { |
| 49 | const body = value.body |
| 50 | if ( |
| 51 | body && |
| 52 | body.type === 'JSXElement' && |
| 53 | body.openingElement.name.type === 'JSXIdentifier' |
| 54 | ) { |
| 55 | refs.push(body.openingElement.name.name) |
| 56 | } |
| 57 | } |
| 58 | // handle render: SomeComponent |
| 59 | else if (value.type === 'Identifier') { |
| 60 | refs.push(value.name) |
| 61 | } |
| 62 | // handle render: someFunction() |
| 63 | else if ( |
| 64 | value.type === 'CallExpression' && |
| 65 | value.callee.type === 'Identifier' |
| 66 | ) { |
| 67 | refs.push(value.callee.name) |