Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 126af70

Browse files
download13Arcanemagus
authored andcommitted
No subprocess (#112)
* Linter now directly imports htmlhint instead of running it as a subprocess. lintOnFly can be enabled as a result. No need for executable path config is there's no executable. * Froze the htmlhint dependency version. We are depending on an internal API. Incompatibilities could be introduced at any time.
1 parent 2ae211b commit 126af70

File tree

2 files changed

+35
-43
lines changed

2 files changed

+35
-43
lines changed

lib/index.js

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use babel';
22

3-
import * as path from 'path';
4-
import { execNode, find, rangeFromLineNumber } from 'atom-linter';
5-
63
const 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-
2814
export 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

3839
export 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
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
"dependencies": {
2929
"atom-linter": "^4.6.1",
3030
"atom-package-deps": "^4.0.1",
31-
"htmlhint": "~0.9.12"
31+
"htmlhint": "0.9.12",
32+
"strip-json-comments": "^2.0.1",
33+
"tiny-promisify": "^0.1.1"
3234
},
3335
"devDependencies": {
3436
"eslint": "^2.9.0",

0 commit comments

Comments
 (0)