2
2
3
3
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
4
4
import { CompositeDisposable } from 'atom' ;
5
+ import { readFile as fsReadFile } from 'fs' ;
6
+ import { dirname } from 'path' ;
7
+
8
+ const lazyReq = require ( 'lazy-req' ) ( require ) ;
9
+
10
+ const { findAsync, rangeFromLineNumber } = lazyReq ( 'atom-linter' ) ( 'findAsync' , 'rangeFromLineNumber' ) ;
11
+ const stripJSONComments = lazyReq ( 'strip-json-comments' ) ;
12
+ const tinyPromisify = lazyReq ( 'tiny-promisify' ) ;
5
13
6
14
const grammarScopes = [ ] ;
7
15
16
+ let subscriptions ;
17
+
8
18
export function activate ( ) {
9
19
require ( 'atom-package-deps' ) . install ( 'linter-htmlhint' ) ;
10
20
11
- const subscriptions = new CompositeDisposable ( ) ;
21
+ subscriptions = new CompositeDisposable ( ) ;
12
22
subscriptions . add ( atom . config . observe ( 'linter-htmlhint.enabledScopes' , ( scopes ) => {
13
23
// Remove any old scopes
14
24
grammarScopes . splice ( 0 , grammarScopes . length ) ;
@@ -17,55 +27,54 @@ export function activate() {
17
27
} ) ) ;
18
28
}
19
29
20
- function getConfig ( filePath ) {
21
- const fs = require ( 'fs' ) ;
22
- const path = require ( 'path' ) ;
23
- const readFile = require ( 'tiny-promisify' ) ( fs . readFile ) ;
24
- const { findAsync } = require ( 'atom-linter' ) ;
25
-
26
- return findAsync ( path . dirname ( filePath ) , '.htmlhintrc' )
27
- . then ( ( configPath ) => {
28
- if ( configPath ) {
29
- return readFile ( configPath , 'utf8' ) ;
30
- }
31
- return null ;
32
- } )
33
- . then ( ( conf ) => {
34
- if ( conf ) {
35
- return JSON . parse ( require ( 'strip-json-comments' ) ( conf ) ) ;
36
- }
37
- return null ;
38
- } ) ;
30
+ export function deactivate ( ) {
31
+ subscriptions . dispose ( ) ;
39
32
}
40
33
34
+ const getConfig = async ( filePath ) => {
35
+ const readFile = tinyPromisify ( ) ( fsReadFile ) ;
36
+ const configPath = await findAsync ( dirname ( filePath ) , '.htmlhintrc' ) ;
37
+ let conf = null ;
38
+ if ( configPath !== null ) {
39
+ conf = await readFile ( configPath , 'utf8' ) ;
40
+ }
41
+ if ( conf ) {
42
+ return JSON . parse ( stripJSONComments ( ) ( conf ) ) ;
43
+ }
44
+ return null ;
45
+ } ;
46
+
41
47
export function provideLinter ( ) {
42
48
return {
43
49
name : 'htmlhint' ,
44
50
grammarScopes,
45
51
scope : 'file' ,
46
52
lintOnFly : true ,
47
- lint : ( editor ) => {
53
+ lint : async ( editor ) => {
48
54
const { HTMLHint } = require ( 'htmlhint' ) ;
49
55
50
- const text = editor . getText ( ) ;
56
+ const fileText = editor . getText ( ) ;
51
57
const filePath = editor . getPath ( ) ;
52
58
53
- if ( ! text ) {
54
- return Promise . resolve ( [ ] ) ;
59
+ if ( ! fileText ) {
60
+ return [ ] ;
61
+ }
62
+
63
+ const ruleset = await getConfig ( filePath ) ;
64
+
65
+ const messages = HTMLHint . verify ( fileText , ruleset || undefined ) ;
66
+
67
+ if ( editor . getText ( ) !== fileText ) {
68
+ // Editor contents have changed, tell Linter not to update
69
+ return null ;
55
70
}
56
71
57
- return getConfig ( filePath )
58
- . then ( ruleset => HTMLHint . verify ( text , ruleset || undefined ) )
59
- . then ( ( messages ) => {
60
- const { rangeFromLineNumber } = require ( 'atom-linter' ) ;
61
-
62
- return messages . map ( message => ( {
63
- range : rangeFromLineNumber ( editor , message . line - 1 , message . col - 1 ) ,
64
- type : message . type ,
65
- text : message . message ,
66
- filePath
67
- } ) ) ;
68
- } ) ;
72
+ return messages . map ( message => ( {
73
+ range : rangeFromLineNumber ( editor , message . line - 1 , message . col - 1 ) ,
74
+ type : message . type ,
75
+ text : message . message ,
76
+ filePath
77
+ } ) ) ;
69
78
}
70
79
} ;
71
80
}
0 commit comments