|
1 | | -var Issue = require('./issues').Issue |
2 | | -var JSHINT = require('jshint').JSHINT |
| 1 | +const Issue = require('./issues').Issue |
| 2 | +const { JSHINT } = require('jshint') |
3 | 3 |
|
4 | | -module.exports = { |
5 | | - /** |
6 | | - * Parse |
7 | | - * |
8 | | - * Similar to native JSON.parse but uses |
9 | | - * a callback structure, jshint for more |
10 | | - * thorough error reporting and error formatting |
11 | | - * of the rest of the validator. |
12 | | - */ |
13 | | - parse: function(file, contents, callback) { |
14 | | - var jsObj = null |
15 | | - var err = null |
| 4 | +/** |
| 5 | + * Similar to native JSON.parse but returns a promise and |
| 6 | + * runs jshint for more thorough error reporting |
| 7 | + */ |
| 8 | +function parse(file, contents) { |
| 9 | + return new Promise(resolve => { |
| 10 | + let jsObj |
| 11 | + let err |
16 | 12 | try { |
17 | 13 | jsObj = JSON.parse(contents) |
18 | 14 | } catch (exception) { |
19 | 15 | err = exception |
20 | 16 | } finally { |
21 | 17 | if (err) { |
22 | | - this.jshint(file, contents, function(issues) { |
23 | | - callback(issues, null) |
| 18 | + jshint(file, contents, function(issues) { |
| 19 | + resolve({ issues, parsed: null }) |
24 | 20 | }) |
25 | 21 | } else { |
26 | | - callback([], jsObj) |
| 22 | + resolve({ issues: [], parsed: jsObj }) |
27 | 23 | } |
28 | 24 | } |
29 | | - }, |
| 25 | + }) |
| 26 | +} |
30 | 27 |
|
31 | | - /** |
32 | | - * JSHint |
33 | | - * |
34 | | - * Checks known invalid JSON file |
35 | | - * content in order to produce a |
36 | | - * verbose error message. |
37 | | - */ |
38 | | - jshint: function(file, contents, callback) { |
39 | | - var issues = [] |
40 | | - if (!JSHINT(contents)) { |
41 | | - var out = JSHINT.data() |
42 | | - for (var i = 0; out.errors.length > i; ++i) { |
43 | | - var error = out.errors[i] |
44 | | - if (error) { |
45 | | - issues.push( |
46 | | - new Issue({ |
47 | | - code: 27, |
48 | | - file: file, |
49 | | - line: error.line ? error.line : null, |
50 | | - character: error.character ? error.character : null, |
51 | | - reason: error.reason ? error.reason : null, |
52 | | - evidence: error.evidence ? error.evidence : null, |
53 | | - }), |
54 | | - ) |
55 | | - } |
| 28 | +/** |
| 29 | + * JSHint |
| 30 | + * |
| 31 | + * Checks known invalid JSON file |
| 32 | + * content in order to produce a |
| 33 | + * verbose error message. |
| 34 | + */ |
| 35 | +function jshint(file, contents, callback) { |
| 36 | + var issues = [] |
| 37 | + if (!JSHINT(contents)) { |
| 38 | + var out = JSHINT.data() |
| 39 | + for (var i = 0; out.errors.length > i; ++i) { |
| 40 | + var error = out.errors[i] |
| 41 | + if (error) { |
| 42 | + issues.push( |
| 43 | + new Issue({ |
| 44 | + code: 27, |
| 45 | + file: file, |
| 46 | + line: error.line ? error.line : null, |
| 47 | + character: error.character ? error.character : null, |
| 48 | + reason: error.reason ? error.reason : null, |
| 49 | + evidence: error.evidence ? error.evidence : null, |
| 50 | + }), |
| 51 | + ) |
56 | 52 | } |
57 | 53 | } |
58 | | - callback(issues) |
59 | | - }, |
| 54 | + } |
| 55 | + callback(issues) |
| 56 | +} |
| 57 | + |
| 58 | +module.exports = { |
| 59 | + parse, |
| 60 | + jshint, |
60 | 61 | } |
0 commit comments