-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
104 lines (85 loc) · 3.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use babel';
import { findCachedAsync, generateRange } from 'atom-linter';
import { Lesshint } from 'lesshint';
import path from 'path';
import os from 'os';
export default class LinterLesshint {
static config = {
onlyWithRc: {
default: false,
title: 'Disable linter when no `.lesshintrc` is found in project.',
type: 'boolean',
},
globalConfig: {
default: false,
title: 'Use a global configuration file?',
type: 'boolean',
},
globalConfigDir: {
default: os.homedir(),
title: 'Path to directory of global configuration file',
type: 'string',
}
}
static get onlyWithRc () {
return atom.config.get('linter-lesshint.onlyWithRc');
}
static get globalConfigDir () {
return atom.config.get('linter-lesshint.globalConfigDir');
}
static get globalConfig () {
return atom.config.get('linter-lesshint.globalConfig');
}
static activate () {
require('atom-package-deps').install('linter-lesshint');
}
static provideLinter () {
return {
name: 'lesshint',
grammarScopes: ['source.css.less'],
scope: 'file',
lintsOnChange: true,
lint: async (editor) => {
const lesshint = new Lesshint();
const filePath = editor.getPath();
let configFile = await findCachedAsync(path.dirname(filePath), '.lesshintrc');
if (!configFile && this.globalConfig) {
configFile = await findCachedAsync(this.globalConfigDir, '.lesshintrc');
}
if (!configFile && this.onlyWithRc) {
return [];
}
let config = {};
try {
if (configFile) {
config = lesshint.getConfig(configFile);
}
} catch (e) {
atom.notifications.addError("Something's wrong with the `.lesshintrc` file.", {
dismissable: true,
});
}
lesshint.configure(config);
let errors = [];
try {
const text = editor.getText();
errors = lesshint.checkString(text, filePath);
} catch (e) {
atom.notifications.addError("lesshint couldn't check this file.", {
detail: e.stack,
dismissable: true
});
}
return errors.map(({ linter, message, line, column, severity }) => {
line = line || editor.getLineCount();
const excerpt = `${linter}: ${message}`;
const location = {
file: filePath,
position: generateRange(editor, line - 1, column - 1),
};
return { excerpt, location, severity };
});
}
};
}
}