11'use babel' ;
22
3- import * as path from 'path' ;
4- import { execNode , find , rangeFromLineNumber } from 'atom-linter' ;
5-
63const GRAMMAR_SCOPES = [
74 'text.html.angular' ,
85 'text.html.basic' ,
@@ -14,65 +11,58 @@ const GRAMMAR_SCOPES = [
1411 'text.html.ruby'
1512] ;
1613
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-
2814export function activate ( ) {
2915 require ( 'atom-package-deps' ) . install ( 'linter-htmlhint' ) ;
16+ }
3017
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' ) ;
3223
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+ } ) ;
3637}
3738
3839export function provideLinter ( ) {
3940 return {
4041 name : 'htmlhint' ,
4142 grammarScopes : GRAMMAR_SCOPES ,
4243 scope : 'file' ,
43- lintOnFly : false ,
44+ lintOnFly : true ,
4445 lint : editor => {
46+ const { HTMLHint } = require ( 'htmlhint' ) ;
4547 const text = editor . getText ( ) ;
4648 const filePath = editor . getPath ( ) ;
4749
4850 if ( ! text ) {
4951 return Promise . resolve ( [ ] ) ;
5052 }
5153
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' ) ;
6858
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+ } ) ;
7666 }
7767 } ;
7868}
0 commit comments