@@ -7,12 +7,14 @@ import child_process from 'child_process';
77import fs from 'fs' ;
88import * as uuid from 'uuid' ;
99import YAML from 'yaml' ;
10+ import { LintErrorDTO } from './dto/lint-error.dto' ;
11+ import { LintResultDTO } from './dto/lint-result.dto' ;
1012import { ConvertToGolangCILintOutput } from './linters/golangcilint' ;
1113import { ConvertToPylintOutput } from './linters/pylint' ;
1214
1315@Injectable ( )
1416export class LintService {
15- lintPython3 ( code : string ) : number {
17+ lintPython3 ( code : string ) : LintResultDTO {
1618 const result = child_process . spawnSync (
1719 'pylint' ,
1820 [ '--from-stdin' , '-f' , 'json' , 'module_or_package' , '--' ] ,
@@ -49,7 +51,22 @@ export class LintService {
4951
5052 Remove one point to the score per violation
5153 */
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+ } ;
5370 }
5471 } catch ( e ) {
5572 throw new InternalServerErrorException ( ) ;
@@ -58,7 +75,7 @@ export class LintService {
5875 throw new InternalServerErrorException ( ) ;
5976 }
6077
61- lintGolang ( code : string ) : number {
78+ lintGolang ( code : string ) : LintResultDTO {
6279 // Golangci-lint doesn't support stdin
6380 const path = `/tmp/codebench_${ uuid . v4 ( ) } .go` ;
6481 try {
@@ -112,19 +129,35 @@ export class LintService {
112129 */
113130 if ( lintOuput . issues ) {
114131 // 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+ } ;
116147 }
117148 // Golangci-lint return `null` if there are no violation
118- return 100 ;
119149 }
120150 } catch ( e ) {
121151 throw new InternalServerErrorException ( e ) ;
122152 }
123153
124- throw new InternalServerErrorException ( ) ;
154+ return {
155+ score : 100 ,
156+ errors : [ ] ,
157+ } ;
125158 }
126159
127- lintCpp ( code : string ) : number {
160+ lintCpp ( code : string ) : LintResultDTO {
128161 // clang-tidy doesn't support stdin
129162 const path = `/tmp/codebench_${ uuid . v4 ( ) } .cpp` ;
130163 const outputPath = `${ path } .yaml` ;
@@ -152,27 +185,46 @@ export class LintService {
152185 if ( fs . existsSync ( outputPath ) ) {
153186 const file = fs . readFileSync ( `${ path } .yaml` , 'utf8' ) ;
154187 const fixes : any = YAML . parse ( file ) ;
188+
155189 if ( fixes ) {
156190 /*
157191 Example file:
158192 ---
159193 MainSourceFile: /root/fib.cpp
160194 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:
166200 ...
167201 */
168202 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+ } ;
170218 }
171219 }
172220 }
173- return 100 ;
174221 } catch ( e ) {
175222 throw new InternalServerErrorException ( e ) ;
176223 }
224+
225+ return {
226+ score : 100 ,
227+ errors : [ ] ,
228+ } ;
177229 }
178230}
0 commit comments