Completed
Push — master ( 9a2275...38ec3f )
by Jean
57s
created

parse.js ➔ parse   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 9.4285
1
/**
2
 * @file Module 'codingame/parse'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse response coming from Codingame's services
7
 * @module codingame/parse
8
 */
9
import CodingameError from './error.js';
10
import parse_failcompile from './parsers/failcompile.js';
11
import parse_failexpected from './parsers/failexpected.js';
12
import parse_success from './parsers/success.js';
13
14
let parsers = [];
15
parsers.push(parse_failcompile);
16
parsers.push(parse_failexpected);
17
parsers.push(parse_success);
18
19
/**
20
 * Parse response coming from tests sent to Codingame website
21
 *
22
 * @name parse
23
 * @function
24
 * @param {Object} response Body of the response
25
 * @returns {Promise<Object|CodingameError>} Resolve if test is a success, reject with a CodingameError if test failed or parsing was impossible
26
 */
27
let parse = function parse(response) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parse already seems to be declared on line 27. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
28
	let body = response.success;
29
	for (let parser of parsers) {
30
		try {
31
			let promise = parser.parse(body);
32
			return Promise.resolve(promise); // Resume loop if not Error has been thrown
33
		} catch (error) {
34
			continue;
0 ignored issues
show
Unused Code introduced by
This continue has no effect on the loop flow and can be removed.
Loading history...
35
		}
36
	}
37
	let message = `Unknown error. Codingame may have changed its API.  Please contact 'codingame-connector' developer.`;
38
	let error = new Error(message);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable error already seems to be declared on line 33. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Comprehensibility introduced by
You are assigning a value to the catch variable error, thereby shadowing the original value.
Loading history...
39
	return Promise.reject(error);
40
};
41
42
export default {
43
	"parse": parse
44
};
45