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

Updates and maintenance #145

Merged
merged 4 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 45 additions & 36 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
}));
}
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down