|
| 1 | +/** |
| 2 | + * A link is followed in ONE way: the lib's navigation executor (`executeNavigation`, `executeNavigationImperative`, |
| 3 | + * `executeNewTabNavigation`, the hooks built on it), which hands a same-tab link to the host's `runtime.navigation.navigate` and lets the browser |
| 4 | + * open what the host does not take. Nothing beside it navigates: no fallback option handed to it, and no router push or |
| 5 | + * `location` write in the same function as a call to it. A second path is how a host's routing gets skipped quietly |
| 6 | + * (CU-17tkuw61kfy: every caller passed `fallbackNavigate: router.push`). If a host does not navigate, wire its |
| 7 | + * `navigate`; never route around it. |
| 8 | + */ |
| 9 | + |
| 10 | +/** The executor and the hooks that call it. */ |
| 11 | +const EXECUTORS = new Set([ |
| 12 | + 'executeNavigation', |
| 13 | + 'executeNavigationImperative', |
| 14 | + 'executeNewTabNavigation', |
| 15 | + 'useNavLinkViaRuntime', |
| 16 | + 'useNavigateViaRuntime', |
| 17 | +]); |
| 18 | + |
| 19 | +/** `router.push(...)`, `router.replace(...)`: a router call on anything named like a router. */ |
| 20 | +const ROUTER_METHOD = new Set(['push', 'replace']); |
| 21 | +const LOCATION_METHOD = new Set(['assign', 'replace']); |
| 22 | + |
| 23 | +function calleeName(callee) { |
| 24 | + if (callee.type === 'Identifier') return callee.name; |
| 25 | + if (callee.type === 'MemberExpression' && !callee.computed && callee.property.type === 'Identifier') { |
| 26 | + return callee.property.name; |
| 27 | + } |
| 28 | + return null; |
| 29 | +} |
| 30 | + |
| 31 | +function objectName(node) { |
| 32 | + if (node.type === 'Identifier') return node.name; |
| 33 | + if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier') { |
| 34 | + return node.property.name; |
| 35 | + } |
| 36 | + if (node.type === 'CallExpression') return calleeName(node.callee); |
| 37 | + return null; |
| 38 | +} |
| 39 | + |
| 40 | +/** A direct navigation: `router.push(x)`, `location.assign(x)`, `window.open(x)`, or a `location.href =` write. */ |
| 41 | +function directNavigation(node) { |
| 42 | + if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && !node.callee.computed) { |
| 43 | + const method = node.callee.property.name; |
| 44 | + const owner = objectName(node.callee.object); |
| 45 | + if (!owner) return null; |
| 46 | + if (ROUTER_METHOD.has(method) && /router$/i.test(owner)) return `${owner}.${method}()`; |
| 47 | + if (LOCATION_METHOD.has(method) && owner === 'location') return `location.${method}()`; |
| 48 | + if (method === 'open' && owner === 'window') return 'window.open()'; |
| 49 | + } |
| 50 | + if ( |
| 51 | + node.type === 'AssignmentExpression' && |
| 52 | + node.left.type === 'MemberExpression' && |
| 53 | + !node.left.computed && |
| 54 | + node.left.property.name === 'href' && |
| 55 | + objectName(node.left.object) === 'location' |
| 56 | + ) { |
| 57 | + return 'location.href ='; |
| 58 | + } |
| 59 | + return null; |
| 60 | +} |
| 61 | + |
| 62 | +const FUNCTION_TYPES = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression']); |
| 63 | + |
| 64 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 65 | +const noNavigationFallback = { |
| 66 | + meta: { |
| 67 | + type: 'problem', |
| 68 | + docs: { description: 'Disallow a second way to navigate beside the lib navigation executor' }, |
| 69 | + schema: [], |
| 70 | + messages: { |
| 71 | + option: |
| 72 | + "`{{key}}` hands {{executor}} a second way to navigate. Links go through the host's " + |
| 73 | + '`runtime.navigation.navigate`; a host that does not navigate wires `navigate`, never a fallback.', |
| 74 | + beside: |
| 75 | + '{{call}} navigates beside {{executor}} in the same function. Follow a link through the executor only; a ' + |
| 76 | + 'host that does not navigate wires `runtime.navigation.navigate`.', |
| 77 | + }, |
| 78 | + }, |
| 79 | + create(context) { |
| 80 | + // The module that DEFINES the executor is where navigating is implemented; the rule guards its callers. |
| 81 | + const definesExecutor = context.sourceCode.ast.body.some(statement => { |
| 82 | + const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement; |
| 83 | + return declaration?.type === 'FunctionDeclaration' && EXECUTORS.has(declaration.id?.name); |
| 84 | + }); |
| 85 | + if (definesExecutor) return {}; |
| 86 | + /** Per enclosing function: the executor calls and the direct navigations anywhere inside it (nested handlers |
| 87 | + * included). The file itself is a frame too, but only for its top-level code: two components in one file are |
| 88 | + * two functions. */ |
| 89 | + const frames = []; |
| 90 | + const reported = new Set(); |
| 91 | + const enter = () => frames.push({ executors: [], direct: [] }); |
| 92 | + const exit = () => { |
| 93 | + const frame = frames.pop(); |
| 94 | + if (!frame || frame.executors.length === 0) return; |
| 95 | + for (const { node, call } of frame.direct) { |
| 96 | + if (reported.has(node)) continue; |
| 97 | + reported.add(node); |
| 98 | + context.report({ node, messageId: 'beside', data: { call, executor: frame.executors[0] } }); |
| 99 | + } |
| 100 | + }; |
| 101 | + /** The frames a fact belongs to: every enclosing function, or the file when at the top level. */ |
| 102 | + const owners = () => (frames.length > 1 ? frames.slice(1) : frames); |
| 103 | + const noteDirect = node => { |
| 104 | + const call = directNavigation(node); |
| 105 | + if (call) for (const frame of owners()) frame.direct.push({ node, call }); |
| 106 | + }; |
| 107 | + |
| 108 | + return { |
| 109 | + Program: enter, |
| 110 | + 'Program:exit': exit, |
| 111 | + ...Object.fromEntries( |
| 112 | + [...FUNCTION_TYPES].flatMap(t => [ |
| 113 | + [t, enter], |
| 114 | + [`${t}:exit`, exit], |
| 115 | + ]), |
| 116 | + ), |
| 117 | + CallExpression(node) { |
| 118 | + const name = calleeName(node.callee); |
| 119 | + if (name && EXECUTORS.has(name)) { |
| 120 | + for (const frame of owners()) frame.executors.push(name); |
| 121 | + for (const arg of node.arguments) { |
| 122 | + if (arg.type !== 'ObjectExpression') continue; |
| 123 | + for (const prop of arg.properties) { |
| 124 | + const key = prop.type === 'Property' && !prop.computed ? (prop.key.name ?? prop.key.value) : null; |
| 125 | + if (typeof key === 'string' && /^fallback/i.test(key)) { |
| 126 | + context.report({ node: prop, messageId: 'option', data: { key, executor: name } }); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return; |
| 131 | + } |
| 132 | + noteDirect(node); |
| 133 | + }, |
| 134 | + AssignmentExpression: noteDirect, |
| 135 | + }; |
| 136 | + }, |
| 137 | +}; |
| 138 | + |
| 139 | +export const navigationPlugin = { |
| 140 | + meta: { name: 'flamingo-navigation' }, |
| 141 | + rules: { 'no-navigation-fallback': noNavigationFallback }, |
| 142 | +}; |
0 commit comments