1
1
'use babel' ;
2
2
3
- import * as path from 'path' ;
4
- import { execNode , find , rangeFromLineNumber } from 'atom-linter' ;
5
-
6
3
const GRAMMAR_SCOPES = [
7
4
'text.html.angular' ,
8
5
'text.html.basic' ,
@@ -14,65 +11,58 @@ const GRAMMAR_SCOPES = [
14
11
'text.html.ruby'
15
12
] ;
16
13
17
- export const config = {
18
- executablePath : {
19
- title : 'Executable Path' ,
20
- description : 'HTMLHint Node Script Path' ,
21
- type : 'string' ,
22
- default : path . join ( __dirname , '..' , 'node_modules' , 'htmlhint' , 'bin' , 'htmlhint' )
23
- }
24
- } ;
25
-
26
- let executablePath = '' ;
27
-
28
14
export function activate ( ) {
29
15
require ( 'atom-package-deps' ) . install ( 'linter-htmlhint' ) ;
16
+ }
30
17
31
- executablePath = atom . config . get ( 'linter-htmlhint.executablePath' ) ;
18
+ function getConfig ( filePath ) {
19
+ const fs = require ( 'fs' ) ;
20
+ const path = require ( 'path' ) ;
21
+ const readFile = require ( 'tiny-promisify' ) ( fs . readFile ) ;
22
+ const { findAsync } = require ( 'atom-linter' ) ;
32
23
33
- atom . config . observe ( 'linter-htmlhint.executablePath' , newValue => {
34
- executablePath = newValue ;
35
- } ) ;
24
+ return findAsync ( path . dirname ( filePath ) , '.htmlhintrc' )
25
+ . then ( configPath => {
26
+ if ( configPath ) {
27
+ return readFile ( configPath , 'utf8' ) ;
28
+ }
29
+ return null ;
30
+ } )
31
+ . then ( conf => {
32
+ if ( conf ) {
33
+ return JSON . parse ( require ( 'strip-json-comments' ) ( conf ) ) ;
34
+ }
35
+ return null ;
36
+ } ) ;
36
37
}
37
38
38
39
export function provideLinter ( ) {
39
40
return {
40
41
name : 'htmlhint' ,
41
42
grammarScopes : GRAMMAR_SCOPES ,
42
43
scope : 'file' ,
43
- lintOnFly : false ,
44
+ lintOnFly : true ,
44
45
lint : editor => {
46
+ const { HTMLHint } = require ( 'htmlhint' ) ;
45
47
const text = editor . getText ( ) ;
46
48
const filePath = editor . getPath ( ) ;
47
49
48
50
if ( ! text ) {
49
51
return Promise . resolve ( [ ] ) ;
50
52
}
51
53
52
- const parameters = [ filePath , '--format' , 'json' ] ;
53
- const htmlhintrc = find ( path . dirname ( filePath ) , '.htmlhintrc' ) ;
54
-
55
- if ( htmlhintrc ) {
56
- parameters . push ( '-c' ) ;
57
- parameters . push ( htmlhintrc ) ;
58
- }
59
-
60
- return execNode ( executablePath , parameters , { } ) . then ( output => {
61
- const results = JSON . parse ( output ) ;
62
-
63
- if ( ! results . length ) {
64
- return [ ] ;
65
- }
66
-
67
- const messages = results [ 0 ] . messages ;
54
+ return getConfig ( filePath )
55
+ . then ( ruleset => HTMLHint . verify ( text , ruleset || undefined ) )
56
+ . then ( messages => {
57
+ const { rangeFromLineNumber } = require ( 'atom-linter' ) ;
68
58
69
- return messages . map ( message => ( {
70
- range : rangeFromLineNumber ( editor , message . line - 1 , message . col - 1 ) ,
71
- type : message . type ,
72
- text : message . message ,
73
- filePath
74
- } ) ) ;
75
- } ) ;
59
+ return messages . map ( message => ( {
60
+ range : rangeFromLineNumber ( editor , message . line - 1 , message . col - 1 ) ,
61
+ type : message . type ,
62
+ text : message . message ,
63
+ filePath
64
+ } ) ) ;
65
+ } ) ;
76
66
}
77
67
} ;
78
68
}
0 commit comments