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

Commit 4fa53e0

Browse files
authored
Merge pull request #145 from AtomLinter/maintenance
Updates and maintenance
2 parents 6e08070 + 1ea13e7 commit 4fa53e0

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

lib/index.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
44
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');
513

614
const grammarScopes = [];
715

16+
let subscriptions;
17+
818
export function activate() {
919
require('atom-package-deps').install('linter-htmlhint');
1020

11-
const subscriptions = new CompositeDisposable();
21+
subscriptions = new CompositeDisposable();
1222
subscriptions.add(atom.config.observe('linter-htmlhint.enabledScopes', (scopes) => {
1323
// Remove any old scopes
1424
grammarScopes.splice(0, grammarScopes.length);
@@ -17,55 +27,54 @@ export function activate() {
1727
}));
1828
}
1929

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();
3932
}
4033

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+
4147
export function provideLinter() {
4248
return {
4349
name: 'htmlhint',
4450
grammarScopes,
4551
scope: 'file',
4652
lintOnFly: true,
47-
lint: (editor) => {
53+
lint: async (editor) => {
4854
const { HTMLHint } = require('htmlhint');
4955

50-
const text = editor.getText();
56+
const fileText = editor.getText();
5157
const filePath = editor.getPath();
5258

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;
5570
}
5671

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+
}));
6978
}
7079
};
7180
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"atom-linter": "^8.0.0",
4848
"atom-package-deps": "^4.0.1",
4949
"htmlhint": "0.9.13",
50+
"lazy-req": "^1.1.0",
5051
"strip-json-comments": "^2.0.1",
5152
"tiny-promisify": "^0.1.1"
5253
},

0 commit comments

Comments
 (0)