@dloizides/frontend-devtools 2.1.0 → 2.2.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.
@@ -954,6 +954,132 @@ var enum_file_isolation_default = {
954
954
  }
955
955
  };
956
956
 
957
+ // src/rules/explicit-initial-focus.js
958
+ var DEFAULT_MIN_CONTROLS = 2;
959
+ var DEFAULT_DIALOG_PATTERN = "(Dialog|Modal)$";
960
+ var MENU_DEFAULT_IDENTIFIER = "MENU_DEFAULT_ATTR";
961
+ var MENU_DEFAULT_TEXT = "data-menu-default";
962
+ var MENU_DEFAULT_DATASET_KEY = "menuDefault";
963
+ var FOCUS_METHOD = "focus";
964
+ var FOCUS_PROP_NAMES = /* @__PURE__ */ new Set(["autoFocus", "initialFocus", "initialFocusRef", MENU_DEFAULT_TEXT]);
965
+ var FOCUS_OPTION_KEYS = /* @__PURE__ */ new Set(["initialFocus", "initialFocusRef", "autoFocus"]);
966
+ var DIALOG_ROLE_PROPS = /* @__PURE__ */ new Set(["role", "accessibilityRole"]);
967
+ var BUTTON_TAG = /<button\b/gi;
968
+ function propertyName(member) {
969
+ if (!member.computed && member.property.type === "Identifier") return member.property.name;
970
+ const prop = member.property;
971
+ return prop.type === "Literal" && typeof prop.value === "string" ? prop.value : void 0;
972
+ }
973
+ function keyName(key) {
974
+ if (key.type === "Identifier") return key.name;
975
+ return key.type === "Literal" && typeof key.value === "string" ? key.value : void 0;
976
+ }
977
+ function jsxName(name) {
978
+ if (name.type === "JSXIdentifier") return name.name;
979
+ if (name.type === "JSXMemberExpression") return name.property.name;
980
+ if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
981
+ return void 0;
982
+ }
983
+ function attributeText(attr) {
984
+ const value = attr.value;
985
+ if (!value) return void 0;
986
+ if (value.type === "Literal" && typeof value.value === "string") return value.value;
987
+ const inner = value.type === "JSXExpressionContainer" ? value.expression : void 0;
988
+ return inner && inner.type === "Literal" && typeof inner.value === "string" ? inner.value : void 0;
989
+ }
990
+ function isDialogRole(attr) {
991
+ if (attr.type !== "JSXAttribute" || !DIALOG_ROLE_PROPS.has(jsxName(attr.name) ?? "")) return false;
992
+ return attributeText(attr) === "dialog";
993
+ }
994
+ function readOptions(context) {
995
+ const options = context.options[0] ?? {};
996
+ return {
997
+ minControls: options.minControls ?? DEFAULT_MIN_CONTROLS,
998
+ dialogPattern: new RegExp(options.dialogPattern ?? DEFAULT_DIALOG_PATTERN),
999
+ selfFocusing: new Set(options.selfFocusing ?? [])
1000
+ };
1001
+ }
1002
+ var rule = {
1003
+ meta: {
1004
+ type: "problem",
1005
+ docs: {
1006
+ description: "FOCUS-1: a game menu or a dialog names the control focused on open (data-menu-default, autoFocus, initialFocus or a ref focus), never the first element by default."
1007
+ },
1008
+ schema: [
1009
+ {
1010
+ type: "object",
1011
+ properties: {
1012
+ minControls: { type: "integer", minimum: 1 },
1013
+ dialogPattern: { type: "string" },
1014
+ selfFocusing: { type: "array", items: { type: "string" } }
1015
+ },
1016
+ additionalProperties: false
1017
+ }
1018
+ ],
1019
+ messages: {
1020
+ missingMenuDefault: "This file builds a menu of {{count}} buttons but names no initial focus. Mark the primary next step with MENU_DEFAULT_ATTR (data-menu-default) or pass initialFocus; never a destructive action.",
1021
+ missingDialogFocus: "'{{name}}' opens a dialog but the file names no initial focus. Give the primary next step autoFocus, an initialFocus prop or a ref focus on mount; never a destructive action."
1022
+ }
1023
+ },
1024
+ create(context) {
1025
+ const options = readOptions(context);
1026
+ let named = false;
1027
+ let controlCount = 0;
1028
+ let firstControl;
1029
+ const dialogs = [];
1030
+ const countControls = (node, text) => {
1031
+ const found = (text.match(BUTTON_TAG) ?? []).length;
1032
+ if (found === 0) return;
1033
+ controlCount += found;
1034
+ firstControl ?? (firstControl = node);
1035
+ };
1036
+ const scanText = (node, text) => {
1037
+ if (text.includes(MENU_DEFAULT_TEXT)) named = true;
1038
+ countControls(node, text);
1039
+ };
1040
+ return {
1041
+ Identifier(node) {
1042
+ if (node.name === MENU_DEFAULT_IDENTIFIER) named = true;
1043
+ },
1044
+ Literal(node) {
1045
+ if (typeof node.value === "string" && node.parent.type !== "JSXAttribute") scanText(node, node.value);
1046
+ },
1047
+ TemplateElement(node) {
1048
+ scanText(node, node.value.cooked ?? node.value.raw);
1049
+ },
1050
+ MemberExpression(node) {
1051
+ if (propertyName(node) === MENU_DEFAULT_DATASET_KEY) named = true;
1052
+ },
1053
+ Property(node) {
1054
+ if (FOCUS_OPTION_KEYS.has(keyName(node.key) ?? "")) named = true;
1055
+ },
1056
+ CallExpression(node) {
1057
+ if (node.callee.type !== "MemberExpression") return;
1058
+ const name = propertyName(node.callee);
1059
+ if (name === FOCUS_METHOD) named = true;
1060
+ const tag = node.arguments[0];
1061
+ const isButton = name === "createElement" && tag && tag.type === "Literal" && String(tag.value).toLowerCase() === "button";
1062
+ if (isButton) countControls(node, "<button");
1063
+ },
1064
+ JSXAttribute(node) {
1065
+ if (FOCUS_PROP_NAMES.has(jsxName(node.name) ?? "")) named = true;
1066
+ },
1067
+ JSXOpeningElement(node) {
1068
+ const name = jsxName(node.name) ?? "";
1069
+ if (options.selfFocusing.has(name)) return;
1070
+ const isDialog = options.dialogPattern.test(name) || node.attributes.some(isDialogRole);
1071
+ if (isDialog) dialogs.push({ node, name });
1072
+ },
1073
+ "Program:exit"() {
1074
+ if (named) return;
1075
+ if (controlCount >= options.minControls) context.report({ node: firstControl, messageId: "missingMenuDefault", data: { count: String(controlCount) } });
1076
+ dialogs.forEach(({ node, name }) => context.report({ node, messageId: "missingDialogFocus", data: { name } }));
1077
+ }
1078
+ };
1079
+ }
1080
+ };
1081
+ var explicit_initial_focus_default = { rules: { "explicit-initial-focus": rule } };
1082
+
957
1083
  // src/rules/game-menu-focusable.js
958
1084
  var POINTER_EVENTS = /* @__PURE__ */ new Set(["click", "pointerdown", "pointerup"]);
959
1085
  var NON_FOCUSABLE_TAGS = /* @__PURE__ */ new Set(["div", "span", "li", "img"]);
@@ -988,14 +1114,14 @@ function stringValue(node) {
988
1114
  if (target.type === "TemplateLiteral" && target.expressions.length === 0) return target.quasis[0].value.cooked;
989
1115
  return void 0;
990
1116
  }
991
- function propertyName(member) {
1117
+ function propertyName2(member) {
992
1118
  if (member.computed) return stringValue(member.property);
993
1119
  return member.property.type === "Identifier" ? member.property.name : void 0;
994
1120
  }
995
1121
  function createdTag(init) {
996
1122
  const call = unwrap(init);
997
1123
  if (!call || call.type !== "CallExpression") return void 0;
998
- if (call.callee.type !== "MemberExpression" || propertyName(call.callee) !== "createElement") return void 0;
1124
+ if (call.callee.type !== "MemberExpression" || propertyName2(call.callee) !== "createElement") return void 0;
999
1125
  const tag = stringValue(call.arguments[0]);
1000
1126
  return tag === void 0 ? void 0 : tag.toLowerCase();
1001
1127
  }
@@ -1007,7 +1133,7 @@ function nonFocusableTag(variable) {
1007
1133
  function setsFocusAttribute(reference) {
1008
1134
  const member = reference.identifier.parent;
1009
1135
  if (!member || member.type !== "MemberExpression" || member.object !== reference.identifier) return false;
1010
- const name = propertyName(member);
1136
+ const name = propertyName2(member);
1011
1137
  const outer = member.parent;
1012
1138
  if (FOCUS_PROPERTIES.has(name ?? "")) return outer.type === "AssignmentExpression" && outer.left === member;
1013
1139
  if (name !== "setAttribute" || outer.type !== "CallExpression" || outer.callee !== member) return false;
@@ -1036,7 +1162,7 @@ function stopPropagationCalls(fn) {
1036
1162
  const visit = (node) => {
1037
1163
  if (!node || typeof node.type !== "string") return;
1038
1164
  if (node !== fn && node.type.includes("Function")) return;
1039
- const isStop = node.type === "CallExpression" && node.callee.type === "MemberExpression" && propertyName(node.callee) === "stopPropagation";
1165
+ const isStop = node.type === "CallExpression" && node.callee.type === "MemberExpression" && propertyName2(node.callee) === "stopPropagation";
1040
1166
  if (isStop) found.push(node);
1041
1167
  for (const key of Object.keys(node)) {
1042
1168
  if (key === "parent") continue;
@@ -1048,7 +1174,7 @@ function stopPropagationCalls(fn) {
1048
1174
  visit(fn.body);
1049
1175
  return found;
1050
1176
  }
1051
- var rule = {
1177
+ var rule2 = {
1052
1178
  meta: {
1053
1179
  type: "problem",
1054
1180
  docs: {
@@ -1073,28 +1199,28 @@ var rule = {
1073
1199
  };
1074
1200
  return {
1075
1201
  CallExpression(node) {
1076
- if (node.callee.type !== "MemberExpression" || propertyName(node.callee) !== "addEventListener") return;
1202
+ if (node.callee.type !== "MemberExpression" || propertyName2(node.callee) !== "addEventListener") return;
1077
1203
  const event = stringValue(node.arguments[0]);
1078
1204
  if (event === KEYDOWN) reportKeydown(node, node.arguments[1]);
1079
1205
  else if (POINTER_EVENTS.has(event ?? "")) reportPointerOnly(node, node.callee.object, event);
1080
1206
  },
1081
1207
  AssignmentExpression(node) {
1082
1208
  if (node.left.type !== "MemberExpression") return;
1083
- const name = propertyName(node.left);
1209
+ const name = propertyName2(node.left);
1084
1210
  if (name === "onclick") reportPointerOnly(node, node.left.object, "onclick");
1085
1211
  else if (name === "onkeydown") reportKeydown(node, node.right);
1086
1212
  }
1087
1213
  };
1088
1214
  }
1089
1215
  };
1090
- var game_menu_focusable_default = { rules: { "game-menu-focusable": rule } };
1216
+ var game_menu_focusable_default = { rules: { "game-menu-focusable": rule2 } };
1091
1217
 
1092
1218
  // src/rules/i18n-interpolation.js
1093
1219
  var DOUBLE_BRACE_REGEX = /\{\{(\w+)\}\}/g;
1094
1220
  var SINGLE_BRACE_REGEX = /(?<!\{)\{(\w+)\}(?!\})/g;
1095
1221
  var ALLOWED_NAMES = /* @__PURE__ */ new Set(["p1", "p2", "p3"]);
1096
1222
  var MAX_POSITIONAL_PARAMS = 3;
1097
- var rule2 = {
1223
+ var rule3 = {
1098
1224
  meta: {
1099
1225
  type: "problem",
1100
1226
  docs: {
@@ -1213,7 +1339,7 @@ var rule2 = {
1213
1339
  };
1214
1340
  var i18n_interpolation_default = {
1215
1341
  rules: {
1216
- "i18n-interpolation": rule2
1342
+ "i18n-interpolation": rule3
1217
1343
  }
1218
1344
  };
1219
1345
 
@@ -1235,7 +1361,7 @@ var I18NEXT_CONFIG_KEYS = /* @__PURE__ */ new Set([
1235
1361
  "formatParams",
1236
1362
  "missingInterpolationHandler"
1237
1363
  ]);
1238
- var rule3 = {
1364
+ var rule4 = {
1239
1365
  meta: {
1240
1366
  type: "problem",
1241
1367
  docs: {
@@ -1260,14 +1386,14 @@ var rule3 = {
1260
1386
  const properties = optionsArg.properties;
1261
1387
  for (const prop of properties) {
1262
1388
  if (prop.type === "SpreadElement") continue;
1263
- const keyName = prop.key.type === "Identifier" ? prop.key.name : prop.key.type === "Literal" ? String(prop.key.value) : null;
1264
- if (keyName === null) continue;
1265
- if (I18NEXT_CONFIG_KEYS.has(keyName)) continue;
1266
- if (!ALLOWED_PARAM_NAMES.has(keyName))
1389
+ const keyName2 = prop.key.type === "Identifier" ? prop.key.name : prop.key.type === "Literal" ? String(prop.key.value) : null;
1390
+ if (keyName2 === null) continue;
1391
+ if (I18NEXT_CONFIG_KEYS.has(keyName2)) continue;
1392
+ if (!ALLOWED_PARAM_NAMES.has(keyName2))
1267
1393
  context.report({
1268
1394
  node: prop,
1269
1395
  messageId: "nonStandardParam",
1270
- data: { paramName: keyName }
1396
+ data: { paramName: keyName2 }
1271
1397
  });
1272
1398
  }
1273
1399
  }
@@ -1276,7 +1402,7 @@ var rule3 = {
1276
1402
  };
1277
1403
  var i18n_param_names_default = {
1278
1404
  rules: {
1279
- "i18n-param-names": rule3
1405
+ "i18n-param-names": rule4
1280
1406
  }
1281
1407
  };
1282
1408
 
@@ -2175,6 +2301,14 @@ function isExemptFile2(path2, allow) {
2175
2301
  if (TEST_FILE2.test(path2) || SHARED_SOURCE.test(path2)) return true;
2176
2302
  return allow.some((fragment) => path2.includes(fragment));
2177
2303
  }
2304
+ var MODULE_DECLARATIONS = /* @__PURE__ */ new Set(["ImportDeclaration", "ExportNamedDeclaration", "ExportAllDeclaration"]);
2305
+ function usesPackage(program, packageName) {
2306
+ return program.body.some((statement) => {
2307
+ if (!MODULE_DECLARATIONS.has(statement.type) || statement.source == null) return false;
2308
+ const source = statement.source.value;
2309
+ return source === packageName || typeof source === "string" && source.startsWith(`${packageName}/`);
2310
+ });
2311
+ }
2178
2312
  function literalText(node) {
2179
2313
  if (node.type === "Literal") return typeof node.value === "string" ? node.value : "";
2180
2314
  return node.quasis.length === 1 ? node.quasis[0].value.cooked ?? "" : "";
@@ -2216,7 +2350,7 @@ var noReimplementedPackageRule = {
2216
2350
  return {
2217
2351
  Program(node) {
2218
2352
  for (const entry of REIMPLEMENTED_PACKAGE_REGISTRY)
2219
- if (entry.paths.some((pattern) => pattern.test(path2))) report(node, entry);
2353
+ if (entry.paths.some((pattern) => pattern.test(path2)) && !usesPackage(node, entry.package)) report(node, entry);
2220
2354
  },
2221
2355
  Literal: checkLiteral,
2222
2356
  TemplateLiteral: checkLiteral
@@ -2324,7 +2458,7 @@ function isQueryKeyProperty(node) {
2324
2458
  if (node.key.type === "Identifier") return node.key.name === QUERY_KEY;
2325
2459
  return node.key.type === "Literal" && node.key.value === QUERY_KEY;
2326
2460
  }
2327
- var rule4 = {
2461
+ var rule5 = {
2328
2462
  meta: {
2329
2463
  type: "suggestion",
2330
2464
  docs: {
@@ -2355,7 +2489,7 @@ var rule4 = {
2355
2489
  };
2356
2490
  }
2357
2491
  };
2358
- var query_key_factory_default = { rules: { "query-key-factory": rule4 } };
2492
+ var query_key_factory_default = { rules: { "query-key-factory": rule5 } };
2359
2493
 
2360
2494
  // src/rules/require-stable-hook-args.js
2361
2495
  var STABLE_INIT_CALLS = /* @__PURE__ */ new Set([
@@ -3026,6 +3160,7 @@ var ruleModules = [
3026
3160
  enforce_route_preload_default,
3027
3161
  enforce_test_colocation_default,
3028
3162
  enum_file_isolation_default,
3163
+ explicit_initial_focus_default,
3029
3164
  game_menu_focusable_default,
3030
3165
  i18n_interpolation_default,
3031
3166
  i18n_param_names_default,
@@ -3049,11 +3184,11 @@ var ruleModules = [
3049
3184
  function mergeRules(modules) {
3050
3185
  const merged = {};
3051
3186
  for (const mod of modules) {
3052
- for (const [name, rule5] of Object.entries(mod.rules)) {
3187
+ for (const [name, rule6] of Object.entries(mod.rules)) {
3053
3188
  if (merged[name] !== void 0) {
3054
3189
  throw new Error(`Duplicate ESLint rule name: "${name}"`);
3055
3190
  }
3056
- merged[name] = rule5;
3191
+ merged[name] = rule6;
3057
3192
  }
3058
3193
  }
3059
3194
  return merged;
@@ -3069,15 +3204,15 @@ var ruleNames = Object.keys(plugin.rules).sort();
3069
3204
  var legacyPlugins = buildLegacyPlugins();
3070
3205
  function buildLegacyPlugins() {
3071
3206
  const out = {};
3072
- for (const [name, rule5] of Object.entries(rules)) {
3073
- out[name] = oneRulePlugin(name, rule5);
3207
+ for (const [name, rule6] of Object.entries(rules)) {
3208
+ out[name] = oneRulePlugin(name, rule6);
3074
3209
  }
3075
3210
  return out;
3076
3211
  }
3077
- function oneRulePlugin(name, rule5) {
3212
+ function oneRulePlugin(name, rule6) {
3078
3213
  return {
3079
3214
  meta: { name, version: "1.0.0" },
3080
- rules: { [name]: rule5 }
3215
+ rules: { [name]: rule6 }
3081
3216
  };
3082
3217
  }
3083
3218
  var OFF_LEVELS = ["off", 0];
@@ -3169,7 +3304,14 @@ var recommendedConfig = {
3169
3304
  plugins: commentsConfig.plugins,
3170
3305
  rules: { ...commentsConfig.rules, ...rowActionsConfig.rules, ...apiConfig.rules }
3171
3306
  };
3307
+ var FOCUS_RULE_NAMES = ["explicit-initial-focus"];
3308
+ var focusConfig = {
3309
+ plugins: commentsConfig.plugins,
3310
+ rules: {
3311
+ "@dloizides/explicit-initial-focus": "error"
3312
+ }
3313
+ };
3172
3314
 
3173
- export { API_RULE_NAMES, COMMENT_RULE_NAMES, GAME_RULE_NAMES, HOUSE_PATTERN_RULE_NAMES, REUSE_RULE_NAMES, ROW_ACTION_RULE_NAMES, apiConfig, commentsConfig, gameConfig, legacyPlugins, patternPacksConfig, plugin, recommendedConfig, reuseConfig, rowActionsConfig, ruleNames, rules };
3315
+ export { API_RULE_NAMES, COMMENT_RULE_NAMES, FOCUS_RULE_NAMES, GAME_RULE_NAMES, HOUSE_PATTERN_RULE_NAMES, REUSE_RULE_NAMES, ROW_ACTION_RULE_NAMES, apiConfig, commentsConfig, focusConfig, gameConfig, legacyPlugins, patternPacksConfig, plugin, recommendedConfig, reuseConfig, rowActionsConfig, ruleNames, rules };
3174
3316
  //# sourceMappingURL=index.mjs.map
3175
3317
  //# sourceMappingURL=index.mjs.map