Skip to content

Commit

Permalink
Merge pull request #24 from Altruistiq/fix/better-variable-identifica…
Browse files Browse the repository at this point in the history
…tion

fix: better variable identification on controllers. Fixed coverage issue
  • Loading branch information
gremp authored May 21, 2024
2 parents e4eb4b2 + a168276 commit 74de29c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reef-framework",
"version": "0.0.36",
"version": "0.0.37",
"description": "a typescript web framework",
"main": "pkg/index.js",
"types": "pkg/index.d.ts",
Expand Down
37 changes: 36 additions & 1 deletion src/decorators/base-param-decorators.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function getVariableName(
}

funcDefParams = funcDefParams.substring(1, funcDefParams.length - 1);
const paramsArr = funcDefParams.split(',');
const paramsArr = splitMethodParams(funcDefParams);

const paramToRetun = paramsArr[index];
if (!paramToRetun)
Expand All @@ -184,3 +184,38 @@ export function getVariableName(
}
return paramToRetun.trim();
}

function splitMethodParams(methodParamStr: string) {
let charIndex = 0;
let openParenthesisCount = 0;
let openBracketCount = 0;
let openCurlyBracketCount = 0;
let paramStart = 0
const variableNames = []
while (charIndex < methodParamStr.length) {
const c = methodParamStr.charAt(charIndex++);
if (c === '(') {
openParenthesisCount++;
} else if (c === ')') {
openParenthesisCount--;
}
if (c === '[') {
openBracketCount++;
} else if (c === ']') {
openBracketCount--;
}
if (c === '{') {
openCurlyBracketCount++;
} else if (c === '}') {
openCurlyBracketCount--;
}
if (openParenthesisCount === 0 &&
openBracketCount === 0 &&
openCurlyBracketCount === 0 &&
paramStart !== charIndex && (c === ',' || charIndex === methodParamStr.length)) {
variableNames.push(methodParamStr.substring(paramStart, c === ',' ? charIndex - 1 : charIndex))
paramStart = charIndex
}
}
return variableNames
}

0 comments on commit 74de29c

Please sign in to comment.