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

Commit e8d70c6

Browse files
authored
Merge pull request #166 from AtomLinter/arcanemagus/updates
Fix a few issues and make several enhancements to the code: * Defer package dependency checking * Guard against invalid `TextEditor`s, and ones without a path. * Refactor to a default export * Move dependency loading to a function
2 parents d8dc5b7 + a48d10c commit e8d70c6

File tree

2 files changed

+109
-62
lines changed

2 files changed

+109
-62
lines changed

lib/index.js

Lines changed: 107 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,126 @@
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';
75

8-
const lazyReq = require('lazy-req')(require);
9-
10-
const { findAsync, generateRange } = lazyReq('atom-linter')('findAsync', 'generateRange');
11-
const stripJSONComments = lazyReq('strip-json-comments');
12-
const tinyPromisify = lazyReq('tiny-promisify');
13-
14-
const grammarScopes = [];
15-
16-
let subscriptions;
17-
18-
export function activate() {
19-
require('atom-package-deps').install('linter-htmlhint');
20-
21-
subscriptions = new CompositeDisposable();
22-
subscriptions.add(atom.config.observe('linter-htmlhint.enabledScopes', (scopes) => {
23-
// Remove any old scopes
24-
grammarScopes.splice(0, grammarScopes.length);
25-
// Add the current scopes
26-
Array.prototype.push.apply(grammarScopes, scopes);
27-
}));
28-
}
29-
30-
export function deactivate() {
31-
subscriptions.dispose();
32-
}
6+
// Dependencies
7+
let dirname;
8+
let HTMLHint;
9+
let findAsync;
10+
let fsReadFile;
11+
let generateRange;
12+
let tinyPromisify;
13+
let stripJSONComments;
3314

3415
const getConfig = async (filePath) => {
35-
const readFile = tinyPromisify()(fsReadFile);
16+
const readFile = tinyPromisify(fsReadFile);
3617
const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
3718
let conf = null;
3819
if (configPath !== null) {
3920
conf = await readFile(configPath, 'utf8');
4021
}
4122
if (conf) {
42-
return JSON.parse(stripJSONComments()(conf));
23+
return JSON.parse(stripJSONComments(conf));
4324
}
4425
return null;
4526
};
4627

47-
export function provideLinter() {
48-
return {
49-
name: 'htmlhint',
50-
grammarScopes,
51-
scope: 'file',
52-
lintOnFly: true,
53-
lint: async (editor) => {
54-
const { HTMLHint } = require('htmlhint');
55-
56-
const fileText = editor.getText();
57-
const filePath = editor.getPath();
28+
const loadDeps = () => {
29+
if (loadDeps.loaded) {
30+
return;
31+
}
32+
if (!dirname) {
33+
({ dirname } = require('path'));
34+
}
35+
if (!HTMLHint) {
36+
({ HTMLHint } = require('htmlhint'));
37+
}
38+
if (!findAsync || !generateRange) {
39+
({ findAsync, generateRange } = require('atom-linter'));
40+
}
41+
if (!fsReadFile) {
42+
({ readFile: fsReadFile } = require('fs'));
43+
}
44+
if (!tinyPromisify) {
45+
tinyPromisify = require('tiny-promisify');
46+
}
47+
if (!stripJSONComments) {
48+
stripJSONComments = require('strip-json-comments');
49+
}
50+
loadDeps.loaded = true;
51+
};
5852

59-
if (!fileText) {
60-
return [];
53+
export default {
54+
activate() {
55+
this.idleCallbacks = new Set();
56+
let depsCallbackID;
57+
const installLinterHtmlhintDeps = () => {
58+
this.idleCallbacks.delete(depsCallbackID);
59+
if (!atom.inSpecMode()) {
60+
require('atom-package-deps').install('linter-htmlhint');
6161
}
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;
62+
loadDeps();
63+
};
64+
depsCallbackID = window.requestIdleCallback(installLinterHtmlhintDeps);
65+
this.idleCallbacks.add(depsCallbackID);
66+
67+
this.grammarScopes = [];
68+
69+
this.subscriptions = new CompositeDisposable();
70+
this.subscriptions.add(atom.config.observe('linter-htmlhint.enabledScopes', (scopes) => {
71+
// Remove any old scopes
72+
this.grammarScopes.splice(0, this.grammarScopes.length);
73+
// Add the current scopes
74+
Array.prototype.push.apply(this.grammarScopes, scopes);
75+
}));
76+
},
77+
78+
deactivate() {
79+
this.idleCallbacks.forEach(callbackID => window.cancelIdleCallback(callbackID));
80+
this.idleCallbacks.clear();
81+
this.subscriptions.dispose();
82+
},
83+
84+
provideLinter() {
85+
return {
86+
name: 'htmlhint',
87+
grammarScopes: this.grammarScopes,
88+
scope: 'file',
89+
lintOnFly: true,
90+
lint: async (editor) => {
91+
if (!atom.workspace.isTextEditor(editor)) {
92+
return null;
93+
}
94+
95+
const filePath = editor.getPath();
96+
if (!filePath) {
97+
// Invalid path
98+
return null;
99+
}
100+
101+
const fileText = editor.getText();
102+
if (!fileText) {
103+
return [];
104+
}
105+
106+
// Ensure that all dependencies are loaded
107+
loadDeps();
108+
109+
const ruleset = await getConfig(filePath);
110+
111+
const messages = HTMLHint.verify(fileText, ruleset || undefined);
112+
113+
if (editor.getText() !== fileText) {
114+
// Editor contents have changed, tell Linter not to update
115+
return null;
116+
}
117+
118+
return messages.map(message => ({
119+
range: generateRange(editor, message.line - 1, message.col - 1),
120+
type: message.type,
121+
text: message.message,
122+
filePath
123+
}));
70124
}
71-
72-
return messages.map(message => ({
73-
range: generateRange(editor, message.line - 1, message.col - 1),
74-
type: message.type,
75-
text: message.message,
76-
filePath
77-
}));
78-
}
79-
};
80-
}
125+
};
126+
}
127+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
"homepage": "https://github.com/AtomLinter/linter-htmlhint#readme",
4444
"dependencies": {
4545
"atom-linter": "^10.0.0",
46-
"atom-package-deps": "^4.0.1",
46+
"atom-package-deps": "^4.6.0",
4747
"htmlhint": "0.9.13",
48-
"lazy-req": "^2.0.0",
4948
"strip-json-comments": "^2.0.1",
5049
"tiny-promisify": "^1.0.0"
5150
},
@@ -76,6 +75,7 @@
7675
"atom": true
7776
},
7877
"env": {
78+
"browser": true,
7979
"node": true
8080
}
8181
},

0 commit comments

Comments
 (0)