2017-08-26 14:49:14 +00:00
|
|
|
/* global CodeMirror messageBox */
|
2017-12-02 20:41:15 +00:00
|
|
|
/* global editors makeSectionVisible showCodeMirrorPopup showHelp */
|
2017-09-12 15:19:16 +00:00
|
|
|
/* global loadScript require CSSLint stylelint */
|
2017-08-16 21:01:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-13 08:53:21 +00:00
|
|
|
onDOMready().then(loadLinterAssets);
|
2017-08-29 14:12:39 +00:00
|
|
|
|
2017-08-28 05:22:19 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var linterConfig = {
|
|
|
|
csslint: {},
|
|
|
|
stylelint: {},
|
|
|
|
defaults: {
|
|
|
|
// set in lint-defaults-csslint.js
|
|
|
|
csslint: {},
|
|
|
|
// set in lint-defaults-stylelint.js
|
|
|
|
stylelint: {},
|
|
|
|
},
|
|
|
|
storageName: {
|
|
|
|
csslint: 'editorCSSLintConfig',
|
|
|
|
stylelint: 'editorStylelintConfig',
|
|
|
|
},
|
2017-11-28 17:03:50 +00:00
|
|
|
worker: {
|
|
|
|
csslint: {path: '/vendor-overwrites/csslint/csslint-worker.js'},
|
|
|
|
stylelint: {path: '/vendor-overwrites/stylelint/stylelint-bundle.min.js'},
|
|
|
|
},
|
|
|
|
allRuleIds: {
|
|
|
|
csslint: null,
|
|
|
|
stylelint: null,
|
|
|
|
},
|
2017-08-28 05:22:19 +00:00
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
getName() {
|
2017-10-15 19:58:02 +00:00
|
|
|
// some dirty hacks to override editor.linter getting from prefs
|
|
|
|
const linter = prefs.get('editor.linter');
|
2017-11-30 17:42:52 +00:00
|
|
|
const mode = linter && editors[0] && editors[0].doc.mode;
|
|
|
|
return mode && mode !== 'css' && mode.name !== 'css' ? 'stylelint' : linter;
|
2017-10-15 19:58:02 +00:00
|
|
|
},
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
getCurrent(linter = linterConfig.getName()) {
|
2017-08-28 05:22:19 +00:00
|
|
|
return this.fallbackToDefaults(this[linter] || {});
|
|
|
|
},
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
getForCodeMirror(linter = linterConfig.getName()) {
|
2017-08-28 05:22:19 +00:00
|
|
|
return CodeMirror.lint && CodeMirror.lint[linter] ? {
|
|
|
|
getAnnotations: CodeMirror.lint[linter],
|
|
|
|
delay: prefs.get('editor.lintDelay'),
|
2017-12-01 00:49:09 +00:00
|
|
|
preUpdateLinting(cm) {
|
|
|
|
cm.startOperation();
|
|
|
|
},
|
2017-11-27 14:02:23 +00:00
|
|
|
onUpdateLinting(annotationsNotSorted, annotations, cm) {
|
2017-12-01 00:49:09 +00:00
|
|
|
cm.endOperation();
|
2017-11-27 14:02:23 +00:00
|
|
|
updateLintReport(cm, 0);
|
|
|
|
},
|
2017-08-28 05:22:19 +00:00
|
|
|
} : false;
|
|
|
|
},
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
fallbackToDefaults(config, linter = linterConfig.getName()) {
|
2017-08-28 05:22:19 +00:00
|
|
|
if (config && Object.keys(config).length) {
|
|
|
|
if (linter === 'stylelint') {
|
|
|
|
// always use default syntax because we don't expose it in config UI
|
|
|
|
config.syntax = this.defaults.stylelint.syntax;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
} else {
|
|
|
|
return deepCopy(this.defaults[linter] || {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
setLinter(linter = linterConfig.getName()) {
|
2017-08-28 05:22:19 +00:00
|
|
|
linter = linter.toLowerCase();
|
|
|
|
linter = linter === 'csslint' || linter === 'stylelint' ? linter : '';
|
2017-11-28 17:03:50 +00:00
|
|
|
if (linterConfig.getName() !== linter) {
|
2017-08-28 05:22:19 +00:00
|
|
|
prefs.set('editor.linter', linter);
|
|
|
|
}
|
|
|
|
return linter;
|
|
|
|
},
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
invokeWorker(message) {
|
|
|
|
const worker = linterConfig.worker[message.linter || linterConfig.getName()];
|
|
|
|
if (!worker.queue) {
|
|
|
|
worker.queue = [];
|
|
|
|
worker.instance.onmessage = ({data}) => {
|
|
|
|
worker.queue.shift().resolve(data);
|
|
|
|
if (worker.queue.length) {
|
|
|
|
worker.instance.postMessage(worker.queue[0].message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-11-27 14:02:23 +00:00
|
|
|
return new Promise(resolve => {
|
2017-11-28 17:03:50 +00:00
|
|
|
worker.queue.push({message, resolve});
|
|
|
|
if (worker.queue.length === 1) {
|
|
|
|
worker.instance.postMessage(message);
|
2017-11-27 14:02:23 +00:00
|
|
|
}
|
2017-11-28 17:03:50 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllRuleIds(linter = linterConfig.getName()) {
|
|
|
|
return Promise.resolve(
|
|
|
|
this.allRuleIds[linter] ||
|
|
|
|
this.invokeWorker({linter, action: 'getAllRuleIds'})
|
|
|
|
.then(ids => (this.allRuleIds[linter] = ids.sort()))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
findInvalidRules(config, linter = linterConfig.getName()) {
|
|
|
|
return this.getAllRuleIds(linter).then(allRuleIds => {
|
|
|
|
const allRuleIdsSet = new Set(allRuleIds);
|
|
|
|
const rules = linter === 'stylelint' ? config.rules : config;
|
|
|
|
return Object.keys(rules).filter(rule => !allRuleIdsSet.has(rule));
|
2017-11-27 14:02:23 +00:00
|
|
|
});
|
2017-08-28 05:22:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stringify(config = this.getCurrent()) {
|
2017-11-28 17:03:50 +00:00
|
|
|
if (linterConfig.getName() === 'stylelint') {
|
2017-08-28 05:22:19 +00:00
|
|
|
config.syntax = undefined;
|
|
|
|
}
|
|
|
|
return JSON.stringify(config, null, 2)
|
|
|
|
.replace(/,\n\s+\{\n\s+("severity":\s"\w+")\n\s+\}/g, ', {$1}');
|
|
|
|
},
|
|
|
|
|
|
|
|
save(config) {
|
|
|
|
config = this.fallbackToDefaults(config);
|
2017-11-28 17:03:50 +00:00
|
|
|
const linter = linterConfig.getName();
|
2017-08-28 05:22:19 +00:00
|
|
|
this[linter] = config;
|
|
|
|
BG.chromeSync.setLZValue(this.storageName[linter], config);
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
|
|
|
|
loadAll() {
|
|
|
|
return BG.chromeSync.getLZValues([
|
|
|
|
'editorCSSLintConfig',
|
|
|
|
'editorStylelintConfig',
|
|
|
|
]).then(data => {
|
|
|
|
this.csslint = this.fallbackToDefaults(data.editorCSSLintConfig, 'csslint');
|
|
|
|
this.stylelint = this.fallbackToDefaults(data.editorStylelintConfig, 'stylelint');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
watchStorage() {
|
|
|
|
chrome.storage.onChanged.addListener((changes, area) => {
|
|
|
|
if (area === 'sync') {
|
|
|
|
for (const name of ['editorCSSLintConfig', 'editorStylelintConfig']) {
|
|
|
|
if (name in changes && changes[name].newValue !== changes[name].oldValue) {
|
2017-08-29 14:59:45 +00:00
|
|
|
this.loadAll().then(updateLinter);
|
2017-08-28 05:22:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// this is an event listener so it can't refer to self via 'this'
|
|
|
|
openOnClick() {
|
|
|
|
setupLinterPopup(linterConfig.stringify());
|
|
|
|
},
|
|
|
|
|
|
|
|
showSavedMessage() {
|
|
|
|
$('#help-popup .saved-message').classList.add('show');
|
|
|
|
clearTimeout($('#help-popup .contents').timer);
|
|
|
|
$('#help-popup .contents').timer = setTimeout(() => {
|
|
|
|
// popup may be closed at this point
|
|
|
|
const msg = $('#help-popup .saved-message');
|
|
|
|
if (msg) {
|
|
|
|
msg.classList.remove('show');
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
},
|
2017-10-14 21:19:22 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
if (!linterConfig.init.pending) {
|
|
|
|
linterConfig.init.pending = linterConfig.loadAll();
|
|
|
|
}
|
|
|
|
return linterConfig.init.pending;
|
|
|
|
}
|
2017-08-28 05:22:19 +00:00
|
|
|
};
|
|
|
|
|
2017-08-17 19:08:48 +00:00
|
|
|
function initLint() {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('#lint-help').addEventListener('click', showLintHelp);
|
|
|
|
$('#lint').addEventListener('click', gotoLintIssue);
|
2017-08-28 05:22:19 +00:00
|
|
|
$('#linter-settings').addEventListener('click', linterConfig.openOnClick);
|
2017-08-16 21:01:45 +00:00
|
|
|
|
2017-10-14 21:19:22 +00:00
|
|
|
updateLinter();
|
2017-08-28 05:22:19 +00:00
|
|
|
linterConfig.watchStorage();
|
2017-09-03 17:06:20 +00:00
|
|
|
prefs.subscribe(['editor.linter'], updateLinter);
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function updateLinter({immediately, linter = linterConfig.getName()} = {}) {
|
2017-08-29 14:59:45 +00:00
|
|
|
if (!immediately) {
|
2017-10-07 10:00:25 +00:00
|
|
|
debounce(updateLinter, 0, {immediately: true, linter});
|
2017-08-29 14:59:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-29 14:12:39 +00:00
|
|
|
const GUTTERS_CLASS = 'CodeMirror-lint-markers';
|
|
|
|
|
2017-11-08 23:26:11 +00:00
|
|
|
Promise.all([
|
|
|
|
linterConfig.init(),
|
|
|
|
loadLinterAssets(linter)
|
|
|
|
]).then(updateEditors);
|
2017-09-03 22:07:07 +00:00
|
|
|
$('#linter-settings').style.display = !linter ? 'none' : 'inline-block';
|
2017-11-23 05:18:55 +00:00
|
|
|
$('#lint').classList.add('hidden');
|
2017-09-03 22:07:07 +00:00
|
|
|
|
2017-08-20 14:06:17 +00:00
|
|
|
function updateEditors() {
|
2017-08-29 14:12:39 +00:00
|
|
|
CodeMirror.defaults.lint = linterConfig.getForCodeMirror(linter);
|
|
|
|
const guttersOption = prepareGuttersOption();
|
2017-09-16 00:44:56 +00:00
|
|
|
editors.forEach(cm => {
|
2017-12-01 00:49:09 +00:00
|
|
|
if (cm.options.lint !== CodeMirror.defaults.lint) {
|
|
|
|
cm.setOption('lint', CodeMirror.defaults.lint);
|
|
|
|
}
|
2017-08-29 14:12:39 +00:00
|
|
|
if (guttersOption) {
|
|
|
|
cm.setOption('guttersOption', guttersOption);
|
|
|
|
updateGutters(cm, guttersOption);
|
2017-08-31 19:25:05 +00:00
|
|
|
cm.refresh();
|
2017-08-29 14:12:39 +00:00
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
setTimeout(updateLintReport, 0, cm);
|
2017-08-20 14:06:17 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-29 14:12:39 +00:00
|
|
|
|
|
|
|
function prepareGuttersOption() {
|
|
|
|
const gutters = CodeMirror.defaults.gutters;
|
|
|
|
const needRefresh = Boolean(linter) !== gutters.includes(GUTTERS_CLASS);
|
|
|
|
if (needRefresh) {
|
|
|
|
if (linter) {
|
|
|
|
gutters.push(GUTTERS_CLASS);
|
|
|
|
} else {
|
|
|
|
gutters.splice(gutters.indexOf(GUTTERS_CLASS), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return needRefresh && gutters;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateGutters(cm, guttersOption) {
|
|
|
|
cm.options.gutters = guttersOption;
|
|
|
|
const el = $('.' + GUTTERS_CLASS, cm.display.gutters);
|
|
|
|
if (linter && !el) {
|
2017-12-03 21:12:09 +00:00
|
|
|
cm.display.gutters.appendChild($create('.CodeMirror-gutter ' + GUTTERS_CLASS));
|
2017-08-29 14:12:39 +00:00
|
|
|
} else if (!linter && el) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 21:01:45 +00:00
|
|
|
function updateLintReport(cm, delay) {
|
2017-08-29 20:36:56 +00:00
|
|
|
const state = cm && cm.state && cm.state.lint || {};
|
2017-08-16 21:01:45 +00:00
|
|
|
if (delay === 0) {
|
|
|
|
// immediately show pending csslint/stylelint messages in onbeforeunload and save
|
2017-08-29 20:36:56 +00:00
|
|
|
clearTimeout(state.lintTimeout);
|
2017-08-31 19:25:05 +00:00
|
|
|
updateLintReportInternal(cm);
|
2017-08-16 21:01:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (delay > 0) {
|
2017-08-29 20:36:56 +00:00
|
|
|
clearTimeout(state.lintTimeout);
|
|
|
|
state.lintTimeout = setTimeout(cm => {
|
2017-08-29 14:12:39 +00:00
|
|
|
if (cm.performLint) {
|
|
|
|
cm.performLint();
|
2017-08-31 19:25:05 +00:00
|
|
|
updateLintReportInternal(cm);
|
2017-08-29 14:12:39 +00:00
|
|
|
}
|
2017-08-20 19:10:58 +00:00
|
|
|
}, delay, cm);
|
2017-08-16 21:01:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
if (state.options) {
|
|
|
|
clearTimeout(state.reportTimeout);
|
|
|
|
const delay = cm && cm.state.renderLintReportNow ? 0 : state.options.delay + 100;
|
|
|
|
state.reportTimeout = setTimeout(updateLintReportInternal, delay, cm, {
|
|
|
|
postponeNewIssues: delay === undefined || delay === null
|
|
|
|
});
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
}
|
2017-08-16 21:01:45 +00:00
|
|
|
|
2017-08-31 19:25:05 +00:00
|
|
|
function updateLintReportInternal(scope, {postponeNewIssues} = {}) {
|
2017-09-03 16:36:33 +00:00
|
|
|
const {changed, fixedSome} = (scope ? [scope] : editors).reduce(process, {});
|
|
|
|
if (changed) {
|
|
|
|
const renderNow = editors.last.state.renderLintReportNow =
|
|
|
|
!postponeNewIssues || fixedSome || editors.last.state.renderLintReportNow;
|
|
|
|
debounce(renderLintReport, renderNow ? 0 : CodeMirror.defaults.lintReportDelay, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function process(result, cm) {
|
2017-08-31 19:25:05 +00:00
|
|
|
const lintState = cm.state.lint || {};
|
2017-09-03 16:36:33 +00:00
|
|
|
const oldMarkers = lintState.stylusMarkers || new Map();
|
|
|
|
const newMarkers = lintState.stylusMarkers = new Map();
|
|
|
|
const oldText = (lintState.body || {}).textContentCached || '';
|
2017-08-31 19:25:05 +00:00
|
|
|
const activeLine = cm.getCursor().line;
|
2017-12-03 21:12:09 +00:00
|
|
|
const body = !(lintState.marked || {}).length ? {} :
|
|
|
|
$create('tbody', lintState.marked.map(mark => {
|
2017-09-03 16:36:33 +00:00
|
|
|
const info = mark.__annotation;
|
|
|
|
const {line, ch} = info.from;
|
|
|
|
const isActiveLine = line === activeLine;
|
|
|
|
const pos = isActiveLine ? 'cursor' : (line + ',' + ch);
|
|
|
|
const title = clipString(info.message, 1000) + `\n(${info.rule})`;
|
|
|
|
const message = clipString(info.message, 100);
|
|
|
|
if (isActiveLine || oldMarkers[pos] === message) {
|
|
|
|
oldMarkers.delete(pos);
|
|
|
|
}
|
|
|
|
newMarkers.set(pos, message);
|
2017-12-03 21:12:09 +00:00
|
|
|
return $create(`tr.${info.severity}`, [
|
|
|
|
$create('td', {attributes: {role: 'severity'}, dataset: {rule: info.rule}},
|
|
|
|
$create('.CodeMirror-lint-marker-' + info.severity, info.severity)),
|
|
|
|
$create('td', {attributes: {role: 'line'}}, line + 1),
|
|
|
|
$create('td', {attributes: {role: 'sep'}}, ':'),
|
|
|
|
$create('td', {attributes: {role: 'col'}}, ch + 1),
|
|
|
|
$create('td', {attributes: {role: 'message'}, title}, message),
|
|
|
|
]);
|
|
|
|
}));
|
2017-09-03 16:36:33 +00:00
|
|
|
body.textContentCached = body.textContent || '';
|
|
|
|
lintState.body = body.textContentCached && body;
|
|
|
|
result.changed |= oldText !== body.textContentCached;
|
|
|
|
result.fixedSome |= lintState.reportDisplayed && oldMarkers.size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clipString(str, limit) {
|
|
|
|
return str.length <= limit ? str : str.substr(0, limit) + '...';
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderLintReport(someBlockChanged) {
|
2017-08-20 18:32:41 +00:00
|
|
|
const container = $('#lint');
|
2017-08-16 21:01:45 +00:00
|
|
|
const content = container.children[1];
|
|
|
|
const label = t('sectionCode');
|
|
|
|
const newContent = content.cloneNode(false);
|
|
|
|
let issueCount = 0;
|
|
|
|
editors.forEach((cm, index) => {
|
2017-09-03 16:36:33 +00:00
|
|
|
cm.state.renderLintReportNow = false;
|
2017-08-31 19:25:05 +00:00
|
|
|
const lintState = cm.state.lint || {};
|
|
|
|
const body = lintState.body;
|
|
|
|
if (!body) {
|
|
|
|
return;
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
const newBlock = $create('table', {cm}, [
|
|
|
|
$create('caption', label + ' ' + (index + 1)),
|
|
|
|
body,
|
|
|
|
]);
|
2017-08-31 19:25:05 +00:00
|
|
|
newContent.appendChild(newBlock);
|
|
|
|
issueCount += newBlock.rows.length;
|
|
|
|
|
|
|
|
const block = content.children[newContent.children.length - 1];
|
|
|
|
const blockChanged =
|
|
|
|
!block ||
|
|
|
|
block.cm !== cm ||
|
|
|
|
body.textContentCached !== block.textContentCached;
|
|
|
|
someBlockChanged |= blockChanged;
|
|
|
|
lintState.reportDisplayed = blockChanged;
|
2017-08-16 21:01:45 +00:00
|
|
|
});
|
|
|
|
if (someBlockChanged || newContent.children.length !== content.children.length) {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('#issue-count').textContent = issueCount;
|
2017-08-16 21:01:45 +00:00
|
|
|
container.replaceChild(newContent, content);
|
2017-11-23 05:18:55 +00:00
|
|
|
container.classList.toggle('hidden', !newContent.children.length);
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gotoLintIssue(event) {
|
|
|
|
const issue = event.target.closest('tr');
|
|
|
|
if (!issue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const block = issue.closest('table');
|
|
|
|
makeSectionVisible(block.cm);
|
|
|
|
block.cm.focus();
|
|
|
|
block.cm.setSelection({
|
2017-08-20 18:32:41 +00:00
|
|
|
line: parseInt($('td[role="line"]', issue).textContent) - 1,
|
|
|
|
ch: parseInt($('td[role="col"]', issue).textContent) - 1
|
2017-08-16 21:01:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showLintHelp() {
|
2017-11-28 17:03:50 +00:00
|
|
|
const linter = linterConfig.getName();
|
2017-08-28 13:33:45 +00:00
|
|
|
const baseUrl = linter === 'stylelint'
|
2017-08-23 22:13:55 +00:00
|
|
|
? 'https://stylelint.io/user-guide/rules/'
|
|
|
|
// some CSSLint rules do not have a url
|
|
|
|
: 'https://github.com/CSSLint/csslint/issues/535';
|
2017-12-04 05:32:27 +00:00
|
|
|
let headerLink, template, csslintRules;
|
2017-08-23 22:13:55 +00:00
|
|
|
if (linter === 'csslint') {
|
2017-12-03 21:12:09 +00:00
|
|
|
headerLink = $createLink('https://github.com/CSSLint/csslint/wiki/Rules-by-ID', 'CSSLint');
|
2017-08-23 22:13:55 +00:00
|
|
|
template = ruleID => {
|
2017-12-04 05:32:27 +00:00
|
|
|
const rule = csslintRules.find(rule => rule.id === ruleID);
|
2017-08-28 13:33:45 +00:00
|
|
|
return rule &&
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('li', [
|
|
|
|
$create('b', $createLink(rule.url || baseUrl, rule.name)),
|
|
|
|
$create('br'),
|
2017-08-28 13:33:45 +00:00
|
|
|
rule.desc,
|
2017-12-03 21:12:09 +00:00
|
|
|
]);
|
2017-08-23 22:13:55 +00:00
|
|
|
};
|
2017-08-16 21:01:45 +00:00
|
|
|
} else {
|
2017-12-03 21:12:09 +00:00
|
|
|
headerLink = $createLink(baseUrl, 'stylelint');
|
2017-08-28 13:33:45 +00:00
|
|
|
template = rule =>
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('li',
|
2017-12-04 05:32:27 +00:00
|
|
|
rule === 'CssSyntaxError' ? rule : $createLink(baseUrl + rule, rule));
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-28 13:33:45 +00:00
|
|
|
const header = t('linterIssuesHelp', '\x01').split('\x01');
|
|
|
|
const activeRules = new Set($$('#lint td[role="severity"]').map(el => el.dataset.rule));
|
2017-12-04 05:32:27 +00:00
|
|
|
Promise.resolve(linter !== 'csslint' || linterConfig.invokeWorker({action: 'getAllRuleInfos'}))
|
|
|
|
.then(data => {
|
|
|
|
csslintRules = data;
|
|
|
|
showHelp(t('linterIssues'),
|
|
|
|
$create([
|
|
|
|
header[0], headerLink, header[1],
|
|
|
|
$create('ul.rules', [...activeRules.values()].map(template)),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-17 19:08:48 +00:00
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function showLinterErrorMessage(title, contents, popup) {
|
2017-08-25 20:49:20 +00:00
|
|
|
messageBox({
|
|
|
|
title,
|
|
|
|
contents,
|
|
|
|
className: 'danger center lint-config',
|
|
|
|
buttons: [t('confirmOK')],
|
2017-11-28 19:45:11 +00:00
|
|
|
}).then(() => popup && popup.codebox && popup.codebox.focus());
|
2017-08-25 20:49:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function setupLinterPopup(config) {
|
|
|
|
const linter = linterConfig.getName();
|
|
|
|
const linterTitle = linter === 'stylelint' ? 'Stylelint' : 'CSSLint';
|
|
|
|
const defaultConfig = linterConfig.stringify(linterConfig.defaults[linter] || {});
|
|
|
|
const title = t('linterConfigPopupTitle', linterTitle);
|
|
|
|
const popup = showCodeMirrorPopup(title, null, {
|
|
|
|
lint: false,
|
|
|
|
extraKeys: {'Ctrl-Enter': save},
|
|
|
|
hintOptions: {hint},
|
|
|
|
});
|
|
|
|
$('.contents', popup).appendChild(makeFooter());
|
|
|
|
|
|
|
|
const cm = popup.codebox;
|
|
|
|
cm.focus();
|
|
|
|
cm.setValue(config);
|
|
|
|
cm.clearHistory();
|
|
|
|
cm.markClean();
|
|
|
|
cm.on('changes', updateButtonState);
|
|
|
|
updateButtonState();
|
|
|
|
|
2017-12-02 20:41:15 +00:00
|
|
|
cm.rerouteHotkeys(false);
|
2017-11-28 17:03:50 +00:00
|
|
|
window.addEventListener('closeHelp', function _() {
|
|
|
|
window.removeEventListener('closeHelp', _);
|
2017-12-02 20:41:15 +00:00
|
|
|
cm.rerouteHotkeys(true);
|
2017-11-28 17:03:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
loadScript([
|
|
|
|
'/vendor/codemirror/mode/javascript/javascript.js',
|
|
|
|
'/vendor/codemirror/addon/lint/json-lint.js',
|
|
|
|
'/vendor/jsonlint/jsonlint.js'
|
|
|
|
]).then(() => {
|
|
|
|
cm.setOption('mode', 'application/json');
|
|
|
|
cm.setOption('lint', 'json');
|
|
|
|
});
|
|
|
|
|
|
|
|
function makeFooter() {
|
2017-12-03 21:12:09 +00:00
|
|
|
return $create('div', [
|
|
|
|
$create('p', [
|
|
|
|
t('linterRulesLink') + ' ',
|
|
|
|
$createLink(
|
|
|
|
linter === 'stylelint'
|
|
|
|
? 'https://stylelint.io/user-guide/rules/'
|
|
|
|
: 'https://github.com/CSSLint/csslint/wiki/Rules-by-ID',
|
|
|
|
linterTitle),
|
|
|
|
linter === 'csslint' ? ' ' + t('linterCSSLintSettings') : '',
|
|
|
|
]),
|
|
|
|
$create('button.save', {onclick: save, title: 'Ctrl-Enter'}, t('styleSaveLabel')),
|
|
|
|
$create('button.cancel', {onclick: cancel}, t('confirmClose')),
|
|
|
|
$create('button.reset', {onclick: reset, title: t('linterResetMessage')}, t('genericResetLabel')),
|
|
|
|
$create('span.saved-message', t('genericSavedMessage')),
|
|
|
|
]);
|
2017-11-28 17:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function save(event) {
|
|
|
|
if (event instanceof Event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
const json = tryJSONparse(cm.getValue());
|
|
|
|
if (!json) {
|
|
|
|
showLinterErrorMessage(linter, t('linterJSONError'), popup);
|
|
|
|
cm.focus();
|
2017-11-28 19:45:11 +00:00
|
|
|
return;
|
2017-11-27 14:02:23 +00:00
|
|
|
}
|
|
|
|
linterConfig.findInvalidRules(json, linter).then(invalid => {
|
2017-08-25 20:49:20 +00:00
|
|
|
if (invalid.length) {
|
2017-08-28 05:22:19 +00:00
|
|
|
showLinterErrorMessage(linter, [
|
|
|
|
t('linterInvalidConfigError'),
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('ul', invalid.map(name => $create('li', name))),
|
2017-11-28 17:03:50 +00:00
|
|
|
], popup);
|
2017-08-28 05:22:19 +00:00
|
|
|
return;
|
2017-08-25 20:49:20 +00:00
|
|
|
}
|
2017-11-28 17:03:50 +00:00
|
|
|
linterConfig.setLinter(linter);
|
2017-08-28 05:22:19 +00:00
|
|
|
linterConfig.save(json);
|
|
|
|
linterConfig.showSavedMessage();
|
2017-11-28 17:03:50 +00:00
|
|
|
cm.markClean();
|
|
|
|
cm.focus();
|
|
|
|
updateButtonState();
|
2017-11-27 14:02:23 +00:00
|
|
|
});
|
2017-11-28 17:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset(event) {
|
2017-08-18 15:46:09 +00:00
|
|
|
event.preventDefault();
|
2017-11-28 19:45:11 +00:00
|
|
|
linterConfig.setLinter(linter);
|
2017-11-28 17:03:50 +00:00
|
|
|
cm.setValue(defaultConfig);
|
|
|
|
cm.focus();
|
|
|
|
updateButtonState();
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancel(event) {
|
2017-08-26 15:03:28 +00:00
|
|
|
event.preventDefault();
|
|
|
|
$('.dismiss').dispatchEvent(new Event('click'));
|
2017-08-20 01:49:13 +00:00
|
|
|
}
|
2017-08-29 15:21:57 +00:00
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function updateButtonState() {
|
2017-08-29 15:21:57 +00:00
|
|
|
$('.save', popup).disabled = cm.isClean();
|
2017-11-28 17:03:50 +00:00
|
|
|
$('.reset', popup).disabled = cm.getValue() === defaultConfig;
|
|
|
|
$('.cancel', popup).textContent = t(cm.isClean() ? 'confirmClose' : 'confirmCancel');
|
|
|
|
}
|
2017-08-20 14:06:17 +00:00
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function hint(cm) {
|
|
|
|
return Promise.all([
|
|
|
|
linterConfig.getAllRuleIds(linter),
|
|
|
|
linter !== 'stylelint' || hint.allOptions ||
|
|
|
|
linterConfig.invokeWorker({action: 'getAllRuleOptions', linter})
|
|
|
|
.then(options => (hint.allOptions = options)),
|
|
|
|
])
|
|
|
|
.then(([ruleIds, options]) => {
|
|
|
|
const cursor = cm.getCursor();
|
|
|
|
const {start, end, string, type, state: {lexical}} = cm.getTokenAt(cursor);
|
|
|
|
const {line, ch} = cursor;
|
|
|
|
|
|
|
|
const quoted = string.startsWith('"');
|
|
|
|
const leftPart = string.slice(quoted ? 1 : 0, ch - start).trim();
|
|
|
|
const depth = getLexicalDepth(lexical);
|
|
|
|
|
|
|
|
const search = cm.getSearchCursor(/"([-\w]+)"/, {line, ch: start - 1});
|
|
|
|
let [, prevWord] = search.find(true) || [];
|
|
|
|
let words = [];
|
|
|
|
|
|
|
|
if (depth === 1 && linter === 'stylelint') {
|
|
|
|
words = quoted ? ['rules'] : [];
|
|
|
|
} else if ((depth === 1 || depth === 2) && type && type.includes('property')) {
|
|
|
|
words = ruleIds;
|
|
|
|
} else if (depth === 2 || depth === 3 && lexical.type === ']') {
|
|
|
|
words = !quoted ? ['true', 'false', 'null'] :
|
|
|
|
ruleIds.includes(prevWord) && (options[prevWord] || [])[0] || [];
|
|
|
|
} else if (depth === 4 && prevWord === 'severity') {
|
|
|
|
words = ['error', 'warning'];
|
|
|
|
} else if (depth === 4) {
|
|
|
|
words = ['ignore', 'ignoreAtRules', 'except', 'severity'];
|
|
|
|
} else if (depth === 5 && lexical.type === ']' && quoted) {
|
|
|
|
while (prevWord && !ruleIds.includes(prevWord)) {
|
|
|
|
prevWord = (search.find(true) || [])[1];
|
|
|
|
}
|
|
|
|
words = (options[prevWord] || []).slice(-1)[0] || ruleIds;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
list: words.filter(word => word.startsWith(leftPart)),
|
|
|
|
from: {line, ch: start + (quoted ? 1 : 0)},
|
|
|
|
to: {line, ch: string.endsWith('"') ? end - 1 : end},
|
|
|
|
};
|
|
|
|
});
|
2017-09-13 13:26:22 +00:00
|
|
|
}
|
2017-11-28 17:03:50 +00:00
|
|
|
|
|
|
|
function getLexicalDepth(lexicalState) {
|
|
|
|
let depth = 0;
|
|
|
|
while ((lexicalState = lexicalState.prev)) {
|
|
|
|
depth++;
|
2017-09-13 13:26:22 +00:00
|
|
|
}
|
2017-11-28 17:03:50 +00:00
|
|
|
return depth;
|
2017-08-28 05:22:19 +00:00
|
|
|
}
|
2017-11-28 17:03:50 +00:00
|
|
|
}
|
2017-09-13 13:26:22 +00:00
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function loadLinterAssets(name = linterConfig.getName()) {
|
|
|
|
const worker = linterConfig.worker[name];
|
|
|
|
return !name || !worker || worker.instance ? Promise.resolve() :
|
|
|
|
loadScript((worker.instance ? [] : [
|
|
|
|
(worker.instance = new Worker(worker.path)),
|
|
|
|
`/edit/lint-defaults-${name}.js`,
|
|
|
|
]).concat(CodeMirror.lint ? [] : [
|
2017-09-12 15:19:16 +00:00
|
|
|
'/vendor/codemirror/addon/lint/lint.css',
|
|
|
|
'/msgbox/msgbox.css',
|
|
|
|
'/vendor/codemirror/addon/lint/lint.js',
|
|
|
|
'/edit/lint-codemirror-helper.js',
|
|
|
|
'/msgbox/msgbox.js'
|
2017-11-28 17:03:50 +00:00
|
|
|
]));
|
2017-08-20 14:06:17 +00:00
|
|
|
}
|