2
2
3
3
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
4
4
import { CompositeDisposable } from 'atom' ;
5
+ import fs from 'fs' ;
6
+ import path from 'path' ;
7
+ import { findAsync , rangeFromLineNumber } from 'atom-linter' ;
8
+ import stripJSONComments from 'strip-json-comments' ;
9
+
10
+ const readFile = require ( 'tiny-promisify' ) ( fs . readFile ) ;
5
11
6
12
const grammarScopes = [ ] ;
7
13
@@ -23,55 +29,44 @@ export function deactivate() {
23
29
subscriptions . dispose ( ) ;
24
30
}
25
31
26
- function getConfig ( filePath ) {
27
- const fs = require ( 'fs' ) ;
28
- const path = require ( 'path' ) ;
29
- const readFile = require ( 'tiny-promisify' ) ( fs . readFile ) ;
30
- const { findAsync } = require ( 'atom-linter' ) ;
31
-
32
- return findAsync ( path . dirname ( filePath ) , '.htmlhintrc' )
33
- . then ( ( configPath ) => {
34
- if ( configPath ) {
35
- return readFile ( configPath , 'utf8' ) ;
36
- }
37
- return null ;
38
- } )
39
- . then ( ( conf ) => {
40
- if ( conf ) {
41
- return JSON . parse ( require ( 'strip-json-comments' ) ( conf ) ) ;
42
- }
43
- return null ;
44
- } ) ;
45
- }
32
+ const getConfig = async ( filePath ) => {
33
+ const configPath = await findAsync ( path . dirname ( filePath ) , '.htmlhintrc' ) ;
34
+ let conf = null ;
35
+ if ( configPath !== null ) {
36
+ conf = await readFile ( configPath , 'utf8' ) ;
37
+ }
38
+ if ( conf ) {
39
+ return JSON . parse ( stripJSONComments ( conf ) ) ;
40
+ }
41
+ return null ;
42
+ } ;
46
43
47
44
export function provideLinter ( ) {
48
45
return {
49
46
name : 'htmlhint' ,
50
47
grammarScopes,
51
48
scope : 'file' ,
52
49
lintOnFly : true ,
53
- lint : ( editor ) => {
50
+ lint : async ( editor ) => {
54
51
const { HTMLHint } = require ( 'htmlhint' ) ;
55
52
56
- const text = editor . getText ( ) ;
53
+ const fileText = editor . getText ( ) ;
57
54
const filePath = editor . getPath ( ) ;
58
55
59
- if ( ! text ) {
60
- return Promise . resolve ( [ ] ) ;
56
+ if ( ! fileText ) {
57
+ return [ ] ;
61
58
}
62
59
63
- return getConfig ( filePath )
64
- . then ( ruleset => HTMLHint . verify ( text , ruleset || undefined ) )
65
- . then ( ( messages ) => {
66
- const { rangeFromLineNumber } = require ( 'atom-linter' ) ;
60
+ const ruleset = await getConfig ( filePath ) ;
61
+
62
+ const messages = HTMLHint . verify ( fileText , ruleset || undefined ) ;
67
63
68
- return messages . map ( message => ( {
69
- range : rangeFromLineNumber ( editor , message . line - 1 , message . col - 1 ) ,
70
- type : message . type ,
71
- text : message . message ,
72
- filePath
73
- } ) ) ;
74
- } ) ;
64
+ return messages . map ( message => ( {
65
+ range : rangeFromLineNumber ( editor , message . line - 1 , message . col - 1 ) ,
66
+ type : message . type ,
67
+ text : message . message ,
68
+ filePath
69
+ } ) ) ;
75
70
}
76
71
} ;
77
72
}
0 commit comments