Total Complexity | 5 |
Complexity/F | 5 |
Lines of Code | 41 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | /** |
||
23 | import CodingameError from '../error.js'; |
||
24 | |||
25 | let name = `fail-expected`; |
||
26 | |||
27 | /** |
||
28 | * Attempt to parse the body of a successful request to Codingame test API. |
||
29 | * This function will try to map a response for a failed test because of a unexpected result for the test. |
||
30 | * |
||
31 | * @function parse |
||
32 | * @param {Object} body Body of the response |
||
33 | * @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful |
||
34 | * @throws {Error} Throw is parsing failed |
||
35 | * @instance |
||
36 | */ |
||
37 | let parse = function parse(body) { |
||
|
|||
38 | try { |
||
39 | let { |
||
40 | "output": output, |
||
41 | "comparison": { |
||
42 | "success": success, |
||
43 | "expected": expected, |
||
44 | "found": found |
||
45 | } |
||
46 | } = body; |
||
47 | if (!success && expected !== undefined && found !== undefined) { |
||
48 | let error = new CodingameError(`Expected <${body.comparison.expected}> but found <${body.comparison.found}>`); |
||
49 | return Promise.reject(error); |
||
50 | } else { |
||
51 | throw `Success value should be false when expected and found properties are provided`; |
||
52 | } |
||
53 | } catch (error) { |
||
54 | let message = `The body is not of response type '${name}'\n`; |
||
55 | message += error.message; |
||
56 | throw new Error(message); |
||
57 | } |
||
58 | }; |
||
59 | |||
60 | export default { |
||
61 | "name": name, |
||
62 | "parse": parse |
||
63 | }; |
||
64 |
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.