This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathmain.js
More file actions
144 lines (120 loc) · 5.09 KB
/
main.js
File metadata and controls
144 lines (120 loc) · 5.09 KB
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
/*
* 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.
*
*/
/*global define, $, JSLINT, brackets */
/**
* Provides JSLint results via the core linting extension point
*/
define(function (require, exports, module) {
"use strict";
// Load JSLint, a non-module lib
require("thirdparty/jslint/jslint");
// Load dependent modules
var CodeInspection = brackets.getModule("language/CodeInspection"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Strings = brackets.getModule("strings"),
_ = brackets.getModule("thirdparty/lodash");
var prefs = PreferencesManager.getExtensionPrefs("jslint");
/**
* @private
*
* Used to keep track of the last options JSLint was run with to avoid running
* again when there were no changes.
*/
var _lastRunOptions;
prefs.definePreference("options", "object")
.on("change", function (e, data) {
var options = prefs.get("options");
if (!_.isEqual(options, _lastRunOptions)) {
CodeInspection.requestRun(Strings.JSLINT_NAME);
}
});
// Predefined environments understood by JSLint.
var ENVIRONMENTS = ["browser", "node", "couch", "rhino"];
// gets indentation size depending whether the tabs or spaces are used
function _getIndentSize(fullPath) {
return PreferencesManager.get(
PreferencesManager.get("useTabChar", fullPath) ? "tabSize" : "spaceUnits",
fullPath
);
}
/**
* Run JSLint on the current document. Reports results to the main UI. Displays
* a gold star when no errors are found.
*/
function lintOneFile(text, fullPath) {
// 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 options = prefs.get("options");
_lastRunOptions = _.clone(options);
if (!options) {
options = {};
} else {
options = _.clone(options);
}
if (!options.indent) {
// default to using the same indentation value that the editor is using
options.indent = _getIndentSize(fullPath);
}
// If the user has not defined the environment, we use browser by default.
var hasEnvironment = _.some(ENVIRONMENTS, function (env) {
return options[env] !== undefined;
});
if (!hasEnvironment) {
options.browser = true;
}
var jslintResult = JSLINT(text, options);
if (!jslintResult) {
// Remove any trailing null placeholder (early-abort indicator)
var errors = JSLINT.errors.filter(function (err) { return err !== null; });
errors = errors.map(function (jslintError) {
return {
// JSLint returns 1-based line/col numbers
pos: { line: jslintError.line - 1, ch: jslintError.character - 1 },
message: jslintError.reason,
type: CodeInspection.Type.WARNING
};
});
var result = { errors: errors };
// If array terminated in a null it means there was a stop notice
if (errors.length !== JSLINT.errors.length) {
result.aborted = true;
errors[errors.length - 1].type = CodeInspection.Type.META;
}
return result;
}
return null;
}
// Register for JS files
CodeInspection.register("javascript", {
name: Strings.JSLINT_NAME,
scanFile: lintOneFile
});
});