@@ -9,10 +9,14 @@ import { getCodeMirror } from 'app/utils/codemirror';
99import 'codemirror/addon/dialog/dialog' ;
1010import 'codemirror/addon/hint/show-hint' ;
1111import 'codemirror/addon/tern/tern' ;
12+ import 'codemirror/addon/lint/lint.css' ;
13+ import 'codemirror/addon/lint/lint' ;
1214
1315import FuzzySearch from '../FuzzySearch' ;
1416import { Container , CodeContainer } from './elements' ;
1517
18+ import LinterWorker from 'worker-loader?name=monaco-linter.[hash].worker.js!../Monaco/workers/linter' ;
19+
1620import type { Props , Editor } from '../types' ;
1721
1822type State = { fuzzySearchEnabled : boolean } ;
@@ -90,6 +94,46 @@ class CodemirrorEditor extends React.Component<Props, State> implements Editor {
9094 } ) ;
9195 } ;
9296
97+ linterWorker : ?Worker ;
98+
99+ validate = ( code : string = '' , updateLinting : Function ) => {
100+ if ( code . trim ( ) === '' ) {
101+ updateLinting ( [ ] ) ;
102+ return ;
103+ }
104+
105+ const linterWorker = this . linterWorker ;
106+ if ( linterWorker ) {
107+ linterWorker . postMessage ( {
108+ code,
109+ title : this . currentModule . title ,
110+ version : code ,
111+ } ) ;
112+
113+ linterWorker . onmessage = ( event : MessageEvent ) => {
114+ // $FlowIssue
115+ const { markers, version } = event . data ;
116+
117+ if ( version === code ) {
118+ updateLinting (
119+ markers . map ( error => ( {
120+ message : `eslint: ${ error . message } ` ,
121+ severity : error . severity === 2 ? 'warning' : 'error' ,
122+ from : new CodeMirror . Pos (
123+ error . startLineNumber - 1 ,
124+ error . startColumn - 1
125+ ) ,
126+ to : new CodeMirror . Pos (
127+ error . endLineNumber - 1 ,
128+ error . endColumn - 1
129+ ) ,
130+ } ) )
131+ ) ;
132+ }
133+ } ;
134+ }
135+ } ;
136+
93137 changeSettings = async ( settings : $PropertyType < Props , 'settings' > ) => {
94138 const defaultKeys = {
95139 'Cmd-/' : cm => {
@@ -185,12 +229,13 @@ class CodemirrorEditor extends React.Component<Props, State> implements Editor {
185229 }
186230
187231 if ( settings . lintEnabled ) {
188- const initialized = 'eslint' in window ;
189- import ( /* webpackChunkName: 'codemirror-eslint' */ 'app/utils/codemirror/eslint-lint' )
190- . then ( initializer => ! initialized && initializer . default ( ) )
191- . then ( ( ) => {
192- this . codemirror . setOption ( 'lint' , true ) ;
232+ if ( ! this . linterWorker ) {
233+ this . linterWorker = new LinterWorker ( ) ;
234+ this . codemirror . setOption ( 'lint' , {
235+ getAnnotations : this . validate ,
236+ async : true ,
193237 } ) ;
238+ }
194239 } else {
195240 this . codemirror . setOption ( 'lint' , false ) ;
196241 }
0 commit comments