@@ -11,52 +11,14 @@ import NodePathTester from './node-path-tester';
1111import JobManager from './job-manager' ;
1212import * as helpers from './helpers' ;
1313
14- const IDLE_CALLBACKS = new Set ( ) ;
15-
16- const makeIdleCallback = work => {
17- let callbackId ;
18- const callback = ( ) => {
19- IDLE_CALLBACKS . delete ( callbackId ) ;
20- work ( ) ;
21- } ;
22- callbackId = window . requestIdleCallback ( callback ) ;
23- IDLE_CALLBACKS . add ( callbackId ) ;
24- } ;
25-
26- const scheduleIdleTasks = ( ) => {
27- const linterEslintInstallPeerPackages = ( ) => {
28- require ( 'atom-package-deps' ) . install ( 'linter-eslint-node' ) ;
29- } ;
30-
31- if ( ! atom . inSpecMode ( ) ) {
32- makeIdleCallback ( linterEslintInstallPeerPackages ) ;
33- }
34- } ;
35-
36- const SCOPES = atom . config . get ( 'linter-eslint-node.scopes' ) ;
37-
38- const EMBEDDED_SCOPE = 'source.js.embedded.html' ;
39-
4014const BUTTON_SETTINGS = {
4115 text : 'Settings' ,
4216 onDidClick ( ) {
4317 atom . workspace . open ( `atom://config/packages/linter-eslint-node` ) ;
4418 }
4519} ;
4620
47-
48- if ( atom . config . get ( 'linter-eslint-node.lintHtmlFiles' ) ) {
49- if ( ! SCOPES . includes ( EMBEDDED_SCOPE ) ) {
50- SCOPES . push ( EMBEDDED_SCOPE ) ;
51- }
52- } else {
53- if ( SCOPES . includes ( EMBEDDED_SCOPE ) ) {
54- SCOPES . splice (
55- SCOPES . indexOf ( EMBEDDED_SCOPE ) ,
56- 1
57- ) ;
58- }
59- }
21+ const EMBEDDED_SCOPE = 'source.js.embedded.html' ;
6022
6123const RECENTLY_SAVED = new Set ( ) ;
6224function hasRecentlySaved ( path ) {
@@ -65,8 +27,20 @@ function hasRecentlySaved (path) {
6527 setTimeout ( ( ) => RECENTLY_SAVED . delete ( path ) , 100 ) ;
6628}
6729
68- class LinterESLintNode {
69- constructor ( ) {
30+ export default {
31+
32+ shouldAutoFix ( textEditor ) {
33+ if ( textEditor . isModified ( ) ) { return false ; }
34+ if ( ! Config . get ( 'autofix.fixOnSave' ) ) { return false ; }
35+ if ( ! helpers . hasValidScope ( textEditor , this . scopes ) ) { return false ; }
36+ return true ;
37+ } ,
38+
39+ isLegacyPackagePresent ( ) {
40+ return ( 'linter-eslint' in atom . packages . activePackages ) ;
41+ } ,
42+
43+ async activate ( ) {
7044 Config . initialize ( ) ;
7145 this . workerPath = Path . join ( __dirname , 'worker.js' ) ;
7246 NodePathTester . test ( Config . get ( 'nodeBin' ) ) ;
@@ -79,21 +53,23 @@ class LinterESLintNode {
7953 incompatibleVersion : false ,
8054 invalidNodeBin : false
8155 } ;
82- }
8356
84- shouldAutoFix ( textEditor ) {
85- if ( textEditor . isModified ( ) ) { return false ; }
86- if ( ! Config . get ( 'autofix.fixOnSave' ) ) { return false ; }
87- if ( ! helpers . hasValidScope ( textEditor , SCOPES ) ) { return false ; }
88- return true ;
89- }
57+ this . subscriptions = new CompositeDisposable ( ) ;
9058
91- isLegacyPackagePresent ( ) {
92- return ( 'linter-eslint' in atom . packages . activePackages ) ;
93- }
59+ this . scopes = atom . config . get ( 'linter-eslint-node.scopes' ) ;
9460
95- async activate ( ) {
96- this . subscriptions = new CompositeDisposable ( ) ;
61+ if ( atom . config . get ( 'linter-eslint-node.lintHtmlFiles' ) ) {
62+ if ( ! this . scopes . includes ( EMBEDDED_SCOPE ) ) {
63+ this . scopes . push ( EMBEDDED_SCOPE ) ;
64+ }
65+ } else {
66+ if ( this . scopes . includes ( EMBEDDED_SCOPE ) ) {
67+ this . scopes . splice (
68+ this . scopes . indexOf ( EMBEDDED_SCOPE ) ,
69+ 1
70+ ) ;
71+ }
72+ }
9773
9874 this . subscriptions . add (
9975 atom . workspace . observeTextEditors (
@@ -127,21 +103,21 @@ class LinterESLintNode {
127103 }
128104
129105 if ( config . scopes !== prevConfig . scopes ) {
130- SCOPES . splice ( 0 , SCOPES . length ) ;
131- SCOPES . push ( ...config . scopes ) ;
106+ this . scopes . splice ( 0 , this . scopes . length ) ;
107+ this . scopes . push ( ...config . scopes ) ;
132108 if ( config . lintHtmlFiles ) {
133- SCOPES . push ( EMBEDDED_SCOPE ) ;
109+ this . scopes . push ( EMBEDDED_SCOPE ) ;
134110 }
135111 }
136112
137113 if ( config . lintHtmlFiles !== prevConfig . lintHtmlFiles ) {
138114 if ( config . lintHtmlFiles ) {
139- if ( ! SCOPES . includes ( EMBEDDED_SCOPE ) ) {
140- SCOPES . push ( EMBEDDED_SCOPE ) ;
115+ if ( ! this . scopes . includes ( EMBEDDED_SCOPE ) ) {
116+ this . scopes . push ( EMBEDDED_SCOPE ) ;
141117 }
142118 } else {
143- if ( SCOPES . includes ( EMBEDDED_SCOPE ) ) {
144- SCOPES . splice ( SCOPES . indexOf ( EMBEDDED_SCOPE ) , 1 ) ;
119+ if ( this . scopes . includes ( EMBEDDED_SCOPE ) ) {
120+ this . scopes . splice ( this . scopes . indexOf ( EMBEDDED_SCOPE ) , 1 ) ;
145121 }
146122 }
147123 }
@@ -186,14 +162,16 @@ class LinterESLintNode {
186162 } )
187163 ) ;
188164
189- scheduleIdleTasks ( ) ;
190- }
165+ if ( ! atom . inSpecMode ( ) ) {
166+ await require ( 'atom-package-deps' ) . install ( 'linter-eslint-node' ) ;
167+ }
168+ } ,
191169
192170 async deactivate ( ) {
193171 this . subscriptions . dispose ( ) ;
194172 this . jobManager . dispose ( ) ;
195173 Config . dispose ( ) ;
196- }
174+ } ,
197175
198176 notifyAboutInvalidNodeBin ( ) {
199177 if ( this . notified . invalidNodeBin ) { return ; }
@@ -207,7 +185,7 @@ class LinterESLintNode {
207185 }
208186 ) ;
209187 this . notified . invalidNodeBin = true ;
210- }
188+ } ,
211189
212190 // We need to tell the worker to clear its cache when
213191 // - any .eslintignore file is changed;
@@ -217,7 +195,7 @@ class LinterESLintNode {
217195 clearESLintCache ( ) {
218196 console . warn ( 'Telling the worker to clear its cache!' ) ;
219197 this . jobManager . send ( { type : 'clear-cache' } ) ;
220- }
198+ } ,
221199
222200 // Show a bunch of stuff to the user that can help them figure out why the
223201 // package isn't behaving the way they think it ought to, or else give them
@@ -331,7 +309,7 @@ class LinterESLintNode {
331309 dismissable : true
332310 }
333311 ) ;
334- }
312+ } ,
335313
336314 // Here we're operating entirely outside the purview of the `linter` package.
337315 // This method is called either automatically, via `fixOnSave`; or manually,
@@ -386,7 +364,7 @@ class LinterESLintNode {
386364 this . handleError ( err ) ;
387365 return ;
388366 }
389- }
367+ } ,
390368
391369 handleError ( err ) {
392370 console . debug ( 'handleError:' , err ) ;
@@ -451,15 +429,15 @@ class LinterESLintNode {
451429 }
452430 ) ;
453431 }
454- }
432+ } ,
455433
456434 provideLinter ( ) {
457435 console . log ( 'provideLinter nodeBin:' , Config . get ( 'nodeBin' ) ) ;
458436 return {
459437 name : 'ESLint (Node)' ,
460438 scope : 'file' ,
461439 lintsOnChange : true ,
462- grammarScopes : SCOPES ,
440+ grammarScopes : this . scopes ,
463441 lint : async ( textEditor ) => {
464442 console . warn ( 'linting' , textEditor ) ;
465443 if ( ! atom . workspace . isTextEditor ( textEditor ) ) {
@@ -545,8 +523,5 @@ class LinterESLintNode {
545523 }
546524 }
547525 } ;
548- }
549- }
550-
551-
552- export default new LinterESLintNode ( ) ;
526+ } ,
527+ } ;
0 commit comments