Code Editor: Keep linting and its error reporting configured when lint option is changed dynamically.

Also explicitly disable `lint` option for SCSS and LESS.

See #12423.

Built from https://develop.svn.wordpress.org/trunk@41974


git-svn-id: http://core.svn.wordpress.org/trunk@41808 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2017-10-23 19:55:46 +00:00
parent 91ad9114d0
commit 0343b1a2c8
4 changed files with 102 additions and 69 deletions

View File

@ -36,91 +36,123 @@ if ( 'undefined' === typeof window.wp.codeEditor ) {
* @returns {void}
*/
function configureLinting( editor, settings ) { // eslint-disable-line complexity
var lintOptions = editor.getOption( 'lint' ), currentErrorAnnotations = [], updateErrorNotice, previouslyShownErrorAnnotations = [];
if ( ! lintOptions ) {
return;
}
if ( true === lintOptions ) {
lintOptions = {};
} else {
lintOptions = $.extend( {}, lintOptions );
}
// Note that rules must be sent in the "deprecated" lint.options property to prevent linter from complaining about unrecognized options. See <https://github.com/codemirror/CodeMirror/pull/4944>.
if ( ! lintOptions.options ) {
lintOptions.options = {};
}
// Configure JSHint.
if ( 'javascript' === settings.codemirror.mode && settings.jshint ) {
$.extend( lintOptions.options, settings.jshint );
}
// Configure CSSLint.
if ( 'css' === settings.codemirror.mode && settings.csslint ) {
$.extend( lintOptions.options, settings.csslint );
}
// Configure HTMLHint.
if ( 'htmlmixed' === settings.codemirror.mode && settings.htmlhint ) {
lintOptions.options.rules = $.extend( {}, settings.htmlhint );
if ( settings.jshint ) {
lintOptions.options.rules.jshint = settings.jshint;
}
if ( settings.csslint ) {
lintOptions.options.rules.csslint = settings.csslint;
}
}
var currentErrorAnnotations = [], previouslyShownErrorAnnotations = [];
/**
* Call the onUpdateErrorNotice if there are new errors to show.
*
* @returns {void}
*/
updateErrorNotice = function() {
function updateErrorNotice() {
if ( settings.onUpdateErrorNotice && ! _.isEqual( currentErrorAnnotations, previouslyShownErrorAnnotations ) ) {
settings.onUpdateErrorNotice( currentErrorAnnotations, editor );
previouslyShownErrorAnnotations = currentErrorAnnotations;
}
};
}
// Wrap the onUpdateLinting CodeMirror event to route to onChangeLintingErrors and onUpdateErrorNotice.
lintOptions.onUpdateLinting = (function( onUpdateLintingOverridden ) {
return function( annotations, annotationsSorted, cm ) {
var errorAnnotations = _.filter( annotations, function( annotation ) {
return 'error' === annotation.severity;
} );
/**
* Get lint options.
*
* @returns {object} Lint options.
*/
function getLintOptions() { // eslint-disable-line complexity
var options = editor.getOption( 'lint' );
if ( onUpdateLintingOverridden ) {
onUpdateLintingOverridden.apply( annotations, annotationsSorted, cm );
if ( ! options ) {
return false;
}
if ( true === options ) {
options = {};
} else if ( _.isObject( options ) ) {
options = $.extend( {}, options );
}
// Note that rules must be sent in the "deprecated" lint.options property to prevent linter from complaining about unrecognized options. See <https://github.com/codemirror/CodeMirror/pull/4944>.
if ( ! options.options ) {
options.options = {};
}
// Configure JSHint.
if ( 'javascript' === settings.codemirror.mode && settings.jshint ) {
$.extend( options.options, settings.jshint );
}
// Configure CSSLint.
if ( 'css' === settings.codemirror.mode && settings.csslint ) {
$.extend( options.options, settings.csslint );
}
// Configure HTMLHint.
if ( 'htmlmixed' === settings.codemirror.mode && settings.htmlhint ) {
options.options.rules = $.extend( {}, settings.htmlhint );
if ( settings.jshint ) {
options.options.rules.jshint = settings.jshint;
}
// Skip if there are no changes to the errors.
if ( _.isEqual( errorAnnotations, currentErrorAnnotations ) ) {
return;
if ( settings.csslint ) {
options.options.rules.csslint = settings.csslint;
}
}
currentErrorAnnotations = errorAnnotations;
// Wrap the onUpdateLinting CodeMirror event to route to onChangeLintingErrors and onUpdateErrorNotice.
options.onUpdateLinting = (function( onUpdateLintingOverridden ) {
return function( annotations, annotationsSorted, cm ) {
var errorAnnotations = _.filter( annotations, function( annotation ) {
return 'error' === annotation.severity;
} );
if ( settings.onChangeLintingErrors ) {
settings.onChangeLintingErrors( errorAnnotations, annotations, annotationsSorted, cm );
}
if ( onUpdateLintingOverridden ) {
onUpdateLintingOverridden.apply( annotations, annotationsSorted, cm );
}
/*
* Update notifications when the editor is not focused to prevent error message
* from overwhelming the user during input, unless there are now no errors or there
* were previously errors shown. In these cases, update immediately so they can know
* that they fixed the errors.
*/
if ( ! cm.state.focused || 0 === currentErrorAnnotations.length || previouslyShownErrorAnnotations.length > 0 ) {
updateErrorNotice();
}
};
})( lintOptions.onUpdateLinting );
// Skip if there are no changes to the errors.
if ( _.isEqual( errorAnnotations, currentErrorAnnotations ) ) {
return;
}
editor.setOption( 'lint', lintOptions );
currentErrorAnnotations = errorAnnotations;
if ( settings.onChangeLintingErrors ) {
settings.onChangeLintingErrors( errorAnnotations, annotations, annotationsSorted, cm );
}
/*
* Update notifications when the editor is not focused to prevent error message
* from overwhelming the user during input, unless there are now no errors or there
* were previously errors shown. In these cases, update immediately so they can know
* that they fixed the errors.
*/
if ( ! editor.state.focused || 0 === currentErrorAnnotations.length || previouslyShownErrorAnnotations.length > 0 ) {
updateErrorNotice();
}
};
})( options.onUpdateLinting );
return options;
}
editor.setOption( 'lint', getLintOptions() );
// Keep lint options populated.
editor.on( 'optionChange', function( cm, option ) {
var options;
if ( 'lint' !== option ) {
return;
}
options = editor.getOption( 'lint' );
if ( true === options ) {
editor.setOption( 'lint', getLintOptions() ); // Expand to include linting options.
}
// Force update on error notice to show or hide.
if ( editor.getOption( 'lint' ) ) {
editor.performLint();
} else {
currentErrorAnnotations = [];
updateErrorNotice();
}
} );
// Update error notice when leaving the editor.
editor.on( 'blur', updateErrorNotice );

View File

@ -1 +1 @@
"undefined"==typeof window.wp&&(window.wp={}),"undefined"==typeof window.wp.codeEditor&&(window.wp.codeEditor={}),function(a,b){"use strict";function c(b,c){var d,e=b.getOption("lint"),f=[],g=[];e&&(e=!0===e?{}:a.extend({},e),e.options||(e.options={}),"javascript"===c.codemirror.mode&&c.jshint&&a.extend(e.options,c.jshint),"css"===c.codemirror.mode&&c.csslint&&a.extend(e.options,c.csslint),"htmlmixed"===c.codemirror.mode&&c.htmlhint&&(e.options.rules=a.extend({},c.htmlhint),c.jshint&&(e.options.rules.jshint=c.jshint),c.csslint&&(e.options.rules.csslint=c.csslint)),d=function(){c.onUpdateErrorNotice&&!_.isEqual(f,g)&&(c.onUpdateErrorNotice(f,b),g=f)},e.onUpdateLinting=function(a){return function(b,e,h){var i=_.filter(b,function(a){return"error"===a.severity});a&&a.apply(b,e,h),_.isEqual(i,f)||(f=i,c.onChangeLintingErrors&&c.onChangeLintingErrors(i,b,e,h),(!h.state.focused||0===f.length||g.length>0)&&d())}}(e.onUpdateLinting),b.setOption("lint",e),b.on("blur",d),b.on("startCompletion",function(){b.off("blur",d)}),b.on("endCompletion",function(){var a=500;b.on("blur",d),_.delay(function(){b.state.focused||d()},a)}),a(document.body).on("mousedown",function(c){!b.state.focused||a.contains(b.display.wrapper,c.target)||a(c.target).hasClass("CodeMirror-hint")||d()}))}function d(b,c){var d=a(b.getTextArea());b.on("blur",function(){d.data("next-tab-blurs",!1)}),b.on("keydown",function(a,e){var f=9,g=27;return g===e.keyCode?void d.data("next-tab-blurs",!0):void(f===e.keyCode&&d.data("next-tab-blurs")&&(e.shiftKey?c.onTabPrevious(b,e):c.onTabNext(b,e),d.data("next-tab-blurs",!1),e.preventDefault()))})}b.codeEditor.defaultSettings={codemirror:{},csslint:{},htmlhint:{},jshint:{},onTabNext:function(){},onTabPrevious:function(){},onChangeLintingErrors:function(){},onUpdateErrorNotice:function(){}},b.codeEditor.initialize=function(e,f){var g,h,i,j;return g=a("string"==typeof e?"#"+e:e),i=a.extend({},b.codeEditor.defaultSettings,f),i.codemirror=a.extend({},i.codemirror),h=b.CodeMirror.fromTextArea(g[0],i.codemirror),c(h,i),j={settings:i,codemirror:h},h.showHint&&h.on("keyup",function(a,c){var d,e,f,g,i=/^[a-zA-Z]$/.test(c.key);h.state.completionActive&&i||(g=h.getTokenAt(h.getCursor()),"string"!==g.type&&"comment"!==g.type&&(f=b.CodeMirror.innerMode(h.getMode(),g.state).mode.name,e=h.doc.getLine(h.doc.getCursor().line).substr(0,h.doc.getCursor().ch),"html"===f||"xml"===f?d="<"===c.key||"/"===c.key&&"tag"===g.type||i&&"tag"===g.type||i&&"attribute"===g.type||"="===g.string&&g.state.htmlState&&g.state.htmlState.tagName:"css"===f?d=i||":"===c.key||" "===c.key&&/:\s+$/.test(e):"javascript"===f?d=i||"."===c.key:"clike"===f&&"application/x-httpd-php"===h.options.mode&&(d="keyword"===g.type||"variable"===g.type),d&&h.showHint({completeSingle:!1})))}),d(h,f),j}}(window.jQuery,window.wp);
"undefined"==typeof window.wp&&(window.wp={}),"undefined"==typeof window.wp.codeEditor&&(window.wp.codeEditor={}),function(a,b){"use strict";function c(b,c){function d(){c.onUpdateErrorNotice&&!_.isEqual(f,g)&&(c.onUpdateErrorNotice(f,b),g=f)}function e(){var e=b.getOption("lint");return!!e&&(!0===e?e={}:_.isObject(e)&&(e=a.extend({},e)),e.options||(e.options={}),"javascript"===c.codemirror.mode&&c.jshint&&a.extend(e.options,c.jshint),"css"===c.codemirror.mode&&c.csslint&&a.extend(e.options,c.csslint),"htmlmixed"===c.codemirror.mode&&c.htmlhint&&(e.options.rules=a.extend({},c.htmlhint),c.jshint&&(e.options.rules.jshint=c.jshint),c.csslint&&(e.options.rules.csslint=c.csslint)),e.onUpdateLinting=function(a){return function(e,h,i){var j=_.filter(e,function(a){return"error"===a.severity});a&&a.apply(e,h,i),_.isEqual(j,f)||(f=j,c.onChangeLintingErrors&&c.onChangeLintingErrors(j,e,h,i),(!b.state.focused||0===f.length||g.length>0)&&d())}}(e.onUpdateLinting),e)}var f=[],g=[];b.setOption("lint",e()),b.on("optionChange",function(a,c){var g;"lint"===c&&(g=b.getOption("lint"),!0===g&&b.setOption("lint",e()),b.getOption("lint")?b.performLint():(f=[],d()))}),b.on("blur",d),b.on("startCompletion",function(){b.off("blur",d)}),b.on("endCompletion",function(){var a=500;b.on("blur",d),_.delay(function(){b.state.focused||d()},a)}),a(document.body).on("mousedown",function(c){!b.state.focused||a.contains(b.display.wrapper,c.target)||a(c.target).hasClass("CodeMirror-hint")||d()})}function d(b,c){var d=a(b.getTextArea());b.on("blur",function(){d.data("next-tab-blurs",!1)}),b.on("keydown",function(a,e){var f=9,g=27;return g===e.keyCode?void d.data("next-tab-blurs",!0):void(f===e.keyCode&&d.data("next-tab-blurs")&&(e.shiftKey?c.onTabPrevious(b,e):c.onTabNext(b,e),d.data("next-tab-blurs",!1),e.preventDefault()))})}b.codeEditor.defaultSettings={codemirror:{},csslint:{},htmlhint:{},jshint:{},onTabNext:function(){},onTabPrevious:function(){},onChangeLintingErrors:function(){},onUpdateErrorNotice:function(){}},b.codeEditor.initialize=function(e,f){var g,h,i,j;return g=a("string"==typeof e?"#"+e:e),i=a.extend({},b.codeEditor.defaultSettings,f),i.codemirror=a.extend({},i.codemirror),h=b.CodeMirror.fromTextArea(g[0],i.codemirror),c(h,i),j={settings:i,codemirror:h},h.showHint&&h.on("keyup",function(a,c){var d,e,f,g,i=/^[a-zA-Z]$/.test(c.key);h.state.completionActive&&i||(g=h.getTokenAt(h.getCursor()),"string"!==g.type&&"comment"!==g.type&&(f=b.CodeMirror.innerMode(h.getMode(),g.state).mode.name,e=h.doc.getLine(h.doc.getCursor().line).substr(0,h.doc.getCursor().ch),"html"===f||"xml"===f?d="<"===c.key||"/"===c.key&&"tag"===g.type||i&&"tag"===g.type||i&&"attribute"===g.type||"="===g.string&&g.state.htmlState&&g.state.htmlState.tagName:"css"===f?d=i||":"===c.key||" "===c.key&&/:\s+$/.test(e):"javascript"===f?d=i||"."===c.key:"clike"===f&&"application/x-httpd-php"===h.options.mode&&(d="keyword"===g.type||"variable"===g.type),d&&h.showHint({completeSingle:!1})))}),d(h,f),j}}(window.jQuery,window.wp);

View File

@ -3311,6 +3311,7 @@ function wp_enqueue_code_editor( $args ) {
} elseif ( 'text/x-scss' === $type || 'text/x-less' === $type || 'text/x-sass' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => $type,
'lint' => false,
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.9-beta3-41973';
$wp_version = '4.9-beta3-41974';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.