-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
87 lines (74 loc) · 3.14 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
define(function (require, exports, module) {
"use strict";
var CodeInspection = brackets.getModule('language/CodeInspection');
var LanguageManager = brackets.getModule('language/LanguageManager');
var ProjectManager = brackets.getModule('project/ProjectManager');
var ExtensionUtils = brackets.getModule('utils/ExtensionUtils');
var FileUtils = brackets.getModule('file/FileUtils');
var NodeDomain = brackets.getModule('utils/NodeDomain');
var SASS_LANGUAGE = LanguageManager.getLanguageForExtension('sass');
var SCSS_LANGUAGE = LanguageManager.getLanguageForExtension('scss');
var LINTER_NAME = 'Sass-Lint';
var DOMAIN_NAME = 'petetnt.brackets-sass-lint';
var TYPES = {
"0": CodeInspection.Type.META,
"1": CodeInspection.Type.WARNING,
"2": CodeInspection.Type.ERROR
};
var nodeDomain = new NodeDomain(DOMAIN_NAME, ExtensionUtils.getModulePath(module, 'domain'));
/**
* Parses errors and warnings from results to CodeInspection formatted array
* @param {object} results - Results object
* @returns {Array} errorsAndWarnings - Array containing CodeInspection formatted errors and warnings
*/
function _parseErrorsAndWarnings(results) {
var messages = results.messages;
var errorsAndWarnings = [];
messages.forEach(function (message) {
var formattedObject = {
pos: {
line: message.line - 1,
ch: message.column - 1
},
message: message.message + ". [" + message.ruleId + "]",
type: TYPES[message.severity]
};
errorsAndWarnings.push(formattedObject);
});
return errorsAndWarnings;
}
/**
* @private
* Lints the given file syncronously
* @param {string} text - Text of the given file
* @param {string} fullPath - Full path to the file
* @returns {$.Deferred} deferred - jQuery deferred promise which resolves to CodeInspection formatted array or error
*/
function _handleLintASync(text, fullPath) {
var deferred = new $.Deferred();
var projectRoot = ProjectManager.getProjectRoot().fullPath;
var fileName = FileUtils.getFilenameWithoutExtension(fullPath);
var fileExt = FileUtils.getFileExtension(fullPath);
// Execute linting in Node domain
nodeDomain.exec('lintFile', text, fullPath, fileExt, projectRoot)
.then(function (results) {
return deferred.resolve({errors: _parseErrorsAndWarnings(results)});
}, function (err) {
deferred.reject(err);
});
return deferred.promise();
}
// Register linter for Sass
CodeInspection.register(SASS_LANGUAGE.getId(), {
name: LINTER_NAME,
scanFileAsync: _handleLintASync
});
// Register linter for Scss
CodeInspection.register(SCSS_LANGUAGE.getId(), {
name: LINTER_NAME,
scanFileAsync: _handleLintASync
});
// For unit testing
exports._handleLintASync = _handleLintASync;
exports._parseErrorsAndWarnings = _parseErrorsAndWarnings;
});