2017-11-28 17:03:50 +00:00
|
|
|
/* global CodeMirror linterConfig */
|
2017-08-28 05:22:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-01 00:49:09 +00:00
|
|
|
(() => {
|
|
|
|
CodeMirror.registerHelper('lint', 'csslint', invokeHelper);
|
|
|
|
CodeMirror.registerHelper('lint', 'stylelint', invokeHelper);
|
2017-08-28 05:22:19 +00:00
|
|
|
|
2018-08-06 12:11:27 +00:00
|
|
|
const COOKS = {
|
2017-12-01 00:49:09 +00:00
|
|
|
csslint: results =>
|
|
|
|
results.map(({line, col: ch, message, rule, type: severity}) => line && {
|
|
|
|
message,
|
|
|
|
from: {line: line - 1, ch: ch - 1},
|
|
|
|
to: {line: line - 1, ch},
|
|
|
|
rule: rule.id,
|
|
|
|
severity,
|
|
|
|
}).filter(Boolean),
|
|
|
|
|
2018-08-06 12:10:42 +00:00
|
|
|
stylelint({results}, cm) {
|
|
|
|
if (!results[0]) return [];
|
|
|
|
const output = results[0].warnings.map(({line, column: ch, text, severity}) => ({
|
2017-12-01 00:49:09 +00:00
|
|
|
from: {line: line - 1, ch: ch - 1},
|
|
|
|
to: {line: line - 1, ch},
|
|
|
|
message: text
|
|
|
|
.replace('Unexpected ', '')
|
|
|
|
.replace(/^./, firstLetter => firstLetter.toUpperCase())
|
|
|
|
.replace(/\s*\([^(]+\)$/, ''), // strip the rule,
|
|
|
|
rule: text.replace(/^.*?\s*\(([^(]+)\)$/, '$1'),
|
|
|
|
severity,
|
2018-08-06 12:10:42 +00:00
|
|
|
}));
|
|
|
|
return cm.doc.mode.name !== 'stylus' ?
|
|
|
|
output :
|
|
|
|
output.filter(({message}) =>
|
|
|
|
!message.includes('"@css"') || !message.includes('(at-rule-no-unknown)'));
|
|
|
|
},
|
2017-12-01 00:49:09 +00:00
|
|
|
};
|
|
|
|
|
2018-08-06 12:10:42 +00:00
|
|
|
function invokeHelper(code, options, cm) {
|
2017-12-01 00:49:09 +00:00
|
|
|
const config = linterConfig.getCurrent();
|
2018-08-06 12:11:27 +00:00
|
|
|
const cook = COOKS[linterConfig.getName()];
|
2017-12-01 00:49:09 +00:00
|
|
|
return linterConfig.invokeWorker({code, config})
|
2018-08-06 12:10:42 +00:00
|
|
|
.then(data => cook(data, cm));
|
2017-12-01 00:49:09 +00:00
|
|
|
}
|
|
|
|
})();
|