@dev-blinq/cucumber_client 1.0.1722-dev → 1.0.1724-dev

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.
@@ -604,15 +604,14 @@ const invertStableCommand = (call, elements, stepParams) => {
604
604
 
605
605
  // Set locators if element exists
606
606
  if (step.element && elements[step.element.key]) {
607
- /** @type {LocatorBundle | Locator[] | undefined} */
608
- const locBundle = elements[step.element.key]?.locators;
607
+ const locBundle = elements[step.element.key];
609
608
  step.locators = locBundle;
610
609
  }
611
610
 
612
611
  if (step.type === Types.CLICK) {
613
612
  // handle click with parametric locators
614
613
  // check if locators are parametric , i.e. contains `{variable}`
615
- if (step.locators && !Array.isArray(step.locators) && Array.isArray(step.locators.locators)) {
614
+ if (step.locators && step.locators.locators && Array.isArray(step.locators.locators)) {
616
615
  let hasParametricLocators = false;
617
616
  let variable = "";
618
617
  const regex = /{([^}]+)}/g;
@@ -567,16 +567,12 @@ export class CodePage {
567
567
  if (!element || !element.locators)
568
568
  return element;
569
569
  const clone = JSON.parse(JSON.stringify(element));
570
- const locatorsArray = Array.isArray(clone.locators)
571
- ? clone.locators
572
- : (clone.locators.locators ?? []);
570
+ const locatorsArray = Array.isArray(clone.locators) ? clone.locators : [];
573
571
  for (let i = 0; i < locatorsArray.length; i++) {
574
572
  if (locatorsArray[i].score)
575
573
  delete locatorsArray[i].score;
576
574
  }
577
- clone.locators = Array.isArray(clone.locators)
578
- ? locatorsArray
579
- : { ...clone.locators, locators: locatorsArray };
575
+ clone.locators = Array.isArray(clone.locators) ? locatorsArray : [];
580
576
  return clone;
581
577
  }
582
578
  insertElements(elements) {
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env node
2
+ import { promises as fs } from "node:fs";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { parse } from "@babel/parser";
6
+ import traverseImport from "@babel/traverse";
7
+
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+
11
+ const ROOT = process.cwd();
12
+ const ALLOWED_HARDCODE_RATIO = 0.2;
13
+ const IGNORED_RATIO = 1.0;
14
+ const PLACEHOLDER_REGEX = /{[^}]+}/;
15
+ const IGNORE_DIRS = new Set(["node_modules", ".git", ".idea"]);
16
+
17
+ const results = [];
18
+
19
+ function hasPlaceholder(str) {
20
+ return PLACEHOLDER_REGEX.test(str);
21
+ }
22
+
23
+ function stringFromNode(node) {
24
+ if (!node) return undefined;
25
+ if (node.type === "StringLiteral") return node.value;
26
+ if (node.type === "TemplateLiteral") {
27
+ const raw = node.quasis.map((q) => q.value.cooked ?? q.value.raw).join("${}");
28
+ return raw;
29
+ }
30
+ return undefined;
31
+ }
32
+
33
+ function locatorIsParameterized(objNode) {
34
+ if (objNode.type !== "ObjectExpression") return false;
35
+ return objNode.properties.some((prop) => {
36
+ if (prop.type !== "ObjectProperty") return false;
37
+ const valueString = stringFromNode(prop.value);
38
+ return typeof valueString === "string" && hasPlaceholder(valueString);
39
+ });
40
+ }
41
+
42
+ function analyzeMjsFile(filePath, code) {
43
+ const ast = parse(code, {
44
+ sourceType: "module",
45
+ plugins: ["jsx", "typescript", "classProperties", "optionalChaining", "nullishCoalescingOperator", "topLevelAwait"],
46
+ });
47
+
48
+ const traverse = typeof traverseImport === "function" ? traverseImport : traverseImport.default;
49
+
50
+ traverse(ast, {
51
+ ObjectExpression(pathObj) {
52
+ const props = pathObj.node.properties;
53
+ const locatorProp = props.find(
54
+ (p) =>
55
+ p.type === "ObjectProperty" &&
56
+ ((p.key.type === "Identifier" && p.key.name === "locators") ||
57
+ (p.key.type === "StringLiteral" && p.key.value === "locators"))
58
+ );
59
+ if (!locatorProp || locatorProp.value.type !== "ArrayExpression") return;
60
+
61
+ const locators = locatorProp.value.elements.filter(Boolean);
62
+ if (locators.length === 0) return;
63
+
64
+ const total = locators.length;
65
+ const hardcoded = locators.filter((loc) => !locatorIsParameterized(loc)).length;
66
+ if (hardcoded / total === IGNORED_RATIO) return; // Skip if all locators are hardcoded (likely a non-locator object)
67
+ if (hardcoded / total <= ALLOWED_HARDCODE_RATIO) return;
68
+
69
+ const nameProp = props.find(
70
+ (p) =>
71
+ p.type === "ObjectProperty" &&
72
+ ((p.key.type === "Identifier" && (p.key.name === "element_name" || p.key.name === "element_key")) ||
73
+ (p.key.type === "StringLiteral" && (p.key.value === "element_name" || p.key.value === "element_key")))
74
+ );
75
+ const elementNameValue = nameProp ? stringFromNode(nameProp.value) : undefined;
76
+ const loc = pathObj.node.loc?.start;
77
+ results.push({
78
+ file: filePath,
79
+ line: loc?.line ?? 0,
80
+ element: elementNameValue ?? "<unknown>",
81
+ hardcoded,
82
+ total,
83
+ });
84
+ },
85
+ });
86
+ }
87
+
88
+ function locatorIsParameterizedJson(locator) {
89
+ if (locator && typeof locator === "object") {
90
+ return Object.values(locator).some((val) => typeof val === "string" && hasPlaceholder(val));
91
+ }
92
+ return false;
93
+ }
94
+
95
+ function findLineNumber(content, searchValue) {
96
+ const idx = content.indexOf(searchValue);
97
+ if (idx === -1) return 0;
98
+ return content.slice(0, idx).split("\n").length;
99
+ }
100
+
101
+ function analyzeJsonFile(filePath, content) {
102
+ let data;
103
+ try {
104
+ data = JSON.parse(content);
105
+ } catch (err) {
106
+ console.warn(`Skipping ${filePath}: invalid JSON`);
107
+ return;
108
+ }
109
+
110
+ const visit = (node) => {
111
+ if (!node || typeof node !== "object") return;
112
+ if (Array.isArray(node)) {
113
+ node.forEach(visit);
114
+ return;
115
+ }
116
+
117
+ if (Array.isArray(node.locators) && node.locators.length > 0) {
118
+ const total = node.locators.length;
119
+ const hardcoded = node.locators.filter((loc) => !locatorIsParameterizedJson(loc)).length;
120
+
121
+ if (hardcoded / total > ALLOWED_HARDCODE_RATIO) {
122
+ if (hardcoded / total === IGNORED_RATIO) return; // Skip if all locators are hardcoded (likely a non-locator object)
123
+ const name = node.element_name || node.element_key || "<unknown>";
124
+ const line = findLineNumber(content, name);
125
+ results.push({ file: filePath, line, element: name, hardcoded, total });
126
+ }
127
+ }
128
+
129
+ Object.values(node).forEach(visit);
130
+ };
131
+
132
+ visit(data);
133
+ }
134
+
135
+ async function walk(dir) {
136
+ const entries = await fs.readdir(dir, { withFileTypes: true });
137
+ for (const entry of entries) {
138
+ if (IGNORE_DIRS.has(entry.name)) continue;
139
+ const resolved = path.join(dir, entry.name);
140
+ if (entry.isDirectory()) {
141
+ await walk(resolved);
142
+ } else if (entry.isFile() && (entry.name.endsWith(".mjs") || entry.name.endsWith(".json"))) {
143
+ const content = await fs.readFile(resolved, "utf8");
144
+ try {
145
+ if (entry.name.endsWith(".mjs")) {
146
+ analyzeMjsFile(resolved, content);
147
+ } else {
148
+ analyzeJsonFile(resolved, content);
149
+ }
150
+ } catch (err) {
151
+ console.error(`Failed to analyze ${resolved}:`, err.message);
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+ async function main() {
158
+ await walk(ROOT);
159
+ if (results.length === 0) {
160
+ console.log("No elements exceeded 20% hardcoded locator threshold.");
161
+ return;
162
+ }
163
+
164
+ for (const r of results) {
165
+ console.log(`${r.file}:${r.line} — ${r.element} (${r.hardcoded}/${r.total} hardcoded)`);
166
+ }
167
+ process.exitCode = 1;
168
+ }
169
+
170
+ main().catch((err) => {
171
+ console.error(err);
172
+ process.exit(1);
173
+ });
@@ -0,0 +1,247 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Codemod to drop hardcoded locators flagged by find_harcoded_locators.mjs.
4
+ * - Default: clones workspace to a temp dir, git init + baseline commit, applies fixes, prints diffs.
5
+ * - --inplace: modifies files in the current workspace.
6
+ *
7
+ * Behavior:
8
+ * For each reported file, in every object with a `locators` array,
9
+ * remove locator entries that contain no placeholders ({...}).
10
+ * If all locators are hardcoded, the array is left unchanged to avoid emptying it.
11
+ * JSON files are handled similarly.
12
+ *
13
+ * Usage:
14
+ * node find_harcoded_locators.mjs | node fix-hardcoded-locators.mjs
15
+ * node find_harcoded_locators.mjs | node fix-hardcoded-locators.mjs --inplace
16
+ */
17
+
18
+ import fs from "node:fs";
19
+ import { promises as fsPromises } from "node:fs";
20
+ import path from "node:path";
21
+ import os from "node:os";
22
+ import { parse } from "@babel/parser";
23
+ import traverseImport from "@babel/traverse";
24
+ import { spawnSync } from "node:child_process";
25
+
26
+ const traverse = typeof traverseImport === "function" ? traverseImport : traverseImport.default;
27
+ const PLACEHOLDER_REGEX = /{[^}]+}/;
28
+ const ROOT = process.cwd();
29
+
30
+ const INPUT = await readStdin();
31
+ if (!INPUT.trim()) {
32
+ console.error("No input provided. Pipe find_harcoded_locators.mjs into this script.");
33
+ process.exit(1);
34
+ }
35
+
36
+ const filesToFix = parseInputLines(INPUT);
37
+ if (filesToFix.size === 0) {
38
+ console.log("No actionable lines detected.");
39
+ process.exit(0);
40
+ }
41
+
42
+ const INPLACE = process.argv.includes("--inplace");
43
+ const { cloneRoot, mapper } = INPLACE ? { cloneRoot: ROOT, mapper: (p) => p } : await cloneWorkspace(ROOT);
44
+ if (!INPLACE) {
45
+ console.log(`Cloned workspace to: ${cloneRoot}`);
46
+ await initGitRepo(cloneRoot);
47
+ } else {
48
+ console.log("Running in-place (original files will be modified).");
49
+ }
50
+
51
+ const changes = [];
52
+
53
+ for (const originalPath of filesToFix) {
54
+ const clonedPath = mapper(originalPath);
55
+ const ext = path.extname(clonedPath).toLowerCase();
56
+ const original = await fsPromises.readFile(clonedPath, "utf8");
57
+ let fixed = original;
58
+
59
+ if (ext === ".json") {
60
+ fixed = fixJson(original);
61
+ } else if (ext === ".mjs" || ext === ".js") {
62
+ fixed = fixJs(original);
63
+ }
64
+
65
+ if (fixed === original) continue;
66
+
67
+ const diff = makeDiff(originalPath, clonedPath, fixed);
68
+ await fsPromises.writeFile(clonedPath, fixed, "utf8");
69
+ changes.push({ filePath: originalPath, diff });
70
+ }
71
+
72
+ if (changes.length === 0) {
73
+ console.log("No changes needed.");
74
+ process.exit(0);
75
+ }
76
+
77
+ for (const { filePath, diff } of changes) {
78
+ console.log(`\n=== ${filePath} (cloned) ===`);
79
+ console.log(diff.trimEnd());
80
+ }
81
+
82
+ process.exit(1);
83
+
84
+ // ---------- helpers ----------
85
+
86
+ function parseInputLines(text) {
87
+ const set = new Set();
88
+ for (const line of text.split(/\r?\n/)) {
89
+ const m = line.match(/^(.*?):\d+ — /);
90
+ if (m) set.add(m[1]);
91
+ }
92
+ return set;
93
+ }
94
+
95
+ function hasPlaceholder(str) {
96
+ return PLACEHOLDER_REGEX.test(str);
97
+ }
98
+
99
+ function locatorHasPlaceholderNode(node) {
100
+ if (!node || node.type !== "ObjectExpression") return false;
101
+ return node.properties.some((prop) => {
102
+ if (prop.type !== "ObjectProperty") return false;
103
+ const val = prop.value;
104
+ if (val.type === "StringLiteral") return hasPlaceholder(val.value);
105
+ if (val.type === "TemplateLiteral") {
106
+ const raw = val.quasis.map((q) => q.value.cooked ?? q.value.raw).join("${}");
107
+ return hasPlaceholder(raw);
108
+ }
109
+ return false;
110
+ });
111
+ }
112
+
113
+ function propName(prop) {
114
+ if (prop.type !== "ObjectProperty") return undefined;
115
+ const k = prop.key;
116
+ if (k.type === "Identifier") return k.name;
117
+ if (k.type === "StringLiteral") return k.value;
118
+ return undefined;
119
+ }
120
+
121
+ function leadingWhitespace(text, index) {
122
+ const lineStart = text.lastIndexOf("\n", index - 1) + 1;
123
+ const m = text.slice(lineStart, index).match(/^[ \t]*/);
124
+ return m ? m[0] : "";
125
+ }
126
+
127
+ function fixJs(code) {
128
+ let touched = false;
129
+ const replacements = [];
130
+
131
+ const ast = parse(code, {
132
+ sourceType: "module",
133
+ plugins: ["jsx", "typescript", "classProperties", "optionalChaining", "nullishCoalescingOperator", "topLevelAwait"],
134
+ ranges: true,
135
+ });
136
+
137
+ traverse(ast, {
138
+ ObjectExpression(pathObj) {
139
+ const props = pathObj.node.properties.filter((p) => p.type === "ObjectProperty");
140
+ if (!props.length) return;
141
+ const locProp = props.find((p) => propName(p) === "locators" && p.value.type === "ArrayExpression");
142
+ if (!locProp) return;
143
+ const elements = locProp.value.elements.filter(Boolean);
144
+ if (elements.length === 0) return;
145
+
146
+ const keep = elements.filter(locatorHasPlaceholderNode);
147
+ if (keep.length === elements.length) return;
148
+ if (keep.length === 0) return; // avoid emptying; leave as-is
149
+
150
+ if (typeof locProp.value.start !== "number" || typeof locProp.value.end !== "number") return;
151
+
152
+ const valueStart = locProp.value.start;
153
+ const valueEnd = locProp.value.end;
154
+ const indent = leadingWhitespace(code, valueStart) + " ";
155
+ const keepTexts = keep.map((el) => code.slice(el.start, el.end));
156
+ const newArray =
157
+ "[\n" + keepTexts.map((t) => indent + t.trim()).join(",\n") + "\n" + leadingWhitespace(code, valueStart) + "]";
158
+
159
+ replacements.push({ start: valueStart, end: valueEnd, text: newArray });
160
+ touched = true;
161
+ },
162
+ });
163
+
164
+ if (!touched) return code;
165
+
166
+ let out = code;
167
+ replacements
168
+ .sort((a, b) => b.start - a.start)
169
+ .forEach(({ start, end, text }) => {
170
+ out = out.slice(0, start) + text + out.slice(end);
171
+ });
172
+ return out;
173
+ }
174
+
175
+ function locatorHasPlaceholderJson(locator) {
176
+ if (locator && typeof locator === "object") {
177
+ return Object.values(locator).some((val) => typeof val === "string" && hasPlaceholder(val));
178
+ }
179
+ return false;
180
+ }
181
+
182
+ function fixJson(content) {
183
+ let data;
184
+ try {
185
+ data = JSON.parse(content);
186
+ } catch {
187
+ return content;
188
+ }
189
+ let touched = false;
190
+
191
+ const visit = (node) => {
192
+ if (!node || typeof node !== "object") return;
193
+ if (Array.isArray(node)) return node.forEach(visit);
194
+
195
+ if (Array.isArray(node.locators) && node.locators.length > 0) {
196
+ const keep = node.locators.filter(locatorHasPlaceholderJson);
197
+ if (keep.length > 0 && keep.length < node.locators.length) {
198
+ node.locators = keep;
199
+ touched = true;
200
+ }
201
+ }
202
+ Object.values(node).forEach(visit);
203
+ };
204
+
205
+ visit(data);
206
+ return touched ? JSON.stringify(data, null, 2) + "\n" : content;
207
+ }
208
+
209
+ async function cloneWorkspace(root) {
210
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "codemod-clone-"));
211
+ const cloneRoot = path.join(tmpDir, path.basename(root));
212
+
213
+ if (fsPromises.cp) {
214
+ await fsPromises.cp(root, cloneRoot, { recursive: true, force: true });
215
+ } else {
216
+ const res = spawnSync("cp", ["-R", ".", cloneRoot], { cwd: root, stdio: "inherit" });
217
+ if (res.status !== 0) throw new Error("Failed to clone workspace");
218
+ }
219
+
220
+ const mapper = (orig) => path.join(cloneRoot, path.relative(root, orig));
221
+ return { cloneRoot, mapper };
222
+ }
223
+
224
+ async function initGitRepo(cloneRoot) {
225
+ if (fs.existsSync(path.join(cloneRoot, ".git"))) return;
226
+ const run = (args) => spawnSync("git", args, { cwd: cloneRoot, stdio: "inherit" });
227
+ run(["init"]);
228
+ run(["add", "."]);
229
+ run(["commit", "-m", "chore: baseline before codemod"]);
230
+ }
231
+
232
+ function makeDiff(originalPath, clonedPath, fixed) {
233
+ const tmpOutDir = fs.mkdtempSync(path.join(os.tmpdir(), "codemod-out-"));
234
+ const fixedPath = path.join(tmpOutDir, path.basename(clonedPath));
235
+ fs.writeFileSync(fixedPath, fixed, "utf8");
236
+ const diff = spawnSync("diff", ["-u", originalPath, fixedPath], { encoding: "utf8" });
237
+ return diff.stdout || diff.stderr || "";
238
+ }
239
+
240
+ async function readStdin() {
241
+ return await new Promise((resolve) => {
242
+ let d = "";
243
+ process.stdin.setEncoding("utf8");
244
+ process.stdin.on("data", (c) => (d += c));
245
+ process.stdin.on("end", () => resolve(d));
246
+ });
247
+ }
@@ -0,0 +1,8 @@
1
+ import { locatorObjectToArray } from "./locators_array";
2
+ export const codeMods = [
3
+ {
4
+ name: "Locator Object to Array",
5
+ description: "Converts locator objects with numeric keys to arrays.",
6
+ apply: locatorObjectToArray,
7
+ },
8
+ ];
@@ -0,0 +1,142 @@
1
+ import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
+ import { parse } from "@babel/parser";
4
+ import traverse from "@babel/traverse";
5
+ import * as t from "@babel/types";
6
+ const IGNORE_DIRS = new Set(["node_modules", ".git", ".idea"]);
7
+ function propName(prop) {
8
+ if (!t.isObjectProperty(prop))
9
+ return undefined;
10
+ const key = prop.key;
11
+ if (t.isIdentifier(key))
12
+ return key.name;
13
+ if (t.isStringLiteral(key))
14
+ return key.value;
15
+ if (t.isNumericLiteral(key))
16
+ return String(key.value);
17
+ return undefined;
18
+ }
19
+ function isNumericKey(name) {
20
+ return typeof name === "string" && /^\d+$/.test(name);
21
+ }
22
+ function getElementLabel(props) {
23
+ const labelProp = props.find((p) => ["element_name", "element_key"].includes(propName(p) ?? ""));
24
+ if (!labelProp)
25
+ return undefined;
26
+ const value = labelProp.value;
27
+ if (t.isStringLiteral(value))
28
+ return value.value;
29
+ if (t.isTemplateLiteral(value)) {
30
+ return value.quasis.map((q) => q.value.cooked ?? q.value.raw).join("${}");
31
+ }
32
+ return undefined;
33
+ }
34
+ function analyzeAst(filePath, code, results) {
35
+ const ast = parse(code, {
36
+ sourceType: "module",
37
+ plugins: ["jsx", "typescript", "classProperties", "optionalChaining", "nullishCoalescingOperator", "topLevelAwait"],
38
+ });
39
+ traverse(ast, {
40
+ ObjectExpression(pathObj) {
41
+ const props = pathObj.node.properties.filter(t.isObjectProperty);
42
+ if (props.length === 0)
43
+ return;
44
+ const names = props.map(propName).filter(Boolean);
45
+ const hasElementMeta = names.some((n) => n === "element_name" || n === "element_key");
46
+ if (!hasElementMeta)
47
+ return;
48
+ const hasLocators = names.includes("locators");
49
+ const numericProps = names.filter(isNumericKey);
50
+ if (!hasLocators && numericProps.length > 0) {
51
+ results.push({
52
+ file: filePath,
53
+ line: pathObj.node.loc?.start.line ?? 0,
54
+ element: getElementLabel(props) ?? "<unknown>",
55
+ numericCount: numericProps.length,
56
+ });
57
+ }
58
+ },
59
+ });
60
+ }
61
+ function findLineNumber(content, searchValue) {
62
+ if (!searchValue)
63
+ return 0;
64
+ const idx = content.indexOf(searchValue);
65
+ if (idx === -1)
66
+ return 0;
67
+ return content.slice(0, idx).split("\n").length;
68
+ }
69
+ function analyzeJson(filePath, content, results, logger) {
70
+ let data;
71
+ try {
72
+ data = JSON.parse(content);
73
+ }
74
+ catch {
75
+ logger.warn(`Skipping ${filePath}: invalid JSON`);
76
+ return;
77
+ }
78
+ const visit = (node) => {
79
+ if (!node || typeof node !== "object")
80
+ return;
81
+ if (Array.isArray(node)) {
82
+ node.forEach(visit);
83
+ return;
84
+ }
85
+ const record = node;
86
+ const keys = Object.keys(record);
87
+ const hasElementMeta = "element_name" in record || "element_key" in record;
88
+ const hasLocators = "locators" in record;
89
+ const numericKeys = keys.filter(isNumericKey);
90
+ if (hasElementMeta && !hasLocators && numericKeys.length > 0) {
91
+ results.push({
92
+ file: filePath,
93
+ line: findLineNumber(content, record.element_name ?? record.element_key),
94
+ element: record.element_name ?? record.element_key ?? "<unknown>",
95
+ numericCount: numericKeys.length,
96
+ });
97
+ }
98
+ Object.values(record).forEach(visit);
99
+ };
100
+ visit(data);
101
+ }
102
+ async function walk(dir, results, logger) {
103
+ const entries = await fs.readdir(dir, { withFileTypes: true });
104
+ for (const entry of entries) {
105
+ if (IGNORE_DIRS.has(entry.name))
106
+ continue;
107
+ const resolved = path.join(dir, entry.name);
108
+ if (entry.isDirectory()) {
109
+ await walk(resolved, results, logger);
110
+ continue;
111
+ }
112
+ if (!entry.isFile())
113
+ continue;
114
+ if (entry.name.endsWith(".mjs") || entry.name.endsWith(".js")) {
115
+ const code = await fs.readFile(resolved, "utf8");
116
+ try {
117
+ analyzeAst(resolved, code, results);
118
+ }
119
+ catch (err) {
120
+ logger.error(`Failed to analyze ${resolved}: ${err.message}`);
121
+ }
122
+ }
123
+ else if (entry.name.endsWith(".json")) {
124
+ const content = await fs.readFile(resolved, "utf8");
125
+ try {
126
+ analyzeJson(resolved, content, results, logger);
127
+ }
128
+ catch (err) {
129
+ logger.error(`Failed to analyze ${resolved}: ${err.message}`);
130
+ }
131
+ }
132
+ }
133
+ }
134
+ /**
135
+ * Scans all JS/MJS/JSON files under `root` and returns an array of issues:
136
+ * [{ file, line, element, numericCount }]
137
+ */
138
+ export async function findIssues(root, logger) {
139
+ const results = [];
140
+ await walk(root, results, logger);
141
+ return results;
142
+ }
@@ -0,0 +1,140 @@
1
+ import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
+ import { parse } from "@babel/parser";
4
+ import traverse from "@babel/traverse";
5
+ import * as t from "@babel/types";
6
+ function isNumericKey(key) {
7
+ return typeof key === "string" && /^\d+$/.test(key);
8
+ }
9
+ function propName(prop) {
10
+ if (!t.isObjectProperty(prop))
11
+ return undefined;
12
+ const k = prop.key;
13
+ if (t.isIdentifier(k))
14
+ return k.name;
15
+ if (t.isStringLiteral(k))
16
+ return k.value;
17
+ if (t.isNumericLiteral(k))
18
+ return String(k.value);
19
+ return undefined;
20
+ }
21
+ function stringifyValue(node, source) {
22
+ if (typeof node.start === "number" && typeof node.end === "number") {
23
+ return source.slice(node.start, node.end);
24
+ }
25
+ return JSON.stringify(null);
26
+ }
27
+ function leadingWhitespace(text, index) {
28
+ const lineStart = text.lastIndexOf("\n", index - 1) + 1;
29
+ const match = text.slice(lineStart, index).match(/^[ \t]*/);
30
+ return match ? match[0] : "";
31
+ }
32
+ function buildLocatorsArray(numericProps, source, indent) {
33
+ const elements = numericProps
34
+ .sort((a, b) => Number(propName(a)) - Number(propName(b)))
35
+ .map((p) => `${indent} ${stringifyValue(p.value, source)}`);
36
+ if (elements.length === 0)
37
+ return "locators: []";
38
+ return [indent + "locators: [", elements.join(",\n"), indent + "]"].join("\n");
39
+ }
40
+ function fixJsPreserveFormat(code, logger) {
41
+ let touched = false;
42
+ const replacements = [];
43
+ const ast = parse(code, {
44
+ sourceType: "module",
45
+ plugins: ["jsx", "typescript", "classProperties", "optionalChaining", "nullishCoalescingOperator", "topLevelAwait"],
46
+ ranges: true,
47
+ });
48
+ traverse(ast, {
49
+ ObjectExpression(pathObj) {
50
+ const props = pathObj.node.properties.filter(t.isObjectProperty);
51
+ if (!props.length)
52
+ return;
53
+ const names = props.map(propName).filter(Boolean);
54
+ const hasElementMeta = names.some((n) => n === "element_name" || n === "element_key");
55
+ if (!hasElementMeta)
56
+ return;
57
+ const hasLocators = names.includes("locators");
58
+ const numericProps = props.filter((p) => isNumericKey(propName(p)));
59
+ if (hasLocators || numericProps.length === 0)
60
+ return;
61
+ if (typeof pathObj.node.start !== "number" || typeof pathObj.node.end !== "number")
62
+ return;
63
+ const leading = leadingWhitespace(code, pathObj.node.start);
64
+ const otherProps = props.filter((p) => !numericProps.includes(p));
65
+ const otherTexts = otherProps.map((p) => code.slice(p.start ?? 0, p.end ?? 0));
66
+ const locatorsText = buildLocatorsArray(numericProps, code, `${leading} `);
67
+ const pieces = [locatorsText, ...otherTexts];
68
+ const baseIndent = `${leading} `;
69
+ const normalized = pieces.map((p) => (p.startsWith(baseIndent) ? p : baseIndent + p));
70
+ const newObjectText = "{\n" + normalized.join(",\n") + "\n" + leading + "}";
71
+ replacements.push({ start: pathObj.node.start, end: pathObj.node.end, text: newObjectText });
72
+ touched = true;
73
+ },
74
+ });
75
+ if (!touched)
76
+ return code;
77
+ let out = code;
78
+ replacements
79
+ .sort((a, b) => b.start - a.start)
80
+ .forEach(({ start, end, text }) => {
81
+ out = out.slice(0, start) + text + out.slice(end);
82
+ });
83
+ return out;
84
+ }
85
+ function fixJson(content, logger) {
86
+ let data;
87
+ try {
88
+ data = JSON.parse(content);
89
+ }
90
+ catch {
91
+ return content;
92
+ }
93
+ let touched = false;
94
+ const visit = (node, logger) => {
95
+ if (!node || typeof node !== "object")
96
+ return;
97
+ if (Array.isArray(node))
98
+ return node.forEach((n) => visit(n, logger));
99
+ const record = node;
100
+ const keys = Object.keys(record);
101
+ const hasMeta = "element_name" in record || "element_key" in record;
102
+ const hasLoc = "locators" in record;
103
+ const nums = keys.filter(isNumericKey);
104
+ if (hasMeta && !hasLoc && nums.length) {
105
+ record.locators = nums.sort((a, b) => Number(a) - Number(b)).map((k) => record[k]);
106
+ nums.forEach((k) => delete record[k]);
107
+ touched = true;
108
+ }
109
+ Object.values(record).forEach((v) => visit(v, logger));
110
+ };
111
+ visit(data, logger);
112
+ return touched ? `${JSON.stringify(data, null, 2)}\n` : content;
113
+ }
114
+ /**
115
+ * Fixes all files referenced in `issues` under `root`.
116
+ * `issues` is the array returned by findIssues().
117
+ * Returns an array of { filePath, original, fixed } for files that changed.
118
+ */
119
+ export async function fixIssues(issues, root, logger) {
120
+ // Deduplicate — multiple issues can point to the same file
121
+ const filePaths = [...new Set(issues.map((i) => i.file))];
122
+ const changes = [];
123
+ for (const filePath of filePaths) {
124
+ const absolutePath = path.isAbsolute(filePath) ? filePath : path.join(root, filePath);
125
+ const ext = path.extname(absolutePath).toLowerCase();
126
+ const original = await fs.readFile(absolutePath, "utf8");
127
+ let fixed = original;
128
+ if (ext === ".json") {
129
+ fixed = fixJson(original, logger);
130
+ }
131
+ else if (ext === ".mjs" || ext === ".js") {
132
+ fixed = fixJsPreserveFormat(original, logger);
133
+ }
134
+ if (fixed === original)
135
+ continue;
136
+ await fs.writeFile(absolutePath, fixed, "utf8");
137
+ changes.push({ filePath: absolutePath, original, fixed });
138
+ }
139
+ return changes;
140
+ }
@@ -0,0 +1,114 @@
1
+ import { findIssues } from "./find_misstructured_elements";
2
+ import { fixIssues } from "./fix_misstructured_elements";
3
+ /**
4
+ * Converts legacy locator objects that use numeric keys (e.g., `{ 0: {...}, 1: {...} }`)
5
+ * into a `locators` array, across all JS/MJS/JSON files under a codebase root.
6
+ * The codemod is idempotent, preserves formatting where possible, and reports timing metrics.
7
+ *
8
+ * @param clonedRoot Absolute path to the cloned project directory to scan and fix.
9
+ * @param logger Logger instance used for progress, issue, and summary output.
10
+ * @returns Structured codemod run result containing issues found, file changes written, and metrics.
11
+ *
12
+ * @example Running the codemod standalone:
13
+ * ```ts
14
+ * import { locatorObjectToArray } from "./code_cleanup/codemod/locators_array";
15
+ * import { consoleLogger } from "./logger";
16
+ *
17
+ * const result = await locatorObjectToArray("/tmp/repo", consoleLogger);
18
+ * console.log(result.message); // e.g., "Fixed 3 file(s)"
19
+ * ```
20
+ *
21
+ * @example Running multiple codemods in sequence:
22
+ * ```ts
23
+ * const codemods = [locatorObjectToArray]; // add other codemods as needed
24
+ * for (const mod of codemods) {
25
+ * const outcome = await mod("/tmp/repo", consoleLogger);
26
+ * if (!outcome.success) throw new Error(outcome.message);
27
+ * }
28
+ * ```
29
+ */
30
+ async function locatorObjectToArray(clonedRoot, logger) {
31
+ const start = Date.now();
32
+ logger.info(`Scanning ${clonedRoot} for misstructured locator objects...`);
33
+ const findStart = Date.now();
34
+ const issues = [];
35
+ try {
36
+ issues.push(...(await findIssues(clonedRoot, logger)));
37
+ }
38
+ catch (error) {
39
+ const metricsOnFindError = {
40
+ issueCount: 0,
41
+ affectedFiles: 0,
42
+ findDurationMs: Date.now() - findStart,
43
+ fixDurationMs: 0,
44
+ filesFixed: 0,
45
+ wasAffected: false,
46
+ totalDurationMs: Date.now() - start,
47
+ };
48
+ logger.error(`Error during issue finding: ${error.message}`);
49
+ return {
50
+ name: "Locator Object to Array",
51
+ description: "Converts locator objects with numeric keys to arrays.",
52
+ success: false,
53
+ message: `Failed to find issues: ${error.message}`,
54
+ details: { issues: [], changes: [], metrics: metricsOnFindError },
55
+ };
56
+ }
57
+ const metricsBase = {
58
+ issueCount: issues.length,
59
+ affectedFiles: [...new Set(issues.map((i) => i.file))].length,
60
+ findDurationMs: Date.now() - findStart,
61
+ fixDurationMs: 0,
62
+ filesFixed: 0,
63
+ wasAffected: issues.length > 0,
64
+ totalDurationMs: 0,
65
+ };
66
+ if (issues.length === 0) {
67
+ metricsBase.totalDurationMs = Date.now() - start;
68
+ logger.info("No issues — nothing to fix");
69
+ logger.info(`Summary: issues=0 files=0 fixed=0 find=${metricsBase.findDurationMs}ms ` +
70
+ `fix=${metricsBase.fixDurationMs}ms total=${metricsBase.totalDurationMs}ms`);
71
+ return {
72
+ name: "Locator Object to Array",
73
+ description: "Converts locator objects with numeric keys to arrays.",
74
+ success: true,
75
+ message: "No issues found",
76
+ details: { issues: [], changes: [], metrics: metricsBase },
77
+ };
78
+ }
79
+ logger.info(`Found ${issues.length} issue(s) across ${metricsBase.affectedFiles} file(s)`);
80
+ issues.forEach((i) => logger.info(` issue: ${i.file}:${i.line} — ${i.element} (${i.numericCount} numeric keys)`));
81
+ const fixStart = Date.now();
82
+ const changes = [];
83
+ try {
84
+ changes.push(...(await fixIssues(issues, clonedRoot, logger)));
85
+ }
86
+ catch (error) {
87
+ metricsBase.fixDurationMs = Date.now() - fixStart;
88
+ metricsBase.totalDurationMs = Date.now() - start;
89
+ logger.error(`Error during issue fixing: ${error.message}`);
90
+ return {
91
+ name: "Locator Object to Array",
92
+ description: "Converts locator objects with numeric keys to arrays.",
93
+ success: false,
94
+ message: `Failed to fix issues: ${error.message}`,
95
+ details: { issues, changes: [], metrics: metricsBase },
96
+ };
97
+ }
98
+ metricsBase.fixDurationMs = Date.now() - fixStart;
99
+ metricsBase.filesFixed = changes.length;
100
+ metricsBase.totalDurationMs = Date.now() - start;
101
+ logger.info(`Fixed ${changes.length} file(s) in ${metricsBase.fixDurationMs}ms`);
102
+ changes.forEach((c) => logger.info(` fixed: ${c.filePath}`));
103
+ logger.info(`Summary: issues=${metricsBase.issueCount} files=${metricsBase.affectedFiles} ` +
104
+ `fixed=${metricsBase.filesFixed} find=${metricsBase.findDurationMs}ms ` +
105
+ `fix=${metricsBase.fixDurationMs}ms total=${metricsBase.totalDurationMs}ms`);
106
+ return {
107
+ name: "Locator Object to Array",
108
+ description: "Converts locator objects with numeric keys to arrays.",
109
+ success: true,
110
+ message: `Fixed ${changes.length} file(s)`,
111
+ details: { issues, changes, metrics: metricsBase },
112
+ };
113
+ }
114
+ export { locatorObjectToArray };
@@ -0,0 +1 @@
1
+ export {};
@@ -16,7 +16,6 @@ import { unEscapeNonPrintables } from "../cucumber/utils.js";
16
16
  import { findAvailablePort, getRunsServiceBaseURL } from "../utils/index.js";
17
17
  import socketLogger, { getErrorMessage } from "../utils/socket_logger.js";
18
18
  import { tmpdir } from "os";
19
- import { faker } from "@faker-js/faker/locale/en_US";
20
19
  import { chromium } from "playwright-core";
21
20
  import { axiosClient } from "../utils/axiosClient.js";
22
21
  import { _generateCodeFromCommand } from "../code_gen/playwright_codeget.js";
@@ -1229,9 +1228,9 @@ export class BVTRecorder {
1229
1228
  datasets,
1230
1229
  };
1231
1230
  }
1232
- async generateLocatorSummaries({ allStrategyLocators, element_name }) {
1231
+ async generateLocatorSummaries({ allStrategyLocators, element_name, }) {
1233
1232
  const input = {
1234
- [element_name ?? "element"]: allStrategyLocators
1233
+ [element_name ?? "element"]: allStrategyLocators,
1235
1234
  };
1236
1235
  const result = await this.namesService.generateLocatorDescriptions({ locatorsObj: input });
1237
1236
  return result[element_name ?? "element"];
@@ -1583,29 +1582,30 @@ export class BVTRecorder {
1583
1582
  this.bvtContext.STORE_DETAILED_NETWORK_DATA = false;
1584
1583
  }
1585
1584
  }
1586
- async fakeParams(params) {
1585
+ async fakeParams(params, faker) {
1586
+ if (!faker) {
1587
+ faker = await import("@faker-js/faker/locale/en_US").then((mod) => mod.faker);
1588
+ }
1587
1589
  const newFakeParams = {};
1588
- Object.entries(params).forEach(([key, rawValue]) => {
1589
- if (!rawValue.startsWith("{{") || !rawValue.endsWith("}}")) {
1590
- newFakeParams[key] = rawValue;
1590
+ Object.keys(params).forEach((key) => {
1591
+ if (!params[key].startsWith("{{") || !params[key].endsWith("}}")) {
1592
+ newFakeParams[key] = params[key];
1591
1593
  return;
1592
1594
  }
1593
1595
  try {
1594
1596
  const value = params[key].substring(2, params[key].length - 2).trim();
1595
1597
  const faking = value.split("(")[0].split(".");
1596
1598
  let argument = value.substring(value.indexOf("(") + 1, value.lastIndexOf(")"));
1597
- argument = isNaN(Number(argument)) || argument === "" ? argument : Number(argument);
1599
+ argument = isNaN(Number(argument)) ? argument : Number(argument);
1598
1600
  let fakeFunc = faker;
1599
1601
  faking.forEach((f) => {
1600
- //@ts-expect-error Trying to support both old and new faker versions
1601
1602
  fakeFunc = fakeFunc[f];
1602
1603
  });
1603
- //@ts-expect-error Trying to support both old and new faker versions
1604
1604
  const newValue = fakeFunc(argument);
1605
1605
  newFakeParams[key] = newValue;
1606
1606
  }
1607
1607
  catch (error) {
1608
- newFakeParams[key] = rawValue;
1608
+ newFakeParams[key] = params[key];
1609
1609
  }
1610
1610
  });
1611
1611
  return newFakeParams;
package/bin/index.js CHANGED
@@ -13,6 +13,7 @@ export * from "./client/cucumber/feature_data.js";
13
13
  export * from "./client/cucumber/steps_definitions.js";
14
14
  export * from "./client/profiler.js";
15
15
  export * from "./client/code_cleanup/utils.js";
16
+ export * from "./client/codemod/index.js";
16
17
  export * from "./client/code_cleanup/find_step_definition_references.js";
17
18
  export * from "./client/recorderv3/bvt_init.js";
18
19
  export * from "./client/recorderv3/step_utils.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dev-blinq/cucumber_client",
3
- "version": "1.0.1722-dev",
3
+ "version": "1.0.1724-dev",
4
4
  "description": " ",
5
5
  "main": "bin/index.js",
6
6
  "types": "bin/index.d.ts",
@@ -62,6 +62,7 @@
62
62
  "winston-daily-rotate-file": "^4.7.1"
63
63
  },
64
64
  "devDependencies": {
65
+ "@types/babel__traverse": "^7.28.0",
65
66
  "@types/prettier": "^2.7.3",
66
67
  "chai": "^5.1.2",
67
68
  "cpx": "^1.5.0",