This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
114 lines (94 loc) · 2.79 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
105
106
107
108
109
110
111
112
113
114
'use babel';
import fs from 'fs';
import { Range } from 'atom';
import postcss from 'postcss';
import stylelint from 'stylelint';
import helper from 'atom-linter';
import assign from 'deep-assign';
export let config = {
usePreset: {
title: 'Use preset',
description: 'Use preset lint config',
type: 'boolean',
default: true
},
presetConfig: {
title: 'Preset config',
description: 'Select lint style if you select from presets',
type: 'string',
default: 'stylelint-config-suitcss',
enum: ['stylelint-config-suitcss', 'stylelint-config-cssrecipes', 'stylelint-config-wordpress']
}
};
const usePreset = () => atom.config.get('linter-stylelint.usePreset');
const presetConfig = () => atom.config.get('linter-stylelint.presetConfig');
const configFiles = ['.stylelintrc'];
export const activate = () => {
require("atom-package-deps").install("linter-stylelint");
};
const getConfig = (configFile) => {
let fileContents = fs.readFileSync(configFile);
let config;
try {
config = JSON.parse(fileContents);
} catch (e) {
config = require(configFile);
}
return config;
};
export const provideLinter = () => {
let preset = require(presetConfig());
return {
name: 'stylelint',
grammarScopes: ['source.css'],
scope: 'file',
lintOnFly: true,
lint: (editor) => {
let path = editor.getPath();
let text = editor.getText();
let config = usePreset() ? preset : {};
// .stylelintrc is preferred if exists
let configFile = helper.findFile(path, configFiles);
if (configFile) {
try {
let stylelintrc = getConfig(configFile);
config = assign(config, stylelintrc);
} catch (e) {
atom.notifications.addWarning(`Invalid .stylelintrc`, {
detail: `Failed to parse .stylelintrc JSON`,
dismissable: true
});
console.error(error);
}
}
return new Promise((resolve, reject) => {
postcss([
stylelint(config)
]).process(text, {
from: path
}).then((data) => {
resolve(data.messages.map(message => {
let range = new Range(
[message.line - 1, message.column - 1],
[message.line - 1, message.column + 1000]
);
return {
type: message.type,
text: message.text,
filePath: path,
range: range
};
}));
}).catch(error => {
if (error.line && error.reason) {
atom.notifications.addWarning(`CSS Syntax Error`, {
detail: `${error.reason} on line ${error.line}`,
dismissable: true
});
}
console.error(error);
});
});
}
};
};