Skip to content

Commit d1bf0c3

Browse files
authored
Merge pull request #237 from icebob/bug-#236
fix debug mode
2 parents 2fa314b + 03d296d commit d1bf0c3

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/validator.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ class Validator {
7878
if (!Array.isArray(plugins)) throw new Error("Plugins type must be array");
7979
plugins.forEach(this.plugin.bind(this));
8080
}
81+
82+
/* istanbul ignore next */
83+
if (this.opts.debug) {
84+
let formatter = function (code) { return code; };
85+
if (typeof window === "undefined") {
86+
formatter = require("./helpers/prettier");
87+
}
88+
89+
this._formatter = formatter;
90+
}
8191
}
8292
}
8393

@@ -215,12 +225,7 @@ class Validator {
215225

216226
/* istanbul ignore next */
217227
if (this.opts.debug) {
218-
let formatter = function (code) { return code; };
219-
if (typeof window === "undefined") // eslint-disable-line no-undef
220-
formatter = require("./helpers/prettier");
221-
222-
context.fn.forEach((fn, i) => console.log(formatter(`// Context.fn[${i}]\n` + fn.toString()))); // eslint-disable-line no-console
223-
console.log(formatter("// Main check function\n" + checkFn.toString())); // eslint-disable-line no-console
228+
console.log(this._formatter("// Main check function\n" + checkFn.toString())); // eslint-disable-line no-console
224229
}
225230

226231
this.cache.clear();
@@ -277,6 +282,11 @@ class Validator {
277282
context.fn[rule.index] = fn.bind(this);
278283
sourceCode.push(this.wrapRequiredCheckSourceCode(rule, innerSrc.replace(/%%INDEX%%/g, rule.index), context, resVar));
279284
sourceCode.push(this.makeCustomValidator({vName: resVar, path: customPath, schema: rule.schema, context, messages: rule.messages, ruleIndex: rule.index}));
285+
286+
/* istanbul ignore next */
287+
if (this.opts.debug) {
288+
console.log(this._formatter(`// Context.fn[${rule.index}]\n` + fn.toString())); // eslint-disable-line no-console
289+
}
280290
}
281291

282292
return sourceCode.join("\n");

0 commit comments

Comments
 (0)