This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
/
JSLintUtils.js
241 lines (202 loc) · 8.42 KB
/
JSLintUtils.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, JSLINT, PathUtils */
/**
* Allows JSLint to run on the current document and report results in a UI panel.
*
*/
define(function (require, exports, module) {
"use strict";
// Load dependent non-module scripts
require("thirdparty/path-utils/path-utils.min");
require("thirdparty/jslint/jslint");
// Load dependent modules
var Global = require("utils/Global"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
DocumentManager = require("document/DocumentManager"),
EditorManager = require("editor/EditorManager"),
PreferencesManager = require("preferences/PreferencesManager"),
PerfUtils = require("utils/PerfUtils"),
Strings = require("strings"),
AppInit = require("utils/AppInit"),
Resizer = require("utils/Resizer");
var PREFERENCES_CLIENT_ID = module.id,
defaultPrefs = { height: 200, enabled: true };
// Height in pixels of the JSLint panel header. Hardcoded to avoid race
// condition when measuring it on htmlReady
var HEADER_HEIGHT = 27;
/**
* @private
* @type {PreferenceStorage}
*/
var _prefs = null;
/**
* @private
* @type {boolean}
*/
var _enabled = true;
/**
* @return {boolean} Enabled state of JSLint.
*/
function getEnabled() {
return _enabled;
}
/**
* Run JSLint on the current document. Reports results to the main UI. Displays
* a gold star when no errors are found.
*/
function run() {
var currentDoc = DocumentManager.getCurrentDocument();
var perfTimerDOM,
perfTimerLint;
var ext = currentDoc ? PathUtils.filenameExtension(currentDoc.file.fullPath) : "";
var $lintResults = $("#jslint-results");
var $goldStar = $("#gold-star");
if (getEnabled() && /^(\.js|\.htm|\.html)$/i.test(ext)) {
perfTimerLint = PerfUtils.markStart("JSLint linting:\t" + (!currentDoc || currentDoc.file.fullPath));
var text = currentDoc.getText();
// If a line contains only whitespace, remove the whitespace
// This should be doable with a regexp: text.replace(/\r[\x20|\t]+\r/g, "\r\r");,
// but that doesn't work.
var i, arr = text.split("\n");
for (i = 0; i < arr.length; i++) {
if (!arr[i].match(/\S/)) {
arr[i] = "";
}
}
text = arr.join("\n");
var result = JSLINT(text, null);
PerfUtils.addMeasurement(perfTimerLint);
perfTimerDOM = PerfUtils.markStart("JSLint DOM:\t" + (!currentDoc || currentDoc.file.fullPath));
if (!result) {
var $errorTable = $("<table class='zebra-striped condensed-table' />")
.append("<tbody>");
var $selectedRow;
JSLINT.errors.forEach(function (item, i) {
if (item) {
var makeCell = function (content) {
return $("<td/>").text(content);
};
// Add row to error table
var $row = $("<tr/>")
.append(makeCell(item.line))
.append(makeCell(item.reason))
.append(makeCell(item.evidence || ""))
.appendTo($errorTable);
$row.click(function () {
if ($selectedRow) {
$selectedRow.removeClass("selected");
}
$row.addClass("selected");
$selectedRow = $row;
var editor = EditorManager.getCurrentFullEditor();
editor.setCursorPos(item.line - 1, item.character - 1);
EditorManager.focusEditor();
});
}
});
$("#jslint-results .table-container")
.empty()
.append($errorTable);
$lintResults.show();
$goldStar.hide();
} else {
$lintResults.hide();
$goldStar.show();
}
PerfUtils.addMeasurement(perfTimerDOM);
} else {
// JSLint is disabled or does not apply to the current file, hide
// both the results and the gold star
$lintResults.hide();
$goldStar.hide();
}
EditorManager.resizeEditor();
}
/**
* @private
* Update DocumentManager listeners.
*/
function _updateListeners() {
if (_enabled) {
// register our event listeners
$(DocumentManager)
.on("currentDocumentChange.jslint", function () {
run();
})
.on("documentSaved.jslint", function (event, document) {
if (document === DocumentManager.getCurrentDocument()) {
run();
}
});
} else {
$(DocumentManager).off(".jslint");
}
}
function _setEnabled(enabled) {
_enabled = enabled;
CommandManager.get(Commands.TOGGLE_JSLINT).setChecked(_enabled);
_updateListeners();
_prefs.setValue("enabled", _enabled);
// run immediately
run();
}
/**
* Enable or disable JSLint.
* @param {boolean} enabled Enabled state.
*/
function setEnabled(enabled) {
if (_enabled !== enabled) {
_setEnabled(enabled);
}
}
/** Command to toggle enablement */
function _handleToggleJSLint() {
setEnabled(!getEnabled());
}
// Register command handlers
CommandManager.register(Strings.CMD_JSLINT, Commands.TOGGLE_JSLINT, _handleToggleJSLint);
// Init PreferenceStorage
_prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_CLIENT_ID, defaultPrefs);
_setEnabled(_prefs.getValue("enabled"));
// Initialize items dependent on HTML DOM
AppInit.htmlReady(function () {
var height = Math.max(_prefs.getValue("height"), 100),
$jslintResults = $("#jslint-results"),
$jslintContent = $("#jslint-results .table-container");
$jslintResults.height(height);
$jslintContent.height(height - HEADER_HEIGHT);
if (_enabled) {
EditorManager.resizeEditor();
}
$jslintResults.on("panelResizeEnd", function (event, height) {
_prefs.setValue("height", height);
});
});
// Define public API
exports.run = run;
exports.getEnabled = getEnabled;
exports.setEnabled = setEnabled;
});