Fix: force using stylelint when editor mode is not css

This commit is contained in:
eight 2017-10-07 18:00:25 +08:00
parent 56e669fbcf
commit fc51d806da
3 changed files with 38 additions and 6 deletions

View File

@ -32,6 +32,18 @@ var linterConfig = {
} : false; } : false;
}, },
getName(cmLintOption) {
if (!cmLintOption) {
return null;
}
for (const linter of ['csslint', 'stylelint']) {
if (cmLintOption.getAnnotations === CodeMirror.lint[linter]) {
return linter;
}
}
return null;
},
fallbackToDefaults(config, linter = prefs.get('editor.linter')) { fallbackToDefaults(config, linter = prefs.get('editor.linter')) {
if (config && Object.keys(config).length) { if (config && Object.keys(config).length) {
if (linter === 'stylelint') { if (linter === 'stylelint') {
@ -136,12 +148,11 @@ function initLint() {
prefs.subscribe(['editor.linter'], updateLinter); prefs.subscribe(['editor.linter'], updateLinter);
} }
function updateLinter({immediately} = {}) { function updateLinter({immediately, linter = prefs.get('editor.linter')} = {}) {
if (!immediately) { if (!immediately) {
debounce(updateLinter, 0, {immediately: true}); debounce(updateLinter, 0, {immediately: true, linter});
return; return;
} }
const linter = prefs.get('editor.linter');
const GUTTERS_CLASS = 'CodeMirror-lint-markers'; const GUTTERS_CLASS = 'CodeMirror-lint-markers';
loadLinterAssets(linter).then(updateEditors); loadLinterAssets(linter).then(updateEditors);

View File

@ -1,7 +1,7 @@
/* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */ /* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */ /* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
/* global hotkeyRerouter setupAutocomplete setupOptionsExpand */ /* global hotkeyRerouter setupAutocomplete setupOptionsExpand */
/* global editors */ /* global editors linterConfig updateLinter */
'use strict'; 'use strict';
@ -40,9 +40,30 @@ function createSourceEditor(style) {
// draw metas info // draw metas info
updateMetas(); updateMetas();
initHooks(); initHooks();
initLint();
initAppliesToReport(cm); initAppliesToReport(cm);
// setup linter
initLint();
const linterEl = $('#editor.linter');
cm.on('optionChange', (cm, option) => {
if (option === 'mode' || option === 'lint') {
const lint = cm.getOption('lint');
const mode = cm.getOption('mode');
if (mode !== 'css' && linterConfig.getName(lint) === 'csslint') {
updateLinter({linter: 'stylelint'});
linterEl.value = 'stylelint';
}
}
});
linterEl.addEventListener('change', () => {
if (cm.getOption('mode') !== 'css' && linterEl.value === 'csslint') {
setTimeout(() => {
linterEl.value = 'stylelint';
});
}
});
function initAppliesToReport(cm) { function initAppliesToReport(cm) {
const APPLIES_TYPE = [ const APPLIES_TYPE = [
[t('appliesUrlOption'), 'url'], [t('appliesUrlOption'), 'url'],

View File

@ -43,7 +43,7 @@ var prefs = new function Prefs() {
indent_conditional: true, indent_conditional: true,
}, },
'editor.lintDelay': 500, // lint gutter marker update delay, ms 'editor.lintDelay': 500, // lint gutter marker update delay, ms
'editor.linter': 'stylelint', // 'csslint' or 'stylelint' or '' 'editor.linter': 'csslint', // 'csslint' or 'stylelint' or ''
'editor.lintReportDelay': 4500, // lint report update delay, ms 'editor.lintReportDelay': 4500, // lint report update delay, ms
'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected 'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected
// selection = only when something is selected // selection = only when something is selected