|
2 | 2 |
|
3 | 3 | // eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
|
4 | 4 | import { CompositeDisposable } from 'atom';
|
5 |
| -import { readFile as fsReadFile } from 'fs'; |
6 |
| -import { dirname } from 'path'; |
7 | 5 |
|
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; |
33 | 14 |
|
34 | 15 | const getConfig = async (filePath) => {
|
35 |
| - const readFile = tinyPromisify()(fsReadFile); |
| 16 | + const readFile = tinyPromisify(fsReadFile); |
36 | 17 | const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
|
37 | 18 | let conf = null;
|
38 | 19 | if (configPath !== null) {
|
39 | 20 | conf = await readFile(configPath, 'utf8');
|
40 | 21 | }
|
41 | 22 | if (conf) {
|
42 |
| - return JSON.parse(stripJSONComments()(conf)); |
| 23 | + return JSON.parse(stripJSONComments(conf)); |
43 | 24 | }
|
44 | 25 | return null;
|
45 | 26 | };
|
46 | 27 |
|
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 | +}; |
58 | 52 |
|
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'); |
61 | 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; |
| 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 | + })); |
70 | 124 | }
|
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 | +}; |
0 commit comments