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

Commit 641fd15

Browse files
committed
Utilize async/await
Simplify the flow of the code quite a bit by using async/await.
1 parent 2aa0631 commit 641fd15

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

lib/index.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

612
const grammarScopes = [];
713

@@ -23,55 +29,44 @@ export function deactivate() {
2329
subscriptions.dispose();
2430
}
2531

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+
};
4643

4744
export function provideLinter() {
4845
return {
4946
name: 'htmlhint',
5047
grammarScopes,
5148
scope: 'file',
5249
lintOnFly: true,
53-
lint: (editor) => {
50+
lint: async (editor) => {
5451
const { HTMLHint } = require('htmlhint');
5552

56-
const text = editor.getText();
53+
const fileText = editor.getText();
5754
const filePath = editor.getPath();
5855

59-
if (!text) {
60-
return Promise.resolve([]);
56+
if (!fileText) {
57+
return [];
6158
}
6259

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);
6763

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+
}));
7570
}
7671
};
7772
}

0 commit comments

Comments
 (0)