-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.js
173 lines (141 loc) · 5.31 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, CSSLint */
define(function (require, exports, module) {
"use strict";
var DocumentManager = brackets.getModule("document/DocumentManager"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
CodeInspection = brackets.getModule("language/CodeInspection"),
LanguageManager = brackets.getModule("language/LanguageManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
AppInit = brackets.getModule("utils/AppInit");
var pm = PreferencesManager.getExtensionPrefs("csslint"),
defaults;
pm.definePreference("options", "object", {})
.on("change", function () {
defaults = pm.get("options");
});
defaults = pm.get("options");
require("csslint/csslint");
var _configFileName = ".csslintrc",
config = {};
function cssLinter(text) {
var results;
// Merge default CSSLint ruleset with the custom .csslintrc config
var ruleset = $.extend(CSSLint.getRuleset(), defaults, config.options);
// Execute CSSLint
results = CSSLint.verify(text, ruleset);
if (results.messages.length) {
var result = {
errors: []
};
for (var i = 0, len = results.messages.length; i < len; i++) {
/*
Currently the Brackets Lint API doesn't work with warnings that are 'document' level.
See bug: https://github.com/adobe/brackets/issues/5452
*/
var messageOb = results.messages[i];
if (!messageOb.line) continue;
//default
var type = CodeInspection.Type.WARNING;
if (messageOb.type === "error") {
type = CodeInspection.Type.ERROR;
} else if (messageOb.type === "warning") {
type = CodeInspection.Type.WARNING;
}
var message = messageOb.rule.name + " - " + messageOb.message;
message += " (" + messageOb.rule.id + ")";
result.errors.push({
pos: {
line: messageOb.line - 1,
ch: messageOb.col
},
message: message,
type: type
});
}
return result;
} else {
//no errors
return null;
}
}
/**
* Loads project-wide CSSLint configuration.
*
* CSSLint project file should be located at <Project Root>/.csslintrc. It
* is loaded each time project is changed or the configuration file is
* modified.
*
* @return Promise to return CSSLint configuration object.
*
*/
function _loadProjectConfig() {
var projectRootEntry = ProjectManager.getProjectRoot(),
result = new $.Deferred(),
file,
config;
file = FileSystem.getFileForPath(projectRootEntry.fullPath + _configFileName);
file.read(function (err, content) {
if (!err) {
var cfg = {};
try {
config = JSON.parse(content);
} catch (e) {
console.error("CSSLint: Error parsing " + file.fullPath + ". Details: " + e);
result.reject(e);
return;
}
cfg.options = config;
result.resolve(cfg);
} else {
result.reject(err);
}
});
return result.promise();
}
/**
* Attempts to load project configuration file.
*/
function tryLoadConfig() {
/**
* Makes sure CSSLint is re-ran when the config is reloaded
*
* This is a workaround due to some loading issues in Sprint 31.
* See bug for details: https://github.com/adobe/brackets/issues/5442
*/
function _refreshCodeInspection() {
CodeInspection.toggleEnabled();
CodeInspection.toggleEnabled();
}
_loadProjectConfig()
.done(function (newConfig) {
config = newConfig;
})
.fail(function () {
config = {};
})
.always(function () {
_refreshCodeInspection();
});
}
AppInit.appReady(function () {
LanguageManager.getLanguage("json").addFileName(_configFileName);
CodeInspection.register("css", {
name: "CSSLint",
scanFile: cssLinter
});
DocumentManager
.on("documentSaved.csslint documentRefreshed.csslint", function (e, document) {
// if this project's .csslintrc config has been updated, reload
if (document.file.fullPath === ProjectManager.getProjectRoot().fullPath + _configFileName) {
tryLoadConfig();
}
});
ProjectManager
.on("projectOpen.csslint", function () {
tryLoadConfig();
});
tryLoadConfig();
});
});