|
| 1 | +/** |
| 2 | + * Components tracked by this rule. |
| 3 | + * |
| 4 | + * @type {Set<string>} |
| 5 | + */ |
| 6 | +const TRACKED_COMPONENTS = new Set( [ 'Link', 'Text', 'VisuallyHidden' ] ); |
| 7 | + |
| 8 | +/** |
| 9 | + * @type {import('eslint').Rule.RuleModule} |
| 10 | + */ |
| 11 | +module.exports = { |
| 12 | + meta: { |
| 13 | + type: 'problem', |
| 14 | + docs: { |
| 15 | + description: |
| 16 | + 'Prevent render-prop composition orders that silently remove semantics.', |
| 17 | + url: 'https://github.com/WordPress/gutenberg/blob/HEAD/packages/eslint-plugin/docs/rules/no-unsafe-render-order.md', |
| 18 | + }, |
| 19 | + schema: [ |
| 20 | + { |
| 21 | + type: 'object', |
| 22 | + properties: { |
| 23 | + checkLocalImports: { |
| 24 | + type: 'boolean', |
| 25 | + description: |
| 26 | + 'When true, also checks tracked components imported from relative paths.', |
| 27 | + }, |
| 28 | + }, |
| 29 | + additionalProperties: false, |
| 30 | + }, |
| 31 | + ], |
| 32 | + messages: { |
| 33 | + visuallyHiddenOrder: |
| 34 | + 'Do not pass `VisuallyHidden` via `render`. Make `VisuallyHidden` the outer component instead.', |
| 35 | + linkTextOrder: |
| 36 | + 'Use `Text` as the outer component and pass `Link` via `render` so the resulting element stays an `<a>`.', |
| 37 | + }, |
| 38 | + }, |
| 39 | + |
| 40 | + create( context ) { |
| 41 | + const checkLocalImports = |
| 42 | + context.options[ 0 ]?.checkLocalImports ?? false; |
| 43 | + const trackedImports = new Map(); |
| 44 | + |
| 45 | + /** |
| 46 | + * @param {string} source |
| 47 | + * @return {boolean} Whether the import should be tracked. |
| 48 | + */ |
| 49 | + function shouldTrackImportSource( source ) { |
| 50 | + if ( source === '@wordpress/ui' ) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + if ( checkLocalImports ) { |
| 55 | + return source.startsWith( '.' ) || source.startsWith( '/' ); |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param {import('estree-jsx').JSXIdentifier|import('estree-jsx').JSXMemberExpression} node |
| 63 | + * @return {string|null} Tracked component name or null. |
| 64 | + */ |
| 65 | + function resolveTrackedJsxName( node ) { |
| 66 | + if ( node.type !== 'JSXIdentifier' ) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + return trackedImports.get( node.name ) ?? null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param {Array} attributes JSX attributes to inspect. |
| 75 | + * @param {string} attributeName Attribute name to match. |
| 76 | + * @return {import('estree-jsx').JSXAttribute|undefined} Matching attribute. |
| 77 | + */ |
| 78 | + function getJsxAttribute( attributes, attributeName ) { |
| 79 | + return attributes.find( |
| 80 | + ( attribute ) => |
| 81 | + attribute.type === 'JSXAttribute' && |
| 82 | + attribute.name?.name === attributeName |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param {Array} attributes |
| 88 | + * @return {string|null} Resolved JSX name inside `render={ <... /> }`. |
| 89 | + */ |
| 90 | + function getRenderedComponentName( attributes ) { |
| 91 | + const renderAttribute = getJsxAttribute( attributes, 'render' ); |
| 92 | + |
| 93 | + if ( |
| 94 | + ! renderAttribute?.value || |
| 95 | + renderAttribute.value.type !== 'JSXExpressionContainer' || |
| 96 | + renderAttribute.value.expression.type !== 'JSXElement' |
| 97 | + ) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + return resolveTrackedJsxName( |
| 102 | + renderAttribute.value.expression.openingElement.name |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + ImportDeclaration( node ) { |
| 108 | + const source = node.source.value; |
| 109 | + |
| 110 | + if ( |
| 111 | + typeof source !== 'string' || |
| 112 | + ! shouldTrackImportSource( source ) |
| 113 | + ) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + node.specifiers.forEach( ( specifier ) => { |
| 118 | + if ( specifier.type === 'ImportSpecifier' ) { |
| 119 | + const importedName = specifier.imported.name; |
| 120 | + |
| 121 | + if ( TRACKED_COMPONENTS.has( importedName ) ) { |
| 122 | + trackedImports.set( |
| 123 | + specifier.local.name, |
| 124 | + importedName |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + } ); |
| 129 | + }, |
| 130 | + |
| 131 | + JSXOpeningElement( node ) { |
| 132 | + const renderedComponentName = getRenderedComponentName( |
| 133 | + node.attributes |
| 134 | + ); |
| 135 | + |
| 136 | + if ( renderedComponentName === 'VisuallyHidden' ) { |
| 137 | + context.report( { |
| 138 | + node, |
| 139 | + messageId: 'visuallyHiddenOrder', |
| 140 | + } ); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + const elementName = resolveTrackedJsxName( node.name ); |
| 145 | + |
| 146 | + if ( |
| 147 | + elementName === 'Link' && |
| 148 | + renderedComponentName === 'Text' |
| 149 | + ) { |
| 150 | + context.report( { |
| 151 | + node, |
| 152 | + messageId: 'linkTextOrder', |
| 153 | + } ); |
| 154 | + } |
| 155 | + }, |
| 156 | + }; |
| 157 | + }, |
| 158 | +}; |
0 commit comments