@dloizides/frontend-devtools 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2139,6 +2139,171 @@ var require_stable_hook_args_default = {
2139
2139
  }
2140
2140
  };
2141
2141
 
2142
+ // src/rules/single-row-action-source.js
2143
+ var UI_BUTTONS_MODULE = "@dloizides/ui-buttons";
2144
+ var GROUP_NAME = "RowActionGroup";
2145
+ var ACTIONS_PROP = "actions";
2146
+ var SPEC_TYPE = "RowActionSpec";
2147
+ var MEMO_HOOKS = /* @__PURE__ */ new Set(["useMemo", "useCallback"]);
2148
+ var MAX_DEPTH = 8;
2149
+ var Source = Object.freeze({ LOCAL: "local", EXTERNAL: "external" });
2150
+ function getScopeFor(context, node) {
2151
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
2152
+ if (typeof sourceCode.getScope === "function") return sourceCode.getScope(node);
2153
+ return context.getScope();
2154
+ }
2155
+ function findVariable(scope, name) {
2156
+ let current = scope;
2157
+ while (current) {
2158
+ const found = current.set.get(name);
2159
+ if (found) return found;
2160
+ current = current.upper;
2161
+ }
2162
+ return void 0;
2163
+ }
2164
+ function rootIdentifier(node) {
2165
+ let current = node;
2166
+ while (current && current.type === "MemberExpression") current = current.object;
2167
+ return current && current.type === "Identifier" ? current : void 0;
2168
+ }
2169
+ function calleeName(callee) {
2170
+ if (callee.type === "Identifier") return callee.name;
2171
+ if (callee.type === "MemberExpression" && callee.property.type === "Identifier") return callee.property.name;
2172
+ return void 0;
2173
+ }
2174
+ function isSpecArrayType(annotation) {
2175
+ const type = annotation && annotation.typeAnnotation;
2176
+ if (!type) return false;
2177
+ const isSpecRef = (ref) => ref && ref.type === "TSTypeReference" && ref.typeName.name === SPEC_TYPE;
2178
+ if (type.type === "TSArrayType") return isSpecRef(type.elementType);
2179
+ if (type.type === "TSTypeOperator") return type.typeAnnotation.type === "TSArrayType" && isSpecRef(type.typeAnnotation.elementType);
2180
+ const params = type.typeParameters && type.typeParameters.params;
2181
+ return type.type === "TSTypeReference" && params !== void 0 && params.some(isSpecRef);
2182
+ }
2183
+ function returnedExpressions(fn) {
2184
+ if (fn.body.type !== "BlockStatement") return [fn.body];
2185
+ const found = [];
2186
+ const visit = (node) => {
2187
+ if (!node || typeof node.type !== "string") return;
2188
+ if (node.type === "ReturnStatement") {
2189
+ if (node.argument) found.push(node.argument);
2190
+ return;
2191
+ }
2192
+ if (node.type.includes("Function")) return;
2193
+ for (const key of Object.keys(node)) {
2194
+ if (key === "parent") continue;
2195
+ const child = node[key];
2196
+ if (Array.isArray(child)) child.forEach(visit);
2197
+ else if (child && typeof child.type === "string") visit(child);
2198
+ }
2199
+ };
2200
+ fn.body.body.forEach(visit);
2201
+ return found;
2202
+ }
2203
+ function isFunctionNode(node) {
2204
+ return node && (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration");
2205
+ }
2206
+ function createClassifier(context) {
2207
+ const anyLocal = (nodes, depth, seen) => nodes.some((n) => classify(n, depth, seen) === Source.LOCAL);
2208
+ function classifyVariable(variable, depth, seen) {
2209
+ if (seen.has(variable)) return Source.EXTERNAL;
2210
+ seen.add(variable);
2211
+ const def = variable.defs[0];
2212
+ if (!def) return Source.EXTERNAL;
2213
+ if (def.type === "ImportBinding" || def.type === "Parameter") return Source.EXTERNAL;
2214
+ if (def.type === "FunctionName") return Source.LOCAL;
2215
+ if (def.type !== "Variable" || def.node.id.type !== "Identifier") return Source.EXTERNAL;
2216
+ const writes = variable.references.filter((ref) => ref.isWrite() && ref.writeExpr).map((ref) => ref.writeExpr);
2217
+ if (writes.length === 0) return isSpecArrayType(def.node.id.typeAnnotation) ? Source.LOCAL : Source.EXTERNAL;
2218
+ return anyLocal(writes, depth + 1, seen) ? Source.LOCAL : Source.EXTERNAL;
2219
+ }
2220
+ function classifyCall(node, depth, seen) {
2221
+ const name = calleeName(node.callee);
2222
+ if (MEMO_HOOKS.has(name) && isFunctionNode(node.arguments[0])) {
2223
+ return anyLocal(returnedExpressions(node.arguments[0]), depth + 1, seen) ? Source.LOCAL : Source.EXTERNAL;
2224
+ }
2225
+ const root = rootIdentifier(node.callee);
2226
+ if (!root) return Source.EXTERNAL;
2227
+ const variable = findVariable(getScopeFor(context, node), root.name);
2228
+ if (!variable || variable.defs.length === 0) return Source.EXTERNAL;
2229
+ const def = variable.defs[0];
2230
+ if (def.type === "ImportBinding" || def.type === "Parameter") return Source.EXTERNAL;
2231
+ if (def.type === "FunctionName") return Source.LOCAL;
2232
+ const init = def.node.init;
2233
+ if (isFunctionNode(init)) return Source.LOCAL;
2234
+ return classifyVariable(variable, depth, seen);
2235
+ }
2236
+ function classify(node, depth, seen) {
2237
+ if (!node || depth > MAX_DEPTH) return Source.EXTERNAL;
2238
+ switch (node.type) {
2239
+ case "ArrayExpression":
2240
+ return Source.LOCAL;
2241
+ case "TSAsExpression":
2242
+ case "TSSatisfiesExpression":
2243
+ case "TSNonNullExpression":
2244
+ return classify(node.expression, depth + 1, seen);
2245
+ case "ConditionalExpression":
2246
+ return anyLocal([node.consequent, node.alternate], depth + 1, seen) ? Source.LOCAL : Source.EXTERNAL;
2247
+ case "LogicalExpression":
2248
+ return anyLocal([node.left, node.right], depth + 1, seen) ? Source.LOCAL : Source.EXTERNAL;
2249
+ case "CallExpression":
2250
+ return classifyCall(node, depth, seen);
2251
+ case "Identifier": {
2252
+ const variable = findVariable(getScopeFor(context, node), node.name);
2253
+ return variable ? classifyVariable(variable, depth, seen) : Source.EXTERNAL;
2254
+ }
2255
+ default:
2256
+ return Source.EXTERNAL;
2257
+ }
2258
+ }
2259
+ return (node) => classify(node, 0, /* @__PURE__ */ new Set());
2260
+ }
2261
+ function groupNameMatcher(imports) {
2262
+ return (nameNode) => {
2263
+ if (nameNode.type === "JSXIdentifier") return imports.named.has(nameNode.name);
2264
+ const isNamespaceMember = nameNode.type === "JSXMemberExpression" && nameNode.object.type === "JSXIdentifier" && imports.namespaces.has(nameNode.object.name);
2265
+ return isNamespaceMember && nameNode.property.name === GROUP_NAME;
2266
+ };
2267
+ }
2268
+ var singleRowActionSourceRule = {
2269
+ meta: {
2270
+ type: "problem",
2271
+ docs: {
2272
+ description: "RowActionGroup actions must come from a shared defineRowActions set, never a locally built array",
2273
+ recommended: true
2274
+ },
2275
+ schema: [],
2276
+ messages: {
2277
+ localRowActions: "Build row actions once per entity with defineRowActions in a *RowActions module; tables may only omit with a reason."
2278
+ }
2279
+ },
2280
+ create(context) {
2281
+ const imports = { named: /* @__PURE__ */ new Set(), namespaces: /* @__PURE__ */ new Set() };
2282
+ const isGroup = groupNameMatcher(imports);
2283
+ const classify = createClassifier(context);
2284
+ return {
2285
+ ImportDeclaration(node) {
2286
+ if (node.source.value !== UI_BUTTONS_MODULE) return;
2287
+ for (const spec of node.specifiers) {
2288
+ if (spec.type === "ImportNamespaceSpecifier") imports.namespaces.add(spec.local.name);
2289
+ else if (spec.type === "ImportSpecifier" && spec.imported.name === GROUP_NAME) imports.named.add(spec.local.name);
2290
+ }
2291
+ },
2292
+ JSXOpeningElement(node) {
2293
+ if (!isGroup(node.name)) return;
2294
+ const attr = node.attributes.find((a) => a.type === "JSXAttribute" && a.name.name === ACTIONS_PROP);
2295
+ if (!attr || !attr.value || attr.value.type !== "JSXExpressionContainer") return;
2296
+ if (classify(attr.value.expression) === Source.LOCAL) context.report({ node: attr, messageId: "localRowActions" });
2297
+ }
2298
+ };
2299
+ }
2300
+ };
2301
+ var single_row_action_source_default = {
2302
+ rules: {
2303
+ "single-row-action-source": singleRowActionSourceRule
2304
+ }
2305
+ };
2306
+
2142
2307
  // src/rules/smart-max-lines.js
2143
2308
  var COMPONENT_MAX_DEFAULT = 200;
2144
2309
  var FUNCTION_MAX_DEFAULT = 50;
@@ -2410,6 +2575,7 @@ var ruleModules = [
2410
2575
  no_raw_color_literal_default,
2411
2576
  prefer_const_enum_default,
2412
2577
  require_stable_hook_args_default,
2578
+ single_row_action_source_default,
2413
2579
  smart_max_lines_default
2414
2580
  ];
2415
2581
  function mergeRules(modules) {
@@ -2462,7 +2628,18 @@ var commentsConfig = {
2462
2628
  "@dloizides/doc-comment-max-lines": ["error", { max: DOC_COMMENT_MAX_LINES }]
2463
2629
  }
2464
2630
  };
2631
+ var ROW_ACTION_RULE_NAMES = ["single-row-action-source"];
2632
+ var rowActionsConfig = {
2633
+ plugins: commentsConfig.plugins,
2634
+ rules: {
2635
+ "@dloizides/single-row-action-source": "error"
2636
+ }
2637
+ };
2638
+ var recommendedConfig = {
2639
+ plugins: commentsConfig.plugins,
2640
+ rules: { ...commentsConfig.rules, ...rowActionsConfig.rules }
2641
+ };
2465
2642
 
2466
- export { COMMENT_RULE_NAMES, commentsConfig, legacyPlugins, plugin, ruleNames, rules };
2643
+ export { COMMENT_RULE_NAMES, ROW_ACTION_RULE_NAMES, commentsConfig, legacyPlugins, plugin, recommendedConfig, rowActionsConfig, ruleNames, rules };
2467
2644
  //# sourceMappingURL=index.mjs.map
2468
2645
  //# sourceMappingURL=index.mjs.map