|
| 1 | +/* |
| 2 | +Copyright 2026 Element Creations Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { ESLintUtils } from "@typescript-eslint/utils"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Node types that introduce a new non-module scope. A getChild() call nested |
| 12 | + * inside any of these is considered "not at the top level". |
| 13 | + */ |
| 14 | +const FUNCTION_OR_CLASS_TYPES = new Set([ |
| 15 | + "FunctionDeclaration", |
| 16 | + "FunctionExpression", |
| 17 | + "ArrowFunctionExpression", |
| 18 | + "ClassBody", |
| 19 | +]); |
| 20 | + |
| 21 | +const rule = ESLintUtils.RuleCreator( |
| 22 | + () => "https://github.com/element-hq/element-call", |
| 23 | +)({ |
| 24 | + name: "no-top-level-logger-get-child", |
| 25 | + meta: { |
| 26 | + type: "problem", |
| 27 | + docs: { |
| 28 | + description: |
| 29 | + "Disallow calling logger.getChild() at the top level of a module." + |
| 30 | + "`getChild` has to be called after the rageshake logger `init()`." + |
| 31 | + "If it is called at the top level the child logger will never be setup for rageshakes.", |
| 32 | + }, |
| 33 | + messages: { |
| 34 | + noTopLevelGetChild: |
| 35 | + "Do not call logger.getChild() at the top level of a module; move it inside a function or class instead that gets called after rageshake logger `init()` is called.", |
| 36 | + }, |
| 37 | + schema: [], |
| 38 | + }, |
| 39 | + create(context) { |
| 40 | + // Tracks the local binding names that refer to the logger imported from |
| 41 | + // 'matrix-js-sdk/lib/logger', e.g. both `logger` and `rootLogger` in: |
| 42 | + // import { logger } from "matrix-js-sdk/lib/logger"; |
| 43 | + // import { logger as rootLogger } from "matrix-js-sdk/lib/logger"; |
| 44 | + const loggerNames = new Set(); |
| 45 | + |
| 46 | + return { |
| 47 | + ImportDeclaration(node) { |
| 48 | + if (node.source.value !== "matrix-js-sdk/lib/logger") return; |
| 49 | + for (const specifier of node.specifiers) { |
| 50 | + if ( |
| 51 | + specifier.type === "ImportSpecifier" && |
| 52 | + specifier.imported.name === "logger" |
| 53 | + ) { |
| 54 | + loggerNames.add(specifier.local.name); |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + |
| 59 | + CallExpression(node) { |
| 60 | + // Must be a non-computed member expression call: something.getChild(...) |
| 61 | + if ( |
| 62 | + node.callee.type !== "MemberExpression" || |
| 63 | + node.callee.computed || |
| 64 | + node.callee.property.type !== "Identifier" || |
| 65 | + node.callee.property.name !== "getChild" |
| 66 | + ) |
| 67 | + return; |
| 68 | + |
| 69 | + // The receiver must be one of the tracked logger names. |
| 70 | + const object = node.callee.object; |
| 71 | + if (object.type !== "Identifier" || !loggerNames.has(object.name)) |
| 72 | + return; |
| 73 | + |
| 74 | + // Flag the call only when it is at module top level — i.e. there is no |
| 75 | + // enclosing function or class body anywhere in the ancestor chain. |
| 76 | + const ancestors = context.sourceCode.getAncestors(node); |
| 77 | + const isTopLevel = !ancestors.some((a) => |
| 78 | + FUNCTION_OR_CLASS_TYPES.has(a.type), |
| 79 | + ); |
| 80 | + |
| 81 | + if (isTopLevel) { |
| 82 | + context.report({ |
| 83 | + messageId: "noTopLevelGetChild", |
| 84 | + node, |
| 85 | + }); |
| 86 | + } |
| 87 | + }, |
| 88 | + }; |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +export default rule; |
0 commit comments