-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
87 lines (72 loc) · 2.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
var jshint = require('jshint').JSHINT,
_ = require('lodash');
var xsxhint = function(transformer) {
function modifiedLines(a, b) {
return _(a).zip(b).reduce(function(modified, lines, i) {
if (lines[0] !== lines[1]) {
modified[i] = true;
}
return modified;
}, {});
}
var ignoreError = {
'W064': true,
'W101': function (line) {
var maxlen = jshint.data().options.maxlen;
return line.length <= maxlen;
},
'W102': true,
'W109': true
};
function jsxhint() {
var args = Array.prototype.slice.call(arguments),
code = args[0],
transformedCode,
success = false,
errors = [];
try {
args[0] = transformedCode = transformer(code);
} catch (e) {
// sometimes react-tools throws error without information about line
if (e.lineNumber === undefined) {
errors.push(null);
} else {
errors.push({
line: e.lineNumber,
character: e.column,
reason: e.description
});
}
}
if (_.isEmpty(errors)) {
success = jshint.apply(null, args);
errors = jshint.errors;
}
var originalLines = code.split('\n');
var transformedLines = transformedCode ? transformedCode.split('\n') : [];
var modified = transformedCode ? modifiedLines(originalLines, transformedLines) : {};
// there can be some nulls, but they cannot be displayed, so let's remove them.
errors = _.compact(errors);
jsxhint.errors = _.reject(errors, function(e) {
var ignore = ignoreError[e.code];
if (!modified[e.line - 1] || !ignore) {
return false;
}
return _.isFunction(ignore) ?
ignore(originalLines[e.line - 1], transformedLines[e.line - 1]) :
true;
});
jsxhint.data = jshint.data;
return success;
}
jsxhint.errors = [];
return jsxhint;
};
var jsxhint = xsxhint(require('react-tools').transform);
var msxhint = xsxhint(require('msx').transform);
module.exports = {
JSHINT: jshint,
JSXHINT: jsxhint,
MSXHINT: msxhint
};