-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
134 lines (118 loc) · 3.82 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint-disable */
const { isString } = require('util');
const commentRegex = /\/\/.*?$/gimu;
const lineCommentRegex = /^\/\/.*?$/gimu;
const blockCommentRegex = /\*[^*]*\*+(?:[^/*][^*]*\*+)*/gimu;
const quoteRegex = /\\"|"(?:\\"|[^"])*"|(\+)/gimu;
const pubPrivRegex =
/(pub|priv)\s+(contract|resource|struct|event|enum|case|entitlement|view|fun|var|let)/gimu;
// Doh...
const importTokenPart = `("[\\w\\d]+?")|('[\\w\\d]+?')|([\\w\\d]+)`;
const importRegex = new RegExp(
`^\\s*import\\s+((:?${importTokenPart}\\s*,\\s*)*${importTokenPart})\\s*?(?!from)[^\\n]*?$`,
'gium',
);
const importFromRegex = new RegExp(
`^\\s*import\\s+((:?${importTokenPart}\\s*,\\s*)*${importTokenPart})\\s*?from`,
'gium',
);
function blankRegex(str, regex) {
let searchData = str;
const matches = searchData.matchAll(regex);
for (const match of matches) {
const len = match[0].length;
searchData =
searchData.slice(0, Math.max(0, match.index)) +
' '.repeat(len) +
searchData.substring(match.index + len, searchData.length);
}
return searchData;
}
function replaceSubstituted(data, searchData, regex, replace) {
const matches = searchData.matchAll(regex);
let res = data;
let pad = 0;
for (const match of matches) {
const len = match[0].length;
const index = match.index + pad;
const replaced = isString(replace) ? replace : replace(match);
pad += replaced.length - len;
res =
res.slice(0, Math.max(0, index)) +
replaced +
res.substring(index + len, res.length);
}
return res;
}
function replaceSubstitutedEscaped(data, regex, replace) {
let searchData = blankRegex(data, lineCommentRegex);
searchData = blankRegex(searchData, blockCommentRegex);
searchData = blankRegex(searchData, quoteRegex);
searchData = blankRegex(searchData, commentRegex);
return replaceSubstituted(data, searchData, regex, replace);
}
String.prototype.splice = function(index, count, add) {
if (index < 0) {
index += this.length;
if (index < 0)
index = 0;
}
return this.slice(0, index) + (add || "") + this.slice(index + count);
}
const collapseSpaces = function collapseSpaces(input) {
return input.replace(/\s+/g, " ");
};
const removeSpaces = function removeSpaces(input) {
return input.replace(/\s+/g, "");
};
const stripParen = function stripParen(code, prefix) {
const quoteRegex = new RegExp(`${prefix?`${prefix}\\s*`:''}\\([^)]*?\\)` ,"gimu");
return code.replace(quoteRegex, "");
}
const stripComments = function stripComments(code) {
let commentsRegExp = /(\/\*[\s\S]*?\*\/)|(\/\/.*)/g;
return code.replace(commentsRegExp, "");
};
const stripStrings = function stripStrings(code) {
// replace all strings with ""
let inString = false;
let res = "";
for (const i = 0; i < code.length; i++) {
if (code[i] === '"') {
if (inString && code[i - 1] !== "\\") {
inString = false;
res += code[i];
} else inString = true;
}
if (!inString) {
res += code[i];
}
}
return res;
};
const generateSchema = function generateSchema(argsDefinition) {
return stripParen(argsDefinition, 'auth').split(",").map(function (item) {
return item.replace(/\s*/g, "");
}).filter(function (item) {
return item !== "";
});
};
const extract = function extract(code, keyWord) {
const noComments = stripComments(code);
const target = collapseSpaces(noComments.replace(/[\n\r]/g, ""));
if (target) {
const regexp = new RegExp(keyWord, "g");
const match = regexp.exec(target);
if (match) {
if (match[1] === "") {
return [];
}
return generateSchema(match[1]);
}
}
return [];
};
const extractSigners = function extractSigners(code) {
return extract(code, "(?:prepare\\s*\\(\\s*)(.+)(?:\\)\\s*?{)");
};
module.exports = {extract, extractSigners, blankRegex, replaceSubstituted, replaceSubstitutedEscaped}