diff --git a/lib/index.js b/lib/index.js index 439f19e..767f5ad 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,13 +2,23 @@ // eslint-disable-next-line import/extensions, import/no-extraneous-dependencies import { CompositeDisposable } from 'atom'; +import { readFile as fsReadFile } from 'fs'; +import { dirname } from 'path'; + +const lazyReq = require('lazy-req')(require); + +const { findAsync, rangeFromLineNumber } = lazyReq('atom-linter')('findAsync', 'rangeFromLineNumber'); +const stripJSONComments = lazyReq('strip-json-comments'); +const tinyPromisify = lazyReq('tiny-promisify'); const grammarScopes = []; +let subscriptions; + export function activate() { require('atom-package-deps').install('linter-htmlhint'); - const subscriptions = new CompositeDisposable(); + subscriptions = new CompositeDisposable(); subscriptions.add(atom.config.observe('linter-htmlhint.enabledScopes', (scopes) => { // Remove any old scopes grammarScopes.splice(0, grammarScopes.length); @@ -17,55 +27,54 @@ export function activate() { })); } -function getConfig(filePath) { - const fs = require('fs'); - const path = require('path'); - const readFile = require('tiny-promisify')(fs.readFile); - const { findAsync } = require('atom-linter'); - - return findAsync(path.dirname(filePath), '.htmlhintrc') - .then((configPath) => { - if (configPath) { - return readFile(configPath, 'utf8'); - } - return null; - }) - .then((conf) => { - if (conf) { - return JSON.parse(require('strip-json-comments')(conf)); - } - return null; - }); +export function deactivate() { + subscriptions.dispose(); } +const getConfig = async (filePath) => { + const readFile = tinyPromisify()(fsReadFile); + const configPath = await findAsync(dirname(filePath), '.htmlhintrc'); + let conf = null; + if (configPath !== null) { + conf = await readFile(configPath, 'utf8'); + } + if (conf) { + return JSON.parse(stripJSONComments()(conf)); + } + return null; +}; + export function provideLinter() { return { name: 'htmlhint', grammarScopes, scope: 'file', lintOnFly: true, - lint: (editor) => { + lint: async (editor) => { const { HTMLHint } = require('htmlhint'); - const text = editor.getText(); + const fileText = editor.getText(); const filePath = editor.getPath(); - if (!text) { - return Promise.resolve([]); + if (!fileText) { + return []; + } + + const ruleset = await getConfig(filePath); + + const messages = HTMLHint.verify(fileText, ruleset || undefined); + + if (editor.getText() !== fileText) { + // Editor contents have changed, tell Linter not to update + return null; } - return getConfig(filePath) - .then(ruleset => HTMLHint.verify(text, ruleset || undefined)) - .then((messages) => { - const { rangeFromLineNumber } = require('atom-linter'); - - return messages.map(message => ({ - range: rangeFromLineNumber(editor, message.line - 1, message.col - 1), - type: message.type, - text: message.message, - filePath - })); - }); + return messages.map(message => ({ + range: rangeFromLineNumber(editor, message.line - 1, message.col - 1), + type: message.type, + text: message.message, + filePath + })); } }; } diff --git a/package.json b/package.json index a9b5deb..fa47603 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "atom-linter": "^8.0.0", "atom-package-deps": "^4.0.1", "htmlhint": "0.9.13", + "lazy-req": "^1.1.0", "strip-json-comments": "^2.0.1", "tiny-promisify": "^0.1.1" },