Skip to content

Commit 70c615c

Browse files
committed
[lint addon] Make sure options meant for the lint addon aren't passed to the inner linter
Since JSHint apparently complains about such unknown options. Closes #6724 Closes #6729
1 parent ddbdce0 commit 70c615c

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

addon/lint/lint.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,37 @@
5959
CodeMirror.on(node, "mouseout", hide);
6060
}
6161

62-
function LintState(cm, options, hasGutter) {
62+
function LintState(cm, conf, hasGutter) {
6363
this.marked = [];
64-
this.options = options;
64+
if (conf instanceof Function) conf = {getAnnotations: conf};
65+
if (!conf || conf === true) conf = {};
66+
this.options = {};
67+
this.linterOptions = conf.options || {};
68+
for (var prop in defaults) this.options[prop] = defaults[prop];
69+
for (var prop in conf) {
70+
if (defaults.hasOwnProperty(prop)) {
71+
if (conf[prop] != null) this.options[prop] = conf[prop];
72+
} else if (!conf.options) {
73+
this.linterOptions[prop] = conf[prop];
74+
}
75+
}
6576
this.timeout = null;
6677
this.hasGutter = hasGutter;
6778
this.onMouseOver = function(e) { onMouseOver(cm, e); };
6879
this.waitingFor = 0
6980
}
7081

71-
function parseOptions(_cm, options) {
72-
if (options instanceof Function) return {getAnnotations: options};
73-
if (!options || options === true) options = {};
74-
return options;
82+
var defaults = {
83+
highlightLines: false,
84+
tooltips: true,
85+
delay: 500,
86+
lintOnChange: true
7587
}
7688

7789
function clearMarks(cm) {
7890
var state = cm.state.lint;
7991
if (state.hasGutter) cm.clearGutter(GUTTER_ID);
80-
if (isHighlightErrorLinesEnabled(state)) clearErrorLines(cm);
92+
if (state.options.highlightLines) clearErrorLines(cm);
8193
for (var i = 0; i < state.marked.length; ++i)
8294
state.marked[i].clear();
8395
state.marked.length = 0;
@@ -90,10 +102,6 @@
90102
})
91103
}
92104

93-
function isHighlightErrorLinesEnabled(state) {
94-
return state.options.highlightLines;
95-
}
96-
97105
function makeMarker(cm, labels, severity, multiple, tooltips) {
98106
var marker = document.createElement("div"), inner = marker;
99107
marker.className = "CodeMirror-lint-marker CodeMirror-lint-marker-" + severity;
@@ -136,7 +144,7 @@
136144
return tip;
137145
}
138146

139-
function lintAsync(cm, getAnnotations, passOptions) {
147+
function lintAsync(cm, getAnnotations) {
140148
var state = cm.state.lint
141149
var id = ++state.waitingFor
142150
function abort() {
@@ -149,7 +157,7 @@
149157
if (state.waitingFor != id) return
150158
if (arg2 && annotations instanceof CodeMirror) annotations = arg2
151159
cm.operation(function() {updateLinting(cm, annotations)})
152-
}, passOptions, cm);
160+
}, state.linterOptions, cm);
153161
}
154162

155163
function startLinting(cm) {
@@ -160,13 +168,12 @@
160168
* Passing rules in `options` property prevents JSHint (and other linters) from complaining
161169
* about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc.
162170
*/
163-
var passOptions = options.options || options;
164171
var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
165172
if (!getAnnotations) return;
166173
if (options.async || getAnnotations.async) {
167-
lintAsync(cm, getAnnotations, passOptions)
174+
lintAsync(cm, getAnnotations)
168175
} else {
169-
var annotations = getAnnotations(cm.getValue(), passOptions, cm);
176+
var annotations = getAnnotations(cm.getValue(), state.linterOptions, cm);
170177
if (!annotations) return;
171178
if (annotations.then) annotations.then(function(issues) {
172179
cm.operation(function() {updateLinting(cm, issues)})
@@ -213,7 +220,7 @@
213220
cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, annotations[line].length > 1,
214221
state.options.tooltips));
215222

216-
if (isHighlightErrorLinesEnabled(state))
223+
if (state.options.highlightLines)
217224
cm.addLineClass(line, "wrap", LINT_LINE_ID + maxSeverity);
218225
}
219226
if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
@@ -223,7 +230,7 @@
223230
var state = cm.state.lint;
224231
if (!state) return;
225232
clearTimeout(state.timeout);
226-
state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
233+
state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay);
227234
}
228235

229236
function popupTooltips(cm, annotations, e) {
@@ -263,8 +270,8 @@
263270
if (val) {
264271
var gutters = cm.getOption("gutters"), hasLintGutter = false;
265272
for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
266-
var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
267-
if (state.options.lintOnChange !== false)
273+
var state = cm.state.lint = new LintState(cm, val, hasLintGutter);
274+
if (state.options.lintOnChange)
268275
cm.on("change", onChange);
269276
if (state.options.tooltips != false && state.options.tooltips != "gutter")
270277
CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);

0 commit comments

Comments
 (0)