-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSPadJSHint.js
664 lines (569 loc) · 21 KB
/
PSPadJSHint.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*==================================================================================================
PSPadJSLint jshint wrapper for pspad, based on jshint.js
Copyright (c) 2008-2011 by PSPadJSHint
Code: http://github.com/PSPadJSHint/pspadjshint
Usage:
* Put this file in the JScripts directory and recompile scripts.
* Create a directory 'library' in the JScripts directory.
* Put a jshint file in the 'library' directory with the name jshint.js.
* Restart PSPad or select Scripts | Recompile scripts.
* Select Scripts | JSHint | Usage for more explanation. (See function usage)
ChangeList:
20111010 Created, version 1.1.1
==================================================================================================*/
/*global
addMenuItem echo inputText sleep pspadVersion getVarValue moduleExists moduleVersion moduleFileName
modulePath closeEditorByTitle closeEditorByIndex closeAllEditors newEditor editorsCount
setClipboardText getClipboardText runPSPadAction activate appendText assignActiveEditor
assignEditorByIndex assignEditorByName assignLog caretX caretY closeFile command fileName lineText
linesCount newFile openFile openFileFromFTP printFile readOnly reloadFile saveFile saveFileAs
saveFileToFTP selText setBlockBegin setBlockEnd blockBeginX blockBeginY blockEndX blockEndY selStart
selLength selectionMode setCaretPos text logClear logAddLine logGetLine logSetLine logLinesCount
projectSave projectFilesCount projectFiles projectFileName ftpConnect ftpDisconnect ftpCommand
ftpDownloadFile ftpUploadFile
ScriptEngine ScriptEngineMajorVersion ActiveXObject Application
JSHINT
*/
/*jshint strict: false wsh:true
*/
var menuName = "JSHint",
module_name = "PSPadJSHint",
module_ver = "3.1.1",
myself = this;
function openScript()
{
var editor = newEditor();
editor.openFile(moduleFileName(module_name));
}
function menuTest()
{
/*jshint newcap:false*/
logClear();
logAddLine(ScriptEngine());
logAddLine(ScriptEngineMajorVersion());
}
function include(name)
{
"use strict";
/*jshint evil: true */
var object, forReading, file, text, includes = myself.includes || [], loaded = false, i, l;
try
{
for (i = 0, l = includes.length; i < l && !loaded; i += 1)
{
if (includes[i] === name)
{
loaded = true;
}
}
if (!loaded)
{
object = new ActiveXObject("Scripting.FileSystemObject");
forReading = 1;
file = object.openTextFile(name, forReading);
text = file.readAll();
file.Close();
eval(text);
includes.push(name);
myself.includes = includes;
myself.JSHINT = JSHINT;
}
}
catch (e)
{
logAddLine(e.message);
}
}
function includeJSHINTJS()
{
var path = modulePath(module_name),
name = 'library\\jshint.js',
filename = path + name;
try
{
include(filename);
this.JSHINT = JSHINT;
}
catch (e)
{
logAddLine(e.message);
}
}
function menuInclude()
{
/*jshint white:false*/
includeJSHINTJS();
if (JSHINT) logAddLine("JSHINT exists");
else logAddLine("JSHINT not found");
}
/*==================================================================================================
JSHint wrapper for PSPad
Now following the code to integrate JSHINT into PsPad.
If there are errors found, they are displayed in the log with file, line and column.
If no errors are found, unused functions and properties are displayed.
==================================================================================================*/
/*jshint indent: 2*/
/*global logAddLine newEditor assignActiveEditor setClipboardText logClear addMenuItem
logSetParser echo logLineIndex logLinesCount runPSPadAction editorsCount getClipboardText
*/
var JSHINTPSPAD = (function ()
{
"use strict";
var logInfoList, itself;
function TLogInfo(afile, aline, acol) {
this.file = afile;
this.line = aline + 1;
this.col = acol + 1;
this.logline = logLinesCount() - 1;
}
logInfoList = {
items: [],
index: 0,
currentItem: function () {
return this.items[this.index];
},
add: function (item) {
this.items.push(item);
},
clear: function () {
this.items = [];
this.index = 0;
},
next: function () {
if (this.index < this.items.length - 1) {
this.index += 1;
}
},
prev: function () {
if (this.index > 0) {
this.index -= 1;
}
}
};
function $defined(obj) {
return (obj !== undefined);
}
itself = function () {
var activeEditor,
source,
fileName,
result,
data,
boolOptions,
key;
function extractFileName(fileName) {
var results = fileName.split('\\');
return results[results.length - 1];
}
function printErrors(data) {
var i, l, item, evidence, reason, line, character;
logAddLine("");
logAddLine(data.length + ' error(s)');
for (i = 0, l = data.length; i < l; i += 1) {
item = data[i];
if (item) {
evidence = (item.evidence || '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
reason = (item.reason || '.');
reason = reason.substring(0, reason.length - 1) + ': ';
line = item.line;
character = item.character;
logAddLine('[Lint] ' + extractFileName(fileName) + '(' + line + ', ' + character + ') : ' + reason + ' ' + evidence);
logInfoList.add(new TLogInfo(fileName, line - 1, character - 1));
}
}
}
function printUnused(data) {
var i, l, item, line, character;
logAddLine("");
logAddLine(data.length + ' unused(s)');
for (i = 0, l = data.length; i < l; i += 1) {
item = data[i];
if (item) {
line = item.line;
character = 1;
logAddLine('[Lint] ' + extractFileName(fileName) + '(' + line + ', ' + character + ') : ' + 'unused variable' + ' ' + item.name + ' in function ' + item['function']);
logInfoList.add(new TLogInfo(fileName, line - 1, character - 1));
}
}
}
function printImplieds(data) {
var i, l, j, k, item, line, character;
logAddLine("");
logAddLine(data.length + ' implied(s)');
for (i = 0, l = data.length; i < l; i += 1) {
item = data[i];
if (item) {
for (j = 0, k = item.line.length; j < k; j += 1) {
line = item.line;
character = 1;
line = item.line[j];
logAddLine('[Lint] ' + extractFileName(fileName) + '(' + line + ', ' + character + ') : ' + 'implied global' + ' ' + item.name);
logInfoList.add(new TLogInfo(fileName, line - 1, character - 1));
}
}
}
}
function printMembers(data) {
var i, list, name, member, info, len;
len = 0;
info = '';
list = [];
for (member in data) {
if (Object.prototype.hasOwnProperty.call(data, member)) {
list.push(member);
}
}
logAddLine('');
logAddLine(list.length + ' members');
if (list.length) {
list.sort();
info += '/*members ';
for (i = 0; i < list.length; i += 1) {
name = list[i].name();
if (len + name.length > 80) {
//clipBoard.push(info);
logAddLine(info);
info = '';
len = 0;
}
if (i < list.length - 1) {
name += ', ';
}
len += name.length;
info += name;
}
info += '*/';
logAddLine(info);
//clipBoard.push(info);
//setClipboardText(clipBoard.join('\n'));
}
}
function printGlobals(data) {
var i, l, list, name, info, len;
logAddLine('');
logAddLine(data.length + ' globals');
len = 0;
info = '';
list = [];
for (i = 0, l = data.length; i < l; i += 1) {
/*jshint forin:true*/
if (typeof data[i] === 'string') {
list.push(data[i]);
}
}
if (list.length) {
list.sort();
info += '/*globals ';
for (i = 0, l = list.length; i < l; i += 1) {
name = list[i].name();
if (len + name.length > 80) {
//clipBoard.push(info);
logAddLine(info);
info = '';
len = 0;
}
if (i < list.length - 1) {
name += ', ';
}
len += name.length;
info += name;
}
info += '*/';
logAddLine(info);
//clipBoard.push(info);
//setClipboardText(clipBoard.join('\n'));
}
}
includeJSHINTJS();
activeEditor = newEditor();
activeEditor.assignActiveEditor();
source = activeEditor.text();
fileName = activeEditor.fileName();
logAddLine("");
logAddLine('parsing ' + fileName);
logInfoList.clear();
boolOptions = {};
for (key in JSHINTPSPAD.useroptions)
{
if (Object.prototype.hasOwnProperty.call(JSHINTPSPAD.useroptions, key))
{
boolOptions[key] = JSHINTPSPAD.useroptions[key];
}
}
result = JSHINT(source, boolOptions);
data = JSHINT.data();
if ($defined(data.errors)) {
printErrors(data.errors);
result = false;
}
if ($defined(data.unused)) {
printUnused(data.unused);
result = false;
}
if ($defined(data.implieds)) {
printImplieds(data.implieds);
result = false;
}
if (result) {
logAddLine('');
logAddLine('no errors found');
if ($defined(data.globals)) {
printGlobals(data.globals);
}
if ($defined(data.member)) {
printMembers(data.member);
}
}
logSetParser('[Lint] %F(%L, %C) : ');
return result;
};
itself.currentLogInfo = function () {
var ed, loginfo;
loginfo = logInfoList.currentItem();
if (loginfo) {
ed = newEditor();
ed.assignEditorByName(loginfo.file);
ed.activate();
ed.setCaretPos(loginfo.col, loginfo.line);
logLineIndex(loginfo.logline);
}
};
itself.nextLogInfo = function () {
if (logInfoList.items.length === 0) {
logClear();
itself();
}
logInfoList.next();
this.currentLogInfo();
};
itself.previousLogInfo = function () {
if (logInfoList.items.length === 0) {
logClear();
itself();
}
logInfoList.prev();
this.currentLogInfo();
};
return itself;
}());
/*==================================================================================================
PSPad menu functions
==================================================================================================*/
function jSHintCurrentFile() {
"use strict";
logClear();
try
{
JSHINTPSPAD();
JSHINTPSPAD.currentLogInfo();
}
catch (e)
{
logAddLine('jSHintCurrentFile: ' + e.message);
}
}
function jSHintPrev() {
"use strict";
JSHINTPSPAD.previousLogInfo();
}
function jSHintNext() {
"use strict";
JSHINTPSPAD.nextLogInfo();
}
function jSHintFile(editor, close) {
"use strict";
var result = true, fileName, pos;
try
{
fileName = editor.fileName();
pos = fileName.search(/\.js/i);
if (pos !== -1 && pos === fileName.length - 3) {
logAddLine(editor.fileName());
editor.activate();
result = JSHINTPSPAD();
if (result && close) {
editor.closeFile();
}
} else if (close) {
editor.closeFile();
}
} catch (e) {
logAddLine(e.message);
}
return result;
}
function jSHintAbout() {
"use strict";
var info, edition;
includeJSHINTJS();
edition = JSHINT.edition;
info = '';
info += 'About JSHintPSPad\n';
info += '\n';
info += 'Version ' + module_ver + '.\n';
info += 'Using jshint from ' + edition + ' (www.jshint.com).\n';
info += '\n';
info += 'Perform jshint on javascript files and browse results.\n';
echo(info);
}
function jSHintUsage() {
"use strict";
var info = '';
info += '---- Usage --------------------------------------------------------------------------------------------------------------------\n';
info += '\n';
info += 'Open a JavaScript file and run JSHint.\n';
info += 'A list with \'[Lint]\' messages is put in the Log.\n';
info += 'You can select previous and next [Lint] message with Shift-Alt-Up and ~Down.\n';
info += 'You can click on the [Lint] message line and the cursor jumps to the line.\n';
info += '\n';
info += 'Alternatively you can run jshint on all open files. All files are saved first.\n';
info += 'Non javascript files are closed. Jshint is performed on javascript files.\n';
info += 'On success, the file is closed and the next file is processed.\n';
info += 'On error, the process stops.\n';
info += '\n';
info += '\n';
info += '---- Functions ----------------------------------------------------------------------------------------------------------------\n';
info += '\n';
info += '- JSHint All Open Files perform JSHint on all open files and close after success.\n';
info += '- JSHint All Open Files and Close (shift+ctrl+alt+F9) perform JSHint on all open files.\n';
info += '- JSHint (ctrl+alt+F9) perform JSHint on current file in editor.\n';
info += '- Prev JSHint (ctrl+alt+up) goto the previous JSHint error.\n';
info += '- Next JSHint (ctrl+alt+down) goto the next JSHint error.\n';
info += '- JSHint Usage explains how to use these functions.\n';
info += '- JSHint About displays the JSHint version number.\n';
info += '- Edit JSHint Script File opens this file in PSPAd.\n';
info += '\n';
info += '\n';
echo(info);
}
function openScript() {
"use strict";
var editor = newEditor();
editor.openFile(moduleFileName(module_name));
}
function dojSHintAllOpenFiles(close) {
"use strict";
var i, editor, error, count, assigned;
runPSPadAction('aSaveAll');
editor = newEditor();
error = false;
count = editorsCount();
for (i = 0; i < count && !error; i += 1)
{
assigned = editor.assignEditorByIndex(i);
if (assigned)
{
logClear();
error = !jSHintFile(editor, close);
}
}
}
function jSHintAllOpenFiles() {
"use strict";
dojSHintAllOpenFiles(false);
}
function jSHintAllOpenFilesClose() {
"use strict";
dojSHintAllOpenFiles(true);
}
(function () {
"use strict";
/*jshint white:false*/
JSHINTPSPAD.useroptions = {
asi : false, // if automatic semicolon insertion should be tolerated
bitwise : true, // if bitwise operators should not be allowed
boss : false, // if advanced usage of assignments should be allowed
browser : true, // if the standard browser globals should be predefined
camelcase : false, // if identifiers should be required in camel case
couch : false, // if CouchDB globals should be predefined
curly : false, // if curly braces around blocks should be required (even in if/for/while)
debug : false, // if debugger statements should be allowed
devel : false, // if logging globals should be predefined (console, alert, etc.)
dojo : false, // if Dojo Toolkit globals should be predefined
eqeqeq : true, // if === should be required
eqnull : false, // if == null comparisons should be tolerated
es5 : true, // if ES5 syntax should be allowed
esnext : true, // if es.next specific syntax should be allowed
evil : false, // if eval should be allowed
expr : false, // if ExpressionStatement should be allowed as Programs
forin : true, // if for in statements must filter
funcscope : true, // if only function scope should be used for scope tests
globalstrict: false, // if global "use strict"; should be allowed (also enables 'strict')
immed : true, // if immediate invocations must be wrapped in parens
iterator : true, // if the `__iterator__` property should be allowed
jquery : false, // if jQuery globals should be predefined
lastsemic : false, // if semicolons may be ommitted for the trailing statements inside of a one-line blocks.
latedef : true, // if the use before definition should not be tolerated
laxbreak : false, // if line breaks should not be checked
laxcomma : true, // if line breaks should not be checked around commas
loopfunc : false, // if functions should be allowed to be defined within loops
mootools : true, // if MooTools globals should be predefined
multistr : true, // allow multiline strings
newcap : true, // if constructor names must be capitalized
noarg : true, // if arguments.caller and arguments.callee should be disallowed
node : true, // if the Node.js environment globals should be predefined
noempty : true, // if empty blocks should be disallowed
nomen : false,// if names should be checked
nonew : true, // if using `new` for side-effects should be disallowed
nonstandard : false, // if non-standard (but widely adopted) globals should be predefined
onevar : true, // if only one var statement per function should be allowed
onecase : true, // if one case switch statements should be allowed
passfail : false, // if the scan should stop on first error
plusplus : false, // if increment/decrement should not be allowed
proto : true, // if the `__proto__` property should be allowed
prototypejs : false, // if Prototype and Scriptaculous globals should be predefined
regexdash : true, // if unescaped last dash (-) inside brackets should be tolerated
regexp : false, // if the . should not be allowed in regexp literals
rhino : false, // if the Rhino environment globals should be predefined
scripturl : true, // if script-targeted URLs should be tolerated
shadow : false, // if variable shadowing should be tolerated
smarttabs : false, // if smarttabs should be tolerated (http://www.emacswiki.org/emacs/SmartTabs)
strict : true, // require the "use strict"; pragma
sub : false, // if all forms of subscript notation are tolerated
supernew : true, // if `new function () { ... };` and `new Object;` should be tolerated
trailing : false,// if trailing whitespace rules apply
undef : true, // if variables should be declared before used
unused : true, // if variables should be always used
validthis : true, // if 'this' inside a non-constructor function is valid. This is a function scoped option only.
withstmt : true, // if with statements should be allowed
white : true, // if strict whitespace rules apply
worker : true, // if Web Worker script symbols should be allowed
wsh : false, // if the Windows Scripting Host environment globals should be predefined
'maxerr' : 50, //This option is used to specify maximum number of errors before JSHint stops processing your source. Default value is 50.
'indent' : 2 // The number of spaces used for indentation (default is 4)
//'predef' : ['Array', 'Function', 'MooTools', 'etc'];
//'predef' : {'Array': true, 'Function': false, 'Mootools': true, 'etc': false};
};
}());
/*==================================================================================================
Description Init function needed by pspad. It put the functions on the Scripts menu together
with shortcuts.
==================================================================================================*/
function Init()
{
"use strict";
var idTag, cbText;
idTag = "<" + menuName + ">";
cbText = getClipboardText();
if (cbText.indexOf(idTag) === -1)
{
addMenuItem("JSHint", menuName, "jSHintCurrentFile", "Ctrl+Alt+F9");
addMenuItem("Prev JSHint", menuName, "jSHintPrev", "Ctrl+Alt+Up");
addMenuItem("Next JSHint", menuName, "jSHintNext", "Ctrl+Alt+Down");
addMenuItem("-", menuName, "");
addMenuItem("JSHint All Open Files", menuName, "jSHintAllOpenFiles");
addMenuItem("JSHint All Open Files and Close", menuName, "jSHintAllOpenFilesClose", "Shift+Ctrl+Alt+F9");
addMenuItem("-", menuName, "");
addMenuItem("JSHint Usage", menuName, "jSHintUsage");
addMenuItem("JSHint About", menuName, "jSHintAbout");
addMenuItem("-", menuName, "");
addMenuItem("Edit JSHint Script File", menuName, "openScript");
addMenuItem("-", menuName, "");
setClipboardText(cbText + idTag);
}
else
{
cbText = cbText.replace(idTag, "");
setClipboardText(cbText);
}
}