Make WordPress Core

Changeset 41974


Ignore:
Timestamp:
10/23/2017 07:54:33 PM (8 years ago)
Author:
westonruter
Message:

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.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/code-editor.js

    r41852 r41974  
    3737     */
    3838    function configureLinting( editor, settings ) { // eslint-disable-line complexity
    39         var lintOptions = editor.getOption( 'lint' ), currentErrorAnnotations = [], updateErrorNotice, previouslyShownErrorAnnotations = [];
    40         if ( ! lintOptions ) {
    41             return;
    42         }
    43 
    44         if ( true === lintOptions ) {
    45             lintOptions = {};
    46         } else {
    47             lintOptions = $.extend( {}, lintOptions );
    48         }
    49 
    50         // 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>.
    51         if ( ! lintOptions.options ) {
    52             lintOptions.options = {};
    53         }
    54 
    55         // Configure JSHint.
    56         if ( 'javascript' === settings.codemirror.mode && settings.jshint ) {
    57             $.extend( lintOptions.options, settings.jshint );
    58         }
    59 
    60         // Configure CSSLint.
    61         if ( 'css' === settings.codemirror.mode && settings.csslint ) {
    62             $.extend( lintOptions.options, settings.csslint );
    63         }
    64 
    65         // Configure HTMLHint.
    66         if ( 'htmlmixed' === settings.codemirror.mode && settings.htmlhint ) {
    67             lintOptions.options.rules = $.extend( {}, settings.htmlhint );
    68 
    69             if ( settings.jshint ) {
    70                 lintOptions.options.rules.jshint = settings.jshint;
    71             }
    72             if ( settings.csslint ) {
    73                 lintOptions.options.rules.csslint = settings.csslint;
    74             }
    75         }
     39        var currentErrorAnnotations = [], previouslyShownErrorAnnotations = [];
    7640
    7741        /**
     
    8044         * @returns {void}
    8145         */
    82         updateErrorNotice = function() {
     46        function updateErrorNotice() {
    8347            if ( settings.onUpdateErrorNotice && ! _.isEqual( currentErrorAnnotations, previouslyShownErrorAnnotations ) ) {
    8448                settings.onUpdateErrorNotice( currentErrorAnnotations, editor );
    8549                previouslyShownErrorAnnotations = currentErrorAnnotations;
    8650            }
    87         };
    88 
    89         // Wrap the onUpdateLinting CodeMirror event to route to onChangeLintingErrors and onUpdateErrorNotice.
    90         lintOptions.onUpdateLinting = (function( onUpdateLintingOverridden ) {
    91             return function( annotations, annotationsSorted, cm ) {
    92                 var errorAnnotations = _.filter( annotations, function( annotation ) {
    93                     return 'error' === annotation.severity;
    94                 } );
    95 
    96                 if ( onUpdateLintingOverridden ) {
    97                     onUpdateLintingOverridden.apply( annotations, annotationsSorted, cm );
    98                 }
    99 
    100                 // Skip if there are no changes to the errors.
    101                 if ( _.isEqual( errorAnnotations, currentErrorAnnotations ) ) {
    102                     return;
    103                 }
    104 
    105                 currentErrorAnnotations = errorAnnotations;
    106 
    107                 if ( settings.onChangeLintingErrors ) {
    108                     settings.onChangeLintingErrors( errorAnnotations, annotations, annotationsSorted, cm );
    109                 }
    110 
    111                 /*
    112                  * Update notifications when the editor is not focused to prevent error message
    113                  * from overwhelming the user during input, unless there are now no errors or there
    114                  * were previously errors shown. In these cases, update immediately so they can know
    115                  * that they fixed the errors.
    116                  */
    117                 if ( ! cm.state.focused || 0 === currentErrorAnnotations.length || previouslyShownErrorAnnotations.length > 0 ) {
    118                     updateErrorNotice();
    119                 }
    120             };
    121         })( lintOptions.onUpdateLinting );
    122 
    123         editor.setOption( 'lint', lintOptions );
     51        }
     52
     53        /**
     54         * Get lint options.
     55         *
     56         * @returns {object} Lint options.
     57         */
     58        function getLintOptions() { // eslint-disable-line complexity
     59            var options = editor.getOption( 'lint' );
     60
     61            if ( ! options ) {
     62                return false;
     63            }
     64
     65            if ( true === options ) {
     66                options = {};
     67            } else if ( _.isObject( options ) ) {
     68                options = $.extend( {}, options );
     69            }
     70
     71            // 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>.
     72            if ( ! options.options ) {
     73                options.options = {};
     74            }
     75
     76            // Configure JSHint.
     77            if ( 'javascript' === settings.codemirror.mode && settings.jshint ) {
     78                $.extend( options.options, settings.jshint );
     79            }
     80
     81            // Configure CSSLint.
     82            if ( 'css' === settings.codemirror.mode && settings.csslint ) {
     83                $.extend( options.options, settings.csslint );
     84            }
     85
     86            // Configure HTMLHint.
     87            if ( 'htmlmixed' === settings.codemirror.mode && settings.htmlhint ) {
     88                options.options.rules = $.extend( {}, settings.htmlhint );
     89
     90                if ( settings.jshint ) {
     91                    options.options.rules.jshint = settings.jshint;
     92                }
     93                if ( settings.csslint ) {
     94                    options.options.rules.csslint = settings.csslint;
     95                }
     96            }
     97
     98            // Wrap the onUpdateLinting CodeMirror event to route to onChangeLintingErrors and onUpdateErrorNotice.
     99            options.onUpdateLinting = (function( onUpdateLintingOverridden ) {
     100                return function( annotations, annotationsSorted, cm ) {
     101                    var errorAnnotations = _.filter( annotations, function( annotation ) {
     102                        return 'error' === annotation.severity;
     103                    } );
     104
     105                    if ( onUpdateLintingOverridden ) {
     106                        onUpdateLintingOverridden.apply( annotations, annotationsSorted, cm );
     107                    }
     108
     109                    // Skip if there are no changes to the errors.
     110                    if ( _.isEqual( errorAnnotations, currentErrorAnnotations ) ) {
     111                        return;
     112                    }
     113
     114                    currentErrorAnnotations = errorAnnotations;
     115
     116                    if ( settings.onChangeLintingErrors ) {
     117                        settings.onChangeLintingErrors( errorAnnotations, annotations, annotationsSorted, cm );
     118                    }
     119
     120                    /*
     121                     * Update notifications when the editor is not focused to prevent error message
     122                     * from overwhelming the user during input, unless there are now no errors or there
     123                     * were previously errors shown. In these cases, update immediately so they can know
     124                     * that they fixed the errors.
     125                     */
     126                    if ( ! editor.state.focused || 0 === currentErrorAnnotations.length || previouslyShownErrorAnnotations.length > 0 ) {
     127                        updateErrorNotice();
     128                    }
     129                };
     130            })( options.onUpdateLinting );
     131
     132            return options;
     133        }
     134
     135        editor.setOption( 'lint', getLintOptions() );
     136
     137        // Keep lint options populated.
     138        editor.on( 'optionChange', function( cm, option ) {
     139            var options;
     140            if ( 'lint' !== option ) {
     141                return;
     142            }
     143            options = editor.getOption( 'lint' );
     144            if ( true === options ) {
     145                editor.setOption( 'lint', getLintOptions() ); // Expand to include linting options.
     146            }
     147
     148            // Force update on error notice to show or hide.
     149            if ( editor.getOption( 'lint' ) ) {
     150                editor.performLint();
     151            } else {
     152                currentErrorAnnotations = [];
     153                updateErrorNotice();
     154            }
     155        } );
    124156
    125157        // Update error notice when leaving the editor.
  • trunk/src/wp-includes/general-template.php

    r41728 r41974  
    33123312        $settings['codemirror'] = array_merge( $settings['codemirror'], array(
    33133313            'mode' => $type,
     3314            'lint' => false,
    33143315            'autoCloseBrackets' => true,
    33153316            'matchBrackets' => true,
Note: See TracChangeset for help on using the changeset viewer.