halstead-metrics 0.0.0-stage → 1.0.0
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.
- package/package.json +33 -5
- package/src/analyze.js +44 -0
- package/src/genericLexer.js +513 -0
- package/src/index.js +33 -0
- package/src/keywords.js +44 -0
- package/src/languages.js +448 -0
- package/src/metrics.js +255 -0
- package/src/report.js +567 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "halstead-metrics",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Halstead metrics for all major programming languages",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test:unit": "node --test test/unit/*.js",
|
|
11
|
+
"test:integration": "node --test test/integration/*.js",
|
|
12
|
+
"test": "npm run test:unit && npm run test:integration"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"pdfkit": "^0.17.2"
|
|
19
|
+
},
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Harish Ramasubramanian",
|
|
22
|
+
"email": "rharish100193@gmail.com",
|
|
23
|
+
"url": "https://github.com/rharish100193"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/rharish100193/halstead-metrics.git",
|
|
29
|
+
"directory": "packages/halstead-metrics"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/analyze.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
|
|
5
|
+
const { getModernLanguage } = require("./languages");
|
|
6
|
+
const { tokenizeGeneric } = require("./genericLexer");
|
|
7
|
+
const { classify, toReport } = require("./metrics");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} source
|
|
11
|
+
* @param {string} extension extension without the leading dot
|
|
12
|
+
*/
|
|
13
|
+
function analyzeSource(source, extension) {
|
|
14
|
+
const language = getModernLanguage(extension);
|
|
15
|
+
if (!language) {
|
|
16
|
+
throw new Error("Unsupported extension: " + extension);
|
|
17
|
+
}
|
|
18
|
+
const tokens = tokenizeGeneric(source, language);
|
|
19
|
+
return classify(tokens, language.keywords, language.operators);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} filePath
|
|
24
|
+
* @param {string} extension
|
|
25
|
+
*/
|
|
26
|
+
function analyzeFile(filePath, extension) {
|
|
27
|
+
const source = fs.readFileSync(filePath, "utf8");
|
|
28
|
+
return analyzeSource(source, extension);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} source
|
|
33
|
+
* @param {string} extension
|
|
34
|
+
* @param {string} filename
|
|
35
|
+
*/
|
|
36
|
+
function reportSource(source, extension, filename) {
|
|
37
|
+
return toReport(analyzeSource(source, extension), filename);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
analyzeSource,
|
|
42
|
+
analyzeFile,
|
|
43
|
+
reportSource,
|
|
44
|
+
};
|
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const STRING_PREFIXES = ["rf", "fr", "rb", "br", "ur", "ru", "r", "u", "b", "f"];
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Tokenize a language defined in languages.js.
|
|
7
|
+
* The trailing <EOF> matches the classic lexer.
|
|
8
|
+
* @param {string} source
|
|
9
|
+
* @param {object} lang
|
|
10
|
+
* @returns {string[]}
|
|
11
|
+
*/
|
|
12
|
+
function tokenizeGeneric(source, lang) {
|
|
13
|
+
const text = source.charCodeAt(0) === 0xfeff ? source.slice(1) : source;
|
|
14
|
+
const operators = lang.operators.slice().sort((a, b) => b.length - a.length);
|
|
15
|
+
const lineComments = lang.lineComments.slice().sort((a, b) => b.length - a.length);
|
|
16
|
+
const blockComments = lang.blockComments.slice().sort((a, b) => b[0].length - a[0].length);
|
|
17
|
+
const tokens = [];
|
|
18
|
+
let i = 0;
|
|
19
|
+
let atLineStart = true;
|
|
20
|
+
|
|
21
|
+
if (text.startsWith("#!")) {
|
|
22
|
+
i = skipToEol(text, 2);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
while (i < text.length) {
|
|
26
|
+
const ws = skipWhitespace(text, i);
|
|
27
|
+
if (ws > i) {
|
|
28
|
+
if (text.slice(i, ws).includes("\n")) {
|
|
29
|
+
atLineStart = true;
|
|
30
|
+
}
|
|
31
|
+
i = ws;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (atLineStart && lang.preprocessor && text[i] === "#") {
|
|
35
|
+
i = skipToEol(text, i + 1);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
atLineStart = false;
|
|
39
|
+
|
|
40
|
+
if (lang.dollar && text[i] === "$" && text[i + 1] && !isIdentStart(text[i + 1], lang) && text[i + 1] !== "{" && !isWhitespace(text[i + 1])) {
|
|
41
|
+
tokens.push(text.slice(i, i + 2));
|
|
42
|
+
i += 2;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const block = matchBlock(text, i, blockComments);
|
|
47
|
+
if (block) {
|
|
48
|
+
i = block;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const line = matchLine(text, i, lineComments);
|
|
52
|
+
if (line > i) {
|
|
53
|
+
i = line;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (lang.rustRaw) {
|
|
58
|
+
const raw = scanRustRaw(text, i);
|
|
59
|
+
if (raw) {
|
|
60
|
+
tokens.push(raw.text);
|
|
61
|
+
i = raw.index;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (lang.verbatimAt && text[i] === "@" && text[i + 1] === "\"") {
|
|
66
|
+
const verbatim = scanVerbatim(text, i);
|
|
67
|
+
tokens.push(verbatim.text);
|
|
68
|
+
i = verbatim.index;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const literal = scanString(text, i, lang);
|
|
72
|
+
if (literal) {
|
|
73
|
+
tokens.push(literal.text);
|
|
74
|
+
i = literal.index;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (lang.lifetimes && text[i] === "'" && isIdentStart(text[i + 1], lang)) {
|
|
78
|
+
const life = scanIdent(text, i + 1, lang);
|
|
79
|
+
if (life) {
|
|
80
|
+
tokens.push(text.slice(i, life.index));
|
|
81
|
+
i = life.index;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const numberEnd = scanNumber(text, i);
|
|
87
|
+
if (numberEnd !== null) {
|
|
88
|
+
tokens.push(text.slice(i, numberEnd));
|
|
89
|
+
i = numberEnd;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (lang.atPrefix && text[i] === "@") {
|
|
94
|
+
let marks = i;
|
|
95
|
+
while (text[marks] === "@") {
|
|
96
|
+
marks += 1;
|
|
97
|
+
}
|
|
98
|
+
const named = scanIdent(text, marks, lang);
|
|
99
|
+
if (named && marks > i) {
|
|
100
|
+
tokens.push(text.slice(i, named.index));
|
|
101
|
+
i = named.index;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const ident = scanIdent(text, i, lang);
|
|
107
|
+
if (ident) {
|
|
108
|
+
let word = ident.text;
|
|
109
|
+
if (lang.caseInsensitive && lang.keywords.has(word.toLowerCase())) {
|
|
110
|
+
word = word.toLowerCase();
|
|
111
|
+
}
|
|
112
|
+
tokens.push(word);
|
|
113
|
+
i = ident.index;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const op = matchOperator(text, i, operators);
|
|
118
|
+
if (op) {
|
|
119
|
+
tokens.push(op);
|
|
120
|
+
i += op.length;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
tokens.push(text[i]);
|
|
125
|
+
i += 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
tokens.push("<EOF>");
|
|
129
|
+
return tokens;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {string} text
|
|
134
|
+
* @param {number} i
|
|
135
|
+
* @returns {number}
|
|
136
|
+
*/
|
|
137
|
+
function skipWhitespace(text, i) {
|
|
138
|
+
let j = i;
|
|
139
|
+
while (j < text.length && isWhitespace(text[j])) {
|
|
140
|
+
j += 1;
|
|
141
|
+
}
|
|
142
|
+
return j;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param {string} ch
|
|
147
|
+
* @returns {boolean}
|
|
148
|
+
*/
|
|
149
|
+
function isWhitespace(ch) {
|
|
150
|
+
return ch === " " || ch === "\t" || ch === "\r" || ch === "\n" || ch === "\f" || ch === "\v";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {string} text
|
|
155
|
+
* @param {number} i
|
|
156
|
+
* @returns {number}
|
|
157
|
+
*/
|
|
158
|
+
function skipToEol(text, i) {
|
|
159
|
+
let j = i;
|
|
160
|
+
while (j < text.length && text[j] !== "\n" && text[j] !== "\r") {
|
|
161
|
+
j += 1;
|
|
162
|
+
}
|
|
163
|
+
return j;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @param {string} text
|
|
168
|
+
* @param {number} i
|
|
169
|
+
* @param {string[][]} blocks
|
|
170
|
+
* @returns {number|null}
|
|
171
|
+
*/
|
|
172
|
+
function matchBlock(text, i, blocks) {
|
|
173
|
+
for (let b = 0; b < blocks.length; b += 1) {
|
|
174
|
+
const open = blocks[b][0];
|
|
175
|
+
const close = blocks[b][1];
|
|
176
|
+
if (!text.startsWith(open, i)) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const end = text.indexOf(close, i + open.length);
|
|
180
|
+
return end === -1 ? text.length : end + close.length;
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {string} text
|
|
187
|
+
* @param {number} i
|
|
188
|
+
* @param {string[]} comments
|
|
189
|
+
* @returns {number}
|
|
190
|
+
*/
|
|
191
|
+
function matchLine(text, i, comments) {
|
|
192
|
+
for (let c = 0; c < comments.length; c += 1) {
|
|
193
|
+
if (text.startsWith(comments[c], i)) {
|
|
194
|
+
return skipToEol(text, i + comments[c].length);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return i;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {string} text
|
|
202
|
+
* @param {number} i
|
|
203
|
+
* @param {string[]} operators
|
|
204
|
+
* @returns {string|null}
|
|
205
|
+
*/
|
|
206
|
+
function matchOperator(text, i, operators) {
|
|
207
|
+
for (let k = 0; k < operators.length; k += 1) {
|
|
208
|
+
if (text.startsWith(operators[k], i)) {
|
|
209
|
+
return operators[k];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {string} text
|
|
217
|
+
* @param {number} i
|
|
218
|
+
* @returns {{text: string, index: number}|null}
|
|
219
|
+
*/
|
|
220
|
+
function scanRustRaw(text, i) {
|
|
221
|
+
let j = i;
|
|
222
|
+
if (text[j] === "b" && text[j + 1] === "r") {
|
|
223
|
+
j += 1;
|
|
224
|
+
}
|
|
225
|
+
if (text[j] !== "r") {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
j += 1;
|
|
229
|
+
let hashes = "";
|
|
230
|
+
while (text[j] === "#") {
|
|
231
|
+
hashes += "#";
|
|
232
|
+
j += 1;
|
|
233
|
+
}
|
|
234
|
+
if (text[j] !== "\"") {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
j += 1;
|
|
238
|
+
const closer = "\"" + hashes;
|
|
239
|
+
const end = text.indexOf(closer, j);
|
|
240
|
+
const stop = end === -1 ? text.length : end + closer.length;
|
|
241
|
+
return { text: text.slice(i, stop), index: stop };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @param {string} text
|
|
246
|
+
* @param {number} i index of @
|
|
247
|
+
* @returns {{text: string, index: number}}
|
|
248
|
+
*/
|
|
249
|
+
function scanVerbatim(text, i) {
|
|
250
|
+
let j = i + 2;
|
|
251
|
+
while (j < text.length) {
|
|
252
|
+
if (text[j] === "\"" && text[j + 1] === "\"") {
|
|
253
|
+
j += 2;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (text[j] === "\"") {
|
|
257
|
+
j += 1;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
j += 1;
|
|
261
|
+
}
|
|
262
|
+
return { text: text.slice(i, j), index: j };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @param {string} text
|
|
267
|
+
* @param {number} i
|
|
268
|
+
* @param {object} lang
|
|
269
|
+
* @returns {{text: string, index: number}|null}
|
|
270
|
+
*/
|
|
271
|
+
function scanString(text, i, lang) {
|
|
272
|
+
let start = i;
|
|
273
|
+
let raw = false;
|
|
274
|
+
if (lang.prefixedStrings) {
|
|
275
|
+
for (let p = 0; p < STRING_PREFIXES.length; p += 1) {
|
|
276
|
+
const prefix = STRING_PREFIXES[p];
|
|
277
|
+
if (text.slice(i, i + prefix.length).toLowerCase() !== prefix) {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (!isQuoteStart(text, i + prefix.length)) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
raw = prefix.toLowerCase().includes("r");
|
|
284
|
+
start = i + prefix.length;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
if (start === i && !isQuoteStart(text, i)) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
} else if (!isQuoteStart(text, i)) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
if (lang.lifetimes && text[start] === "'" && isIdentStart(text[start + 1], lang) && text[start + 2] !== "'") {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
const quote = quoteAt(text, start);
|
|
297
|
+
let j = start + quote.length;
|
|
298
|
+
while (j < text.length) {
|
|
299
|
+
if (!raw && text[j] === "\\") {
|
|
300
|
+
j += 2;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (text.startsWith(quote, j)) {
|
|
304
|
+
j += quote.length;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
j += 1;
|
|
308
|
+
}
|
|
309
|
+
return { text: text.slice(i, j), index: j };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* @param {string} text
|
|
314
|
+
* @param {number} i
|
|
315
|
+
* @returns {boolean}
|
|
316
|
+
*/
|
|
317
|
+
function isQuoteStart(text, i) {
|
|
318
|
+
return quoteAt(text, i) !== null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @param {string} text
|
|
323
|
+
* @param {number} i
|
|
324
|
+
* @returns {string|null}
|
|
325
|
+
*/
|
|
326
|
+
function quoteAt(text, i) {
|
|
327
|
+
if ((text[i] === "\"" || text[i] === "'") && text[i + 1] === text[i] && text[i + 2] === text[i]) {
|
|
328
|
+
return text[i] + text[i] + text[i];
|
|
329
|
+
}
|
|
330
|
+
if (text[i] === "\"" || text[i] === "'" || text[i] === "`") {
|
|
331
|
+
return text[i];
|
|
332
|
+
}
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @param {string} text
|
|
338
|
+
* @param {number} i
|
|
339
|
+
* @returns {number|null}
|
|
340
|
+
*/
|
|
341
|
+
function scanNumber(text, i) {
|
|
342
|
+
let j = i;
|
|
343
|
+
if (text[j] === "0" && (text[j + 1] === "x" || text[j + 1] === "X") && isHex(text[j + 2])) {
|
|
344
|
+
j += 2;
|
|
345
|
+
while (isHex(text[j]) || text[j] === "_") {
|
|
346
|
+
j += 1;
|
|
347
|
+
}
|
|
348
|
+
return consumeSuffix(text, j);
|
|
349
|
+
}
|
|
350
|
+
if (text[j] === "0" && (text[j + 1] === "b" || text[j + 1] === "B") && (text[j + 2] === "0" || text[j + 2] === "1")) {
|
|
351
|
+
j += 2;
|
|
352
|
+
while (text[j] === "0" || text[j] === "1" || text[j] === "_") {
|
|
353
|
+
j += 1;
|
|
354
|
+
}
|
|
355
|
+
return consumeSuffix(text, j);
|
|
356
|
+
}
|
|
357
|
+
if (text[j] === "0" && (text[j + 1] === "o" || text[j + 1] === "O") && text[j + 2] >= "0" && text[j + 2] <= "7") {
|
|
358
|
+
j += 2;
|
|
359
|
+
while ((text[j] >= "0" && text[j] <= "7") || text[j] === "_") {
|
|
360
|
+
j += 1;
|
|
361
|
+
}
|
|
362
|
+
return consumeSuffix(text, j);
|
|
363
|
+
}
|
|
364
|
+
if (text[j] === "." && isDigit(text[j + 1])) {
|
|
365
|
+
j += 1;
|
|
366
|
+
j = consumeDigits(text, j);
|
|
367
|
+
j = consumeExponent(text, j);
|
|
368
|
+
return consumeSuffix(text, j);
|
|
369
|
+
}
|
|
370
|
+
if (!isDigit(text[j])) {
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
j = consumeDigits(text, j);
|
|
374
|
+
if (text[j] === "." && text[j + 1] !== "." && isDigit(text[j + 1])) {
|
|
375
|
+
j += 1;
|
|
376
|
+
j = consumeDigits(text, j);
|
|
377
|
+
}
|
|
378
|
+
if (text[j] === "e" || text[j] === "E") {
|
|
379
|
+
j = consumeExponent(text, j);
|
|
380
|
+
}
|
|
381
|
+
return consumeSuffix(text, j);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* @param {string} text
|
|
386
|
+
* @param {number} i
|
|
387
|
+
* @returns {number}
|
|
388
|
+
*/
|
|
389
|
+
function consumeDigits(text, i) {
|
|
390
|
+
let j = i;
|
|
391
|
+
while (isDigit(text[j]) || text[j] === "_") {
|
|
392
|
+
j += 1;
|
|
393
|
+
}
|
|
394
|
+
return j;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* @param {string} text
|
|
399
|
+
* @param {number} i
|
|
400
|
+
* @returns {number}
|
|
401
|
+
*/
|
|
402
|
+
function consumeExponent(text, i) {
|
|
403
|
+
if (text[i] !== "e" && text[i] !== "E") {
|
|
404
|
+
return i;
|
|
405
|
+
}
|
|
406
|
+
let j = i + 1;
|
|
407
|
+
if (text[j] === "+" || text[j] === "-") {
|
|
408
|
+
j += 1;
|
|
409
|
+
}
|
|
410
|
+
if (!isDigit(text[j])) {
|
|
411
|
+
return i;
|
|
412
|
+
}
|
|
413
|
+
return consumeDigits(text, j);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @param {string} text
|
|
418
|
+
* @param {number} i
|
|
419
|
+
* @returns {number}
|
|
420
|
+
*/
|
|
421
|
+
function consumeSuffix(text, i) {
|
|
422
|
+
const slice = text.slice(i, i + 3);
|
|
423
|
+
const match = /^(?:n|[uU](?:ll|LL|[lL])?|[lL][lL][uU]|[lL][uU]|[lL]{1,2}|[fFdD])/.exec(slice);
|
|
424
|
+
if (!match) {
|
|
425
|
+
return i;
|
|
426
|
+
}
|
|
427
|
+
const after = text[i + match[0].length];
|
|
428
|
+
if (after && /[A-Za-z0-9_]/.test(after)) {
|
|
429
|
+
return i;
|
|
430
|
+
}
|
|
431
|
+
return i + match[0].length;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* @param {string} text
|
|
436
|
+
* @param {number} i
|
|
437
|
+
* @param {object} lang
|
|
438
|
+
* @returns {{text: string, index: number}|null}
|
|
439
|
+
*/
|
|
440
|
+
function scanIdent(text, i, lang) {
|
|
441
|
+
if (!isIdentStart(text[i], lang)) {
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
let j = i + 1;
|
|
445
|
+
while (j < text.length) {
|
|
446
|
+
if (isIdentPart(text[j], lang)) {
|
|
447
|
+
j += 1;
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (lang.hyphenIdent && text[j] === "-" && isIdentStart(text[j + 1], lang)) {
|
|
451
|
+
j += 2;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
if (lang.identSuffix && lang.identSuffix.includes(text[j]) && !isIdentPart(text[j + 1], lang)) {
|
|
457
|
+
j += 1;
|
|
458
|
+
}
|
|
459
|
+
return { text: text.slice(i, j), index: j };
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* @param {string} ch
|
|
464
|
+
* @param {object} lang
|
|
465
|
+
* @returns {boolean}
|
|
466
|
+
*/
|
|
467
|
+
function isIdentStart(ch, lang) {
|
|
468
|
+
if (!ch) {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
if (lang.dollar && ch === "$") {
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
if (ch === "_" || /[A-Za-z]/.test(ch)) {
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
return ch.codePointAt(0) > 127 && /\p{L}/u.test(ch);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* @param {string} ch
|
|
482
|
+
* @param {object} lang
|
|
483
|
+
* @returns {boolean}
|
|
484
|
+
*/
|
|
485
|
+
function isIdentPart(ch, lang) {
|
|
486
|
+
if (!ch) {
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
if (isIdentStart(ch, lang) || isDigit(ch)) {
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
return lang.identPartExtra.includes(ch);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* @param {string} ch
|
|
497
|
+
* @returns {boolean}
|
|
498
|
+
*/
|
|
499
|
+
function isDigit(ch) {
|
|
500
|
+
return ch >= "0" && ch <= "9";
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* @param {string} ch
|
|
505
|
+
* @returns {boolean}
|
|
506
|
+
*/
|
|
507
|
+
function isHex(ch) {
|
|
508
|
+
return isDigit(ch) || (ch >= "a" && ch <= "f") || (ch >= "A" && ch <= "F");
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
module.exports = {
|
|
512
|
+
tokenizeGeneric,
|
|
513
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { analyzeFile, analyzeSource, reportSource } = require("./analyze");
|
|
4
|
+
const {
|
|
5
|
+
C_KEYWORDS,
|
|
6
|
+
CPP_KEYWORDS,
|
|
7
|
+
JAVA_KEYWORDS,
|
|
8
|
+
SUPPORTED_EXTENSIONS,
|
|
9
|
+
keywordsForExtension,
|
|
10
|
+
languageForExtension,
|
|
11
|
+
} = require("./keywords");
|
|
12
|
+
const { syntaxForExtension } = require("./languages");
|
|
13
|
+
const { formatRounded, toReport, roundTo2Decimals } = require("./metrics");
|
|
14
|
+
const { renderHtmlReport, renderPdfReport, reportBaseName } = require("./report");
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
analyzeFile,
|
|
18
|
+
analyzeSource,
|
|
19
|
+
reportSource,
|
|
20
|
+
toReport,
|
|
21
|
+
roundTo2Decimals,
|
|
22
|
+
formatRounded,
|
|
23
|
+
renderHtmlReport,
|
|
24
|
+
renderPdfReport,
|
|
25
|
+
reportBaseName,
|
|
26
|
+
SUPPORTED_EXTENSIONS,
|
|
27
|
+
languageForExtension,
|
|
28
|
+
keywordsForExtension,
|
|
29
|
+
syntaxForExtension,
|
|
30
|
+
JAVA_KEYWORDS,
|
|
31
|
+
C_KEYWORDS,
|
|
32
|
+
CPP_KEYWORDS,
|
|
33
|
+
};
|
package/src/keywords.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
C_KEYWORDS,
|
|
5
|
+
CPP_KEYWORDS,
|
|
6
|
+
JAVA_KEYWORDS,
|
|
7
|
+
getModernLanguage,
|
|
8
|
+
modernExtensions,
|
|
9
|
+
} = require("./languages");
|
|
10
|
+
|
|
11
|
+
const SUPPORTED_EXTENSIONS = new Set(modernExtensions);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} extension extension without the leading dot
|
|
15
|
+
* @returns {"java"|"c"|"cpp"|null}
|
|
16
|
+
*/
|
|
17
|
+
function languageForExtension(extension) {
|
|
18
|
+
const language = getModernLanguage(extension);
|
|
19
|
+
if (!language) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
if (language.id === "java" || language.id === "c" || language.id === "cpp") {
|
|
23
|
+
return language.id;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} extension
|
|
30
|
+
* @returns {Set<string>|null}
|
|
31
|
+
*/
|
|
32
|
+
function keywordsForExtension(extension) {
|
|
33
|
+
const language = getModernLanguage(extension);
|
|
34
|
+
return language ? language.keywords : null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = {
|
|
38
|
+
JAVA_KEYWORDS,
|
|
39
|
+
C_KEYWORDS,
|
|
40
|
+
CPP_KEYWORDS,
|
|
41
|
+
SUPPORTED_EXTENSIONS,
|
|
42
|
+
languageForExtension,
|
|
43
|
+
keywordsForExtension,
|
|
44
|
+
};
|