|
1 | 1 | import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor';
|
2 |
| -import { CharStreams, CommonTokenStream } from 'antlr4ts'; |
3 | 2 | import { GoParserVisitor } from './generated/GoParserVisitor';
|
4 | 3 | import { QualityDTO } from '../../dto/quality.dto';
|
5 |
| -import { GoLexer } from './generated/GoLexer'; |
6 |
| -import { FunctionDeclContext, GoParser } from './generated/GoParser'; |
| 4 | +import { CommonQualityFunction } from '../common/CommonQualityFunction'; |
7 | 5 |
|
8 |
| -export class GoQualityVistor |
| 6 | +export class GoQualityVisitor |
9 | 7 | extends AbstractParseTreeVisitor<void>
|
10 | 8 | implements GoParserVisitor<void>
|
11 | 9 | {
|
12 | 10 | private codeQuality: QualityDTO;
|
13 | 11 |
|
14 | 12 | run(source: string): QualityDTO {
|
15 |
| - const inputStream = CharStreams.fromString(source); |
16 |
| - const lexer = new GoLexer(inputStream); |
17 |
| - const tokenStream = new CommonTokenStream(lexer); |
18 |
| - const parser = new GoParser(tokenStream); |
19 |
| - |
20 | 13 | this.codeQuality = {
|
21 | 14 | score: 100,
|
22 | 15 | };
|
23 | 16 |
|
24 |
| - const context = parser.functionDecl(); |
25 |
| - this.visit(context); |
| 17 | + const lines = source.split('\n'); |
| 18 | + |
| 19 | + this.qualityEvalFunctionName(lines); |
| 20 | + |
| 21 | + this.qualityEvalFunctionBody(lines); |
26 | 22 |
|
27 | 23 | return this.codeQuality;
|
28 | 24 | }
|
29 | 25 |
|
30 |
| - visitFunctionDecl(context: FunctionDeclContext): void { |
31 |
| - context.IDENTIFIER(); |
| 26 | + /** |
| 27 | + * Applying quality rules on function name |
| 28 | + * @param lines |
| 29 | + * @private |
| 30 | + */ |
| 31 | + private qualityEvalFunctionName(lines: string[]): void { |
| 32 | + // Applying rules on function name |
| 33 | + lines |
| 34 | + .filter((line) => line.startsWith('func')) |
| 35 | + .map((line) => line.substring(5, line.indexOf('(')).trim()) |
| 36 | + // If function name name is greater than 25 character => quality -1 |
| 37 | + .map((functionName) => { |
| 38 | + if (functionName.length > 25) { |
| 39 | + this.codeQuality.score -= 1; |
| 40 | + } |
| 41 | + return functionName; |
| 42 | + }) |
| 43 | + // If function name is not written is snake case => quality -3 |
| 44 | + .map((functionName) => { |
| 45 | + if (!CommonQualityFunction.isSnakeCase(functionName)) { |
| 46 | + this.codeQuality.score -= 3; |
| 47 | + } |
| 48 | + return functionName; |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Applying quality rules on function body |
| 54 | + * @param lines |
| 55 | + * @private |
| 56 | + */ |
| 57 | + private qualityEvalFunctionBody(lines: string[]): void { |
| 58 | + let counter = 0; |
| 59 | + let isInFunction = false; |
| 60 | + lines.forEach((line) => { |
| 61 | + if (line.startsWith('func') && line.endsWith('{')) { |
| 62 | + isInFunction = true; |
| 63 | + } else if (isInFunction) { |
| 64 | + counter += 1; |
| 65 | + } |
| 66 | + if (line.trim().endsWith('}')) { |
| 67 | + if (counter > 30) { |
| 68 | + this.codeQuality.score -= 5; |
| 69 | + } |
| 70 | + isInFunction = false; |
| 71 | + counter = 0; |
| 72 | + } |
| 73 | + }); |
32 | 74 | }
|
33 | 75 |
|
34 | 76 | // eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
0 commit comments