Show alert with invalid JSON and rule names

This commit is contained in:
Rob Garrison 2017-08-25 15:49:20 -05:00
parent cbe6decb03
commit 0cd88873e1
3 changed files with 64 additions and 22 deletions

View File

@ -159,6 +159,10 @@
"message": "Reset", "message": "Reset",
"description": "Used in various parts of UI to indicate that something may be reset to its original state" "description": "Used in various parts of UI to indicate that something may be reset to its original state"
}, },
"genericSavedMessage": {
"message": "Saved",
"description": "Used in various parts of the UI to indicate that something was saved"
},
"confirmNo": { "confirmNo": {
"message": "No", "message": "No",
"description": "'No' button in a confirm dialog" "description": "'No' button in a confirm dialog"
@ -531,6 +535,10 @@
"message": "Invalid JSON format", "message": "Invalid JSON format",
"description": "Setting linter rules with invalid JSON message" "description": "Setting linter rules with invalid JSON message"
}, },
"setLinterInvalidRuleError": {
"message": "Not saved due to these invalid rules:",
"description": "Invalid linter rules will show a message followed by a list of invalid rules"
},
"showCSSLintSettings": { "showCSSLintSettings": {
"message": "(Set rules: 0 = disabled; 1 = warning; 2 = error)", "message": "(Set rules: 0 = disabled; 1 = warning; 2 = error)",
"description": "CSSLint rule settings values" "description": "CSSLint rule settings values"

View File

@ -374,16 +374,13 @@ body[data-match-highlight="selection"] .CodeMirror-selection-highlight-scrollbar
right: 4px; right: 4px;
top: .5em; top: .5em;
} }
#help-popup .error { #help-popup .saved-message {
display: none; display: none;
color: #090;
margin-left: 10px; margin-left: 10px;
color: #900;
font-weight: bold; font-weight: bold;
} }
#help-popup .error a { #help-popup .saved-message.show,
color: #f00;
}
#help-popup .error.show,
#options .linter-settings { #options .linter-settings {
display: inline-block; display: inline-block;
} }
@ -467,6 +464,9 @@ body[data-match-highlight="selection"] .CodeMirror-selection-highlight-scrollbar
#lint td[role="message"] { #lint td[role="message"] {
text-align: left; text-align: left;
} }
#message-box.center.lint-config #message-box-contents {
text-align: left;
}
/************ CSS beautifier ************/ /************ CSS beautifier ************/
.beautify-options { .beautify-options {

View File

@ -1,4 +1,4 @@
/* global CodeMirror CSSLint editors makeSectionVisible showHelp showCodeMirrorPopup */ /* global CodeMirror CSSLint editors makeSectionVisible showHelp showCodeMirrorPopup messageBox */
/* global stylelintDefaultConfig csslintDefaultRuleset onDOMscripted injectCSS require */ /* global stylelintDefaultConfig csslintDefaultRuleset onDOMscripted injectCSS require */
'use strict'; 'use strict';
@ -56,8 +56,7 @@ function updateLinter(linter) {
loadSelectedLinter(linter).then(() => { loadSelectedLinter(linter).then(() => {
updateEditors(); updateEditors();
}); });
$('#linter-settings').style.display = linter === 'null' ? $('#linter-settings').style.display = linter === 'null' ? 'none' : 'inline-block';
'none' : 'inline-block';
} }
function updateLintReport(cm, delay) { function updateLintReport(cm, delay) {
@ -236,6 +235,27 @@ function showLintHelp() {
return showHelp(t('issues'), header + list + '</ul>'); return showHelp(t('issues'), header + list + '</ul>');
} }
function showLinterErrorMessage(title, contents) {
messageBox({
title,
contents,
className: 'danger center lint-config',
buttons: [t('confirmOK')],
});
}
function 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);
}
function checkLinter(linter = prefs.get('editor.linter')) { function checkLinter(linter = prefs.get('editor.linter')) {
linter = linter.toLowerCase(); linter = linter.toLowerCase();
if (prefs.get('editor.linter') !== linter) { if (prefs.get('editor.linter') !== linter) {
@ -244,29 +264,41 @@ function checkLinter(linter = prefs.get('editor.linter')) {
return linter; return linter;
} }
function checkRules(linter, rules) {
const invalid = [];
const linterRules = linter === 'stylelint'
? Object.keys(window.stylelint.rules)
: CSSLint.getRules().map(rule => rule.id);
Object.keys(rules).forEach(rule => {
if (!linterRules.includes(rule)) {
invalid.push(rule);
}
});
return invalid;
}
function setupLinterSettingsEvents(popup) { function setupLinterSettingsEvents(popup) {
$('.save', popup).addEventListener('click', event => { $('.save', popup).addEventListener('click', event => {
event.preventDefault(); event.preventDefault();
const linter = checkLinter(event.target.dataset.linter); const linter = checkLinter(event.target.dataset.linter);
const json = tryJSONparse(popup.codebox.getValue()); const json = tryJSONparse(popup.codebox.getValue());
if (json && json.rules) { if (json && json.rules) {
// it is possible to have stylelint rules popup open & switch to csslint const invalid = checkRules(linter, json.rules);
if (invalid.length) {
return showLinterErrorMessage(
linter,
t('setLinterInvalidRuleError') + `<ul><li>${invalid.join('</li><li>')}</li></ul>`
);
}
if (linter === 'stylelint') { if (linter === 'stylelint') {
setStylelintRules(json.rules); setStylelintRules(json.rules);
} else { } else {
setCSSLintRules(json.rules); setCSSLintRules(json.rules);
} }
updateLinter(linter); updateLinter(linter);
showSavedMessage();
} else { } else {
$('#help-popup .error').classList.add('show'); showLinterErrorMessage(linter, t('setLinterError'));
clearTimeout($('#help-popup .contents').timer);
$('#help-popup .contents').timer = setTimeout(() => {
// popup may be closed at this point
const error = $('#help-popup .error');
if (error) {
error.classList.remove('show');
}
}, 3000);
} }
}); });
$('.reset', popup).addEventListener('click', event => { $('.reset', popup).addEventListener('click', event => {
@ -334,8 +366,8 @@ function setupLinterPopup(rules) {
makeButton('reset', 'genericResetLabel'), makeButton('reset', 'genericResetLabel'),
$element({ $element({
tag: 'span', tag: 'span',
className: 'error', className: 'saved-message',
textContent: t('setLinterError') textContent: t('genericSavedMessage')
}) })
] ]
})); }));
@ -357,10 +389,12 @@ function loadSelectedLinter(name) {
if (name !== 'null' && !$('script[src*="css-lint.js"]')) { if (name !== 'null' && !$('script[src*="css-lint.js"]')) {
// inject css // inject css
injectCSS('vendor/codemirror/addon/lint/lint.css'); injectCSS('vendor/codemirror/addon/lint/lint.css');
injectCSS('msgbox/msgbox.css');
// load CodeMirror lint code // load CodeMirror lint code
scripts.push( scripts.push(
'vendor/codemirror/addon/lint/lint.js', 'vendor/codemirror/addon/lint/lint.js',
'vendor-overwrites/codemirror/addon/lint/css-lint.js' 'vendor-overwrites/codemirror/addon/lint/css-lint.js',
'msgbox/msgbox.js'
); );
} }
if (name === 'csslint' && !window.CSSLint) { if (name === 'csslint' && !window.CSSLint) {