-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheslint.js
80 lines (68 loc) · 1.9 KB
/
eslint.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
"use strict";
const sidekickAnalyser = require("@sidekick/analyser-common");
const fs = require('fs');
const path = require('path');
const stripJsonComments = require("strip-json-comments");
const eslint = require('eslint');
const debug = require('debug')('eslint');
const annotationDefaults = {analyserName: 'sidekick-eslint'};
const configFileName = '.eslintrc';
const LOG_FILE = path.join(__dirname, '/debug.log');
//log to file as any stdout will be reported to the analyser runner
function logger(message) {
fs.appendFile(LOG_FILE, message + '\n');
}
if(require.main === module) {
execute();
}
module.exports = exports = execute;
function execute() {
sidekickAnalyser(function(setup) {
var config;
logger('setup: ' + JSON.stringify(setup));
var conf = (setup.configFiles || {})[configFileName];
if(conf) {
try {
config = JSON.parse(stripJsonComments(conf));
logger('using config: ' + JSON.stringify(config));
} catch(e) {
logger('config parsing failed: ' + e.message);
// FIXME need some way of signalling
console.error("can't parse config");
console.error(e);
}
}
if(!config) {
config = {};
debug('using default config');
}
var results = run(setup.content, config);
console.log(JSON.stringify({ meta: results }));
});
}
function run(content, config) {
logger('run');
try {
var errors = eslint.linter.verify(content, config);
return errors.map(format);
} catch (err) {
console.error("failed to analyse");
console.log({ error: err });
process.exit(1);
}
}
function format(error) {
var line = error.line - 1;
var col = error.column - 1;
return {
analyser: annotationDefaults.analyserName,
location: {
startLine: line,
endLine: line,
startCharacter: col,
endCharacter: col,
},
message: error.message,
kind: error.ruleId,
};
}