@@ -7,12 +7,14 @@ import child_process from 'child_process';
7
7
import fs from 'fs' ;
8
8
import * as uuid from 'uuid' ;
9
9
import YAML from 'yaml' ;
10
+ import { LintErrorDTO } from './dto/lint-error.dto' ;
11
+ import { LintResultDTO } from './dto/lint-result.dto' ;
10
12
import { ConvertToGolangCILintOutput } from './linters/golangcilint' ;
11
13
import { ConvertToPylintOutput } from './linters/pylint' ;
12
14
13
15
@Injectable ( )
14
16
export class LintService {
15
- lintPython3 ( code : string ) : number {
17
+ lintPython3 ( code : string ) : LintResultDTO {
16
18
const result = child_process . spawnSync (
17
19
'pylint' ,
18
20
[ '--from-stdin' , '-f' , 'json' , 'module_or_package' , '--' ] ,
@@ -49,7 +51,22 @@ export class LintService {
49
51
50
52
Remove one point to the score per violation
51
53
*/
52
- return 100 - pylintOutput . length ;
54
+ const score = 100 - pylintOutput . length ;
55
+ const errors : LintErrorDTO [ ] = [ ] ;
56
+
57
+ for ( const violation of pylintOutput ) {
58
+ errors . push ( {
59
+ message : violation . message ,
60
+ line : violation . line ,
61
+ column : violation . column ,
62
+ offset : null ,
63
+ } ) ;
64
+ }
65
+
66
+ return {
67
+ score,
68
+ errors,
69
+ } ;
53
70
}
54
71
} catch ( e ) {
55
72
throw new InternalServerErrorException ( ) ;
@@ -58,7 +75,7 @@ export class LintService {
58
75
throw new InternalServerErrorException ( ) ;
59
76
}
60
77
61
- lintGolang ( code : string ) : number {
78
+ lintGolang ( code : string ) : LintResultDTO {
62
79
// Golangci-lint doesn't support stdin
63
80
const path = `/tmp/codebench_${ uuid . v4 ( ) } .go` ;
64
81
try {
@@ -112,19 +129,35 @@ export class LintService {
112
129
*/
113
130
if ( lintOuput . issues ) {
114
131
// Remove one point to the score per violation
115
- return 100 - lintOuput . issues . length ;
132
+ const score = 100 - lintOuput . issues . length ;
133
+ const errors : LintErrorDTO [ ] = [ ] ;
134
+
135
+ for ( const issue of lintOuput . issues ) {
136
+ errors . push ( {
137
+ message : issue . text ,
138
+ line : issue . pos . line ,
139
+ column : issue . pos . column ,
140
+ offset : null ,
141
+ } ) ;
142
+ }
143
+ return {
144
+ score,
145
+ errors,
146
+ } ;
116
147
}
117
148
// Golangci-lint return `null` if there are no violation
118
- return 100 ;
119
149
}
120
150
} catch ( e ) {
121
151
throw new InternalServerErrorException ( e ) ;
122
152
}
123
153
124
- throw new InternalServerErrorException ( ) ;
154
+ return {
155
+ score : 100 ,
156
+ errors : [ ] ,
157
+ } ;
125
158
}
126
159
127
- lintCpp ( code : string ) : number {
160
+ lintCpp ( code : string ) : LintResultDTO {
128
161
// clang-tidy doesn't support stdin
129
162
const path = `/tmp/codebench_${ uuid . v4 ( ) } .cpp` ;
130
163
const outputPath = `${ path } .yaml` ;
@@ -152,27 +185,46 @@ export class LintService {
152
185
if ( fs . existsSync ( outputPath ) ) {
153
186
const file = fs . readFileSync ( `${ path } .yaml` , 'utf8' ) ;
154
187
const fixes : any = YAML . parse ( file ) ;
188
+
155
189
if ( fixes ) {
156
190
/*
157
191
Example file:
158
192
---
159
193
MainSourceFile: /root/fib.cpp
160
194
Diagnostics:
161
- - DiagnosticName: clang-diagnostic-unused-variable
162
- Message: 'unused variable ''d'''
163
- FileOffset: 142
164
- FilePath: /root/fib.cpp
165
- Replacements:
195
+ - DiagnosticName: clang-diagnostic-unused-variable
196
+ Message: 'unused variable ''d'''
197
+ FileOffset: 142
198
+ FilePath: /root/fib.cpp
199
+ Replacements:
166
200
...
167
201
*/
168
202
if ( fixes . Diagnostics ) {
169
- return 100 - fixes . Diagnostics . length ;
203
+ const score = 100 - fixes . Diagnostics . length ;
204
+ const errors : LintErrorDTO [ ] = [ ] ;
205
+
206
+ for ( const diagnostic of fixes . Diagnostics ) {
207
+ errors . push ( {
208
+ message : diagnostic . Message ,
209
+ line : diagnostic . FileOffset ,
210
+ column : null ,
211
+ offset : null ,
212
+ } ) ;
213
+ }
214
+ return {
215
+ score,
216
+ errors,
217
+ } ;
170
218
}
171
219
}
172
220
}
173
- return 100 ;
174
221
} catch ( e ) {
175
222
throw new InternalServerErrorException ( e ) ;
176
223
}
224
+
225
+ return {
226
+ score : 100 ,
227
+ errors : [ ] ,
228
+ } ;
177
229
}
178
230
}
0 commit comments