|
| 1 | + |
| 2 | +export class paramDef { |
| 3 | + constructor(public paramName: string, public paramType: string) { |
| 4 | + this.paramName = paramName; |
| 5 | + this.paramType = paramType; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +export function getParameterText(functionParams: paramDef[], returnParams: paramDef[]): string { |
| 10 | + var textToInsert: string = ""; |
| 11 | + textToInsert = textToInsert + '/**\n * @notice .\n * @dev .\n'; |
| 12 | + functionParams.forEach(element => { |
| 13 | + if (element.paramName != '') { |
| 14 | + textToInsert = textToInsert + ' * @param '; |
| 15 | + // textToInsert = textToInsert + '{' + element.paramType + '}' + ' '; // for type insertion |
| 16 | + textToInsert = textToInsert + element.paramName + ' .' + '\n'; |
| 17 | + } |
| 18 | + }); |
| 19 | + returnParams.forEach(element => { |
| 20 | + if (element.paramType != '') { |
| 21 | + textToInsert = textToInsert + ' * @return '; |
| 22 | + // textToInsert = textToInsert + '{' + element.paramType + '}' + ' '; // for type insertion |
| 23 | + if (element.paramName != '') { |
| 24 | + textToInsert = textToInsert + element.paramName + ' .' + '\n'; |
| 25 | + } else { |
| 26 | + // textToInsert = textToInsert + 'UNNAMED' + '\n'; // for type insertion |
| 27 | + textToInsert = textToInsert + element.paramType + ' .' + '\n'; |
| 28 | + } |
| 29 | + } |
| 30 | + }); |
| 31 | + textToInsert = textToInsert + ' */'; |
| 32 | + return textToInsert; |
| 33 | +} |
| 34 | + |
| 35 | +// split a given line by a keyword |
| 36 | +// for Solidity keyword is expected to be used with 'returns' |
| 37 | +// everything before 'returns' will be used to build lines[0] for function parameters |
| 38 | +// everything after 'returns' will be used to build lines[1] for the return parameters |
| 39 | +export function splitTextByKeyword(text: string, keyword: string): string[] { |
| 40 | + const result = text.trim().split(/\s+/); |
| 41 | + const index = result.indexOf("{", 0); |
| 42 | + if (index > -1) { |
| 43 | + result.splice(index, 1); |
| 44 | + } |
| 45 | + let lines: string[] = []; |
| 46 | + const rPos = result.indexOf(keyword); |
| 47 | + if (rPos != -1) { |
| 48 | + lines[0] = result.slice(0, rPos).join(" "); |
| 49 | + lines[1] = result.slice(rPos + 1, result.length).join(" "); |
| 50 | + } else { |
| 51 | + lines[0] = result.slice(0, result.length).join(" "); |
| 52 | + lines[1] = ""; |
| 53 | + } |
| 54 | + return lines; |
| 55 | +} |
| 56 | + |
| 57 | +//Assumes that the string passed in starts with ( and continues to ) and does not contain any comments |
| 58 | +export function getFunctionParams(text: string): paramDef[] { |
| 59 | + var params: paramDef[] = []; |
| 60 | + //Start by looking for the function name declaration |
| 61 | + var index = 0; |
| 62 | + text = text.trim(); // delete the leading and trailing spaces |
| 63 | + //if there is no '(' then this is not a valid function declaration |
| 64 | + if (text.charAt(index) == '(') { |
| 65 | + //count the number of matching opening and closing braces. Keep parsing until 0 |
| 66 | + var numBraces = 1; |
| 67 | + index++; |
| 68 | + while ((numBraces != 0) && (index != text.length)) { |
| 69 | + var name_type: string = ''; |
| 70 | + while ((text.charAt(index) != ',') && (text.charAt(index) != ')') && (index < text.length)) { |
| 71 | + name_type = name_type + text.charAt(index); |
| 72 | + index++; |
| 73 | + } |
| 74 | + if (text.charAt(index) == ')') { |
| 75 | + numBraces--; |
| 76 | + } |
| 77 | + name_type = name_type.trim() // trim leading and trailing whitespace |
| 78 | + let parts = name_type.split(" "); // spitting a returns element (bool b, uint256 u, ...) |
| 79 | + let idx = parts.indexOf("memory", 0); // remove memory from parts |
| 80 | + if (idx > -1) { |
| 81 | + parts.splice(idx, 1); |
| 82 | + } |
| 83 | + idx = parts.indexOf("calldata", 0); // remove calldata from parts |
| 84 | + if (idx > -1) { |
| 85 | + parts.splice(idx, 1); |
| 86 | + } |
| 87 | + idx = parts.indexOf("storage", 0); // remove storage from parts |
| 88 | + if (idx > -1) { |
| 89 | + parts.splice(idx, 1); |
| 90 | + } |
| 91 | + if (parts.length == 1) { |
| 92 | + params.push(new paramDef("", "")); |
| 93 | + } else if (parts.length == 2) { |
| 94 | + params.push(new paramDef(parts[1], parts[0])); |
| 95 | + } else { |
| 96 | + const errorMessage = "Arguments are malformed in the selected code"; |
| 97 | + throw new Error(errorMessage); |
| 98 | + } |
| 99 | + if (index < text.length) { |
| 100 | + index++; |
| 101 | + while (text.charAt(index) == ' ') index++; // consume whitespace |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return params; |
| 106 | +} |
| 107 | + |
| 108 | +//Assumes that the string passed in starts with ( and continues to ) and does not contain any comments |
| 109 | +export function getReturnParams(text: string): paramDef[] { |
| 110 | + var params: paramDef[] = []; |
| 111 | + //Start by looking for the function name declaration |
| 112 | + var index = 0; |
| 113 | + text = text.trim(); // delete the leading and trailing spaces |
| 114 | + //if there is no '(' then this is not a valid function declaration |
| 115 | + if (text.charAt(index) == '(') { |
| 116 | + //count the number of matching opening and closing braces. Keep parsing until 0 |
| 117 | + var numBraces = 1; |
| 118 | + index++; |
| 119 | + while ((numBraces != 0) && (index != text.length)) { |
| 120 | + var name_type: string = ''; |
| 121 | + while ((text.charAt(index) != ',') && (text.charAt(index) != ')') && (index < text.length)) { |
| 122 | + name_type = name_type + text.charAt(index); |
| 123 | + index++; |
| 124 | + } |
| 125 | + if (text.charAt(index) == ')') { |
| 126 | + numBraces--; |
| 127 | + } |
| 128 | + name_type = name_type.trim() // trim leading and trailing whitespace |
| 129 | + const parts = name_type.split(" "); // spitting a returns element (bool b, uint256 u, ...) or (bool, uint256, ...) |
| 130 | + let idx = parts.indexOf("memory", 0); // remove memory from parts |
| 131 | + if (idx > -1) { |
| 132 | + parts.splice(idx, 1); |
| 133 | + } |
| 134 | + idx = parts.indexOf("calldata", 0); // remove calldata from parts |
| 135 | + if (idx > -1) { |
| 136 | + parts.splice(idx, 1); |
| 137 | + } |
| 138 | + idx = parts.indexOf("storage", 0); // remove storage from parts |
| 139 | + if (idx > -1) { |
| 140 | + parts.splice(idx, 1); |
| 141 | + } |
| 142 | + if (parts.length == 0) { |
| 143 | + params.push(new paramDef("", "")); |
| 144 | + } else if (parts.length == 2) { |
| 145 | + params.push(new paramDef(parts[1], parts[0])); |
| 146 | + } else if (parts.length == 1) { |
| 147 | + params.push(new paramDef("", parts[0])); |
| 148 | + } else { |
| 149 | + const errorMessage = "Elements of returns are malformed in the selected code"; |
| 150 | + throw new Error(errorMessage); |
| 151 | + } |
| 152 | + if (index < text.length) { |
| 153 | + index++; |
| 154 | + while (text.charAt(index) == ' ') index++; // consume whitespace |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + return params; |
| 159 | +} |
| 160 | + |
| 161 | +export function stripComments(text: string): string { |
| 162 | + var uncommentedText: string = ''; |
| 163 | + var index = 0; |
| 164 | + while (index != text.length) { |
| 165 | + if ((text.charAt(index) == '/') && (text.charAt(index + 1) == '*')) { |
| 166 | + //parse comment |
| 167 | + if ((index + 2) != text.length) { //Check for the corner case that the selected text contains a /* right at the end |
| 168 | + index = index + 2; |
| 169 | + while ((text.charAt(index) != '*') && (text.charAt(index + 1) != '/')) { |
| 170 | + index++; |
| 171 | + } |
| 172 | + } |
| 173 | + index = index + 2; |
| 174 | + } |
| 175 | + else if ((text.charAt(index) == '/') && (text.charAt(index + 1) == '/')) { |
| 176 | + //read to end of line |
| 177 | + while ((text.charAt(index) != '\n') && (index < text.length)) { |
| 178 | + index++; |
| 179 | + } |
| 180 | + } |
| 181 | + else { |
| 182 | + uncommentedText = uncommentedText + text.charAt(index); |
| 183 | + index++; |
| 184 | + } |
| 185 | + } |
| 186 | + return uncommentedText; |
| 187 | +} |
0 commit comments