Skip to content

Commit fef7785

Browse files
committed
fix: improve arrow function parsing in getParams
- Fixed regex to properly convert arrow functions without braces - Handles both block and expression arrow functions correctly - Fixes 'SyntaxError: Unexpected token' when parsing arrow functions - async () => expr now converts to async function() { return expr } This resolves parser errors that were preventing proper function parameter extraction from arrow functions in BDD tests.
1 parent d676982 commit fef7785

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/parser.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ function getParams(fn) {
1919
try {
2020
// Convert arrow functions to regular functions for parsing
2121
let fnString = fn.toString()
22-
// Handle async arrow functions: async (...) => { becomes async function(...) {
23-
fnString = fnString.replace(/^async\s*(\([^)]*\))\s*=>/, 'async function$1')
24-
// Handle regular arrow functions: (...) => { becomes function(...) {
25-
fnString = fnString.replace(/^(\([^)]*\))\s*=>/, 'function$1')
26-
22+
// Handle async arrow functions: async (...) => expr becomes async function(...) { return expr }
23+
// Handle async arrow functions: async (...) => { ... } becomes async function(...) { ... }
24+
fnString = fnString.replace(/^async\s*(\([^)]*\))\s*=>\s*\{/, 'async function$1 {')
25+
fnString = fnString.replace(/^async\s*(\([^)]*\))\s*=>\s*(.+)$/, 'async function$1 { return $2 }')
26+
// Handle regular arrow functions: (...) => expr becomes function(...) { return expr }
27+
// Handle regular arrow functions: (...) => { ... } becomes function(...) { ... }
28+
fnString = fnString.replace(/^(\([^)]*\))\s*=>\s*\{/, 'function$1 {')
29+
fnString = fnString.replace(/^(\([^)]*\))\s*=>\s*(.+)$/, 'function$1 { return $2 }')
30+
2731
const reflected = parser.parse(fnString)
2832
if (reflected.args.length > 1 || reflected.args[0] === 'I') {
2933
output.error('Error: old CodeceptJS v2 format detected. Upgrade your project to the new format -> https://bit.ly/codecept3Up')

0 commit comments

Comments
 (0)