C. Scott Ananian has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/398175 )

Change subject: Test a Set for trace/debug/dump flags.
......................................................................

Test a Set for trace/debug/dump flags.

This reads more naturally than repeated indexOf() operations against
an array, and folds the null check into the has() test as well.

Change-Id: I6531dda9d525702c76e4fe1f2911dea9a665b270
---
M bin/parserTests.js
M lib/config/ParsoidConfig.js
M lib/html2wt/SelectiveSerializer.js
M lib/html2wt/WikitextSerializer.js
M lib/logger/ParsoidLogger.js
M lib/wt2html/DOMPostProcessor.js
M lib/wt2html/TokenTransformManager.js
M lib/wt2html/pp/processors/computeDSR.js
M lib/wt2html/pp/processors/wrapSections.js
M lib/wt2html/pp/processors/wrapTemplates.js
M lib/wt2html/tokenizer.js
M lib/wt2html/tt/TemplateHandler.js
12 files changed, 35 insertions(+), 35 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/75/398175/1

diff --git a/bin/parserTests.js b/bin/parserTests.js
index 9fe93e6..e6ac079 100755
--- a/bin/parserTests.js
+++ b/bin/parserTests.js
@@ -366,8 +366,7 @@
                }
        }
 
-       if (this.env.conf.parsoid.dumpFlags &&
-               this.env.conf.parsoid.dumpFlags.indexOf("dom:post-changes") !== 
-1) {
+       if (this.env.conf.parsoid.dumpFlagSet().has("dom:post-changes")) {
                DU.dumpDOM(body, 'Original DOM');
        }
 
@@ -381,8 +380,7 @@
                applyChangesInternal(body, item.changes);
        }
 
-       if (this.env.conf.parsoid.dumpFlags &&
-               this.env.conf.parsoid.dumpFlags.indexOf("dom:post-changes") !== 
-1) {
+       if (this.env.conf.parsoid.dumpFlagSet().has("dom:post-changes")) {
                console.warn("Change tree : " + JSON.stringify(item.changes));
                DU.dumpDOM(body, 'Edited DOM');
        }
diff --git a/lib/config/ParsoidConfig.js b/lib/config/ParsoidConfig.js
index 48383e2..0b3371c 100644
--- a/lib/config/ParsoidConfig.js
+++ b/lib/config/ParsoidConfig.js
@@ -212,6 +212,19 @@
 ParsoidConfig.prototype.dumpFlags = null;
 
 /**
+ * Convenience function to allow us to access these arrays as a Set.
+ */
+['trace', 'debug', 'dump'].forEach(function(method) {
+    var backingSet = null;
+    ParsoidConfig.prototype[method+'FlagSet'] = function() {
+        if (backingSet === null) {
+            backingSet = new Set(this[method+'Flags'] || []);
+        }
+        return backingSet;
+    };
+});
+
+/**
  * @property {boolean} fetchTemplates Whether we should request templates from 
a wiki, or just use cached versions.
  */
 ParsoidConfig.prototype.fetchTemplates = true;
diff --git a/lib/html2wt/SelectiveSerializer.js 
b/lib/html2wt/SelectiveSerializer.js
index 66f286d..2e632a2 100644
--- a/lib/html2wt/SelectiveSerializer.js
+++ b/lib/html2wt/SelectiveSerializer.js
@@ -29,8 +29,7 @@
        this.wts = options.wts || new WikitextSerializer(options);
 
        // Debug options
-       this.trace = this.env.conf.parsoid.traceFlags &&
-                       (this.env.conf.parsoid.traceFlags.indexOf("selser") !== 
-1);
+       this.trace = this.env.conf.parsoid.traceFlagSet().has("selser");
 
        // Performance Timing option
        this.metrics = this.env.conf.parsoid.metrics;
@@ -96,8 +95,7 @@
                        // Nothing was modified, just re-use the original source
                        p = Promise.resolve(this.env.page.src);
                } else {
-                       if (this.trace || (this.env.conf.parsoid.dumpFlags &&
-                               
this.env.conf.parsoid.dumpFlags.indexOf('dom:post-dom-diff') !== -1)) {
+                       if (this.trace || 
this.env.conf.parsoid.dumpFlagSet().has('dom:post-dom-diff')) {
                                DU.dumpDOM(body, 'DOM after running DOMDiff', {
                                        storeDiffMark: true,
                                        env: this.env,
diff --git a/lib/html2wt/WikitextSerializer.js 
b/lib/html2wt/WikitextSerializer.js
index bdaba5e..04907f3 100644
--- a/lib/html2wt/WikitextSerializer.js
+++ b/lib/html2wt/WikitextSerializer.js
@@ -1429,7 +1429,7 @@
        (new Normalizer(state)).normalizeDOM(body);
 
        var psd = this.env.conf.parsoid;
-       if (psd.dumpFlags && (psd.dumpFlags.indexOf("dom:post-normal") !== -1)) 
{
+       if (psd.dumpFlagSet().has("dom:post-normal")) {
                DU.dumpDOM(body, 'DOM: post-normal');
        }
 
diff --git a/lib/logger/ParsoidLogger.js b/lib/logger/ParsoidLogger.js
index 63b2db4..d44690d 100644
--- a/lib/logger/ParsoidLogger.js
+++ b/lib/logger/ParsoidLogger.js
@@ -90,13 +90,9 @@
        // TRACE / DEBUG: Make trace / debug regexp with appropriate postfixes,
        // depending on the command-line options passed in.
        function buildTraceOrDebugFlag(parsoidFlags, logType) {
-               if (Array.isArray(parsoidFlags)) {
-                       var escapedFlags = parsoidFlags.map(Util.escapeRegExp);
-                       var combinedFlag = logType + "\/(" + 
escapedFlags.join("|") + ")(\\/|$)";
-                       return new RegExp(combinedFlag);
-               } else {
-                       return null;
-               }
+               var escapedFlags = 
Array.from(parsoidFlags).map(Util.escapeRegExp);
+               var combinedFlag = logType + "\/(" + escapedFlags.join("|") + 
")(\\/|$)";
+               return new RegExp(combinedFlag);
        }
 
        // Register separate backend for tracing / debugging events.
diff --git a/lib/wt2html/DOMPostProcessor.js b/lib/wt2html/DOMPostProcessor.js
index e2d3840..3bf1063 100644
--- a/lib/wt2html/DOMPostProcessor.js
+++ b/lib/wt2html/DOMPostProcessor.js
@@ -470,11 +470,11 @@
        var env = this.env;
 
        var psd = env.conf.parsoid;
-       if (psd.dumpFlags && (psd.dumpFlags.indexOf("dom:post-builder") !== 
-1)) {
+       if (psd.dumpFlagSet().has("dom:post-builder")) {
                DU.dumpDOM(document.body, 'DOM: after tree builder');
        }
 
-       var tracePP = psd.traceFlags && (psd.traceFlags.indexOf("time/dompp") 
!== -1 || psd.traceFlags.indexOf("time") !== -1);
+       var tracePP = psd.traceFlagSet().has("time/dompp") || 
psd.traceFlagSet().has("time"));
 
        // Holder for data-* attributes
        if (this.atTopLevel && env.pageBundle) {
@@ -530,10 +530,10 @@
        // For the top-level document, we generate <head> and add it.
        if (this.atTopLevel) {
                DOMPostProcessor.addMetaData(env, document);
-               if (psd.traceFlags && psd.traceFlags.indexOf('time') !== -1) {
+               if (psd.traceFlagSet().has('time')) {
                        env.printTimeProfile();
                }
-               if (psd.dumpFlags && psd.dumpFlags.indexOf('wt2html:limits') 
!== -1) {
+               if (psd.dumpFlagSet().has('wt2html:limits')) {
                        env.printParserResourceUsage({ 'HTML Size': 
document.outerHTML.length });
                }
                if (env.conf.parsoid.linting) {
diff --git a/lib/wt2html/TokenTransformManager.js 
b/lib/wt2html/TokenTransformManager.js
index 7c75438..b882a1f 100644
--- a/lib/wt2html/TokenTransformManager.js
+++ b/lib/wt2html/TokenTransformManager.js
@@ -153,8 +153,7 @@
  *   tag name for tags, omitted for non-tags
  */
 TokenTransformManager.prototype.addTransform = function(transformation, 
debugName, rank, type, name) {
-       var traceFlags = this.env.conf.parsoid.traceFlags;
-       var traceTime = traceFlags && (traceFlags.indexOf("time") !== -1);
+       var traceTime = this.env.conf.parsoid.traceFlagSet().has("time");
        if (traceTime) {
                transformation = this.timeTracer(transformation, debugName);
        }
@@ -550,8 +549,7 @@
        }
 
        // Time tracing related state
-       var traceFlags = this.env.conf.parsoid.traceFlags;
-       var traceTime = traceFlags && traceFlags.indexOf('time') !== -1;
+       var traceTime = this.env.conf.parsoid.traceFlagSet().has('time');
        var startTime = traceTime && Date.now();
        var tokenTimes = 0;
 
@@ -900,8 +898,7 @@
 
        // Time tracing related state
        var tokenTimes = 0;
-       var traceFlags = this.env.conf.parsoid.traceFlags;
-       var traceTime = traceFlags && traceFlags.indexOf('time') !== -1;
+       var traceTime = this.env.conf.parsoid.traceFlagSet().has('time');
        var startTime = traceTime && Date.now();
 
        // Stack of token arrays to process
diff --git a/lib/wt2html/pp/processors/computeDSR.js 
b/lib/wt2html/pp/processors/computeDSR.js
index 716909f..341074f 100644
--- a/lib/wt2html/pp/processors/computeDSR.js
+++ b/lib/wt2html/pp/processors/computeDSR.js
@@ -651,7 +651,7 @@
        var endOffset = options.sourceOffsets ? options.sourceOffsets[1] : 
env.page.src.length;
        var psd = env.conf.parsoid;
 
-       if (psd.dumpFlags && (psd.dumpFlags.indexOf("dom:pre-dsr") !== -1)) {
+       if (psd.dumpFlagSet().has("dom:pre-dsr")) {
                DU.dumpDOM(rootNode, 'DOM: pre-DSR');
        }
 
@@ -666,7 +666,7 @@
 
        env.log("trace/dsr", "------- done tracing computation -------");
 
-       if (psd.dumpFlags && (psd.dumpFlags.indexOf("dom:post-dsr") !== -1)) {
+       if (psd.dumpFlagSet().has("dom:post-dsr")) {
                DU.dumpDOM(rootNode, 'DOM: post-DSR');
        }
 }
diff --git a/lib/wt2html/pp/processors/wrapSections.js 
b/lib/wt2html/pp/processors/wrapSections.js
index ab25dc5..11f9871 100644
--- a/lib/wt2html/pp/processors/wrapSections.js
+++ b/lib/wt2html/pp/processors/wrapSections.js
@@ -295,7 +295,7 @@
                return;
        }
 
-       if (env.conf.parsoid.dumpFlags && 
(env.conf.parsoid.dumpFlags.indexOf("dom:pre-sections") !== -1)) {
+       if (env.conf.parsoid.dumpFlagSet().has("dom:pre-sections")) {
                DU.dumpDOM(rootNode, 'DOM: before section wrapping');
        }
 
diff --git a/lib/wt2html/pp/processors/wrapTemplates.js 
b/lib/wt2html/pp/processors/wrapTemplates.js
index 6a45123..fba8ba3 100644
--- a/lib/wt2html/pp/processors/wrapTemplates.js
+++ b/lib/wt2html/pp/processors/wrapTemplates.js
@@ -1024,13 +1024,13 @@
 function wrapTemplates(body, env, options) {
        var psd = env.conf.parsoid;
 
-       if (psd.dumpFlags && (psd.dumpFlags.indexOf("dom:pre-encap") !== -1)) {
+       if (psd.dumpFlagSet().has("dom:pre-encap")) {
                DU.dumpDOM(body, 'DOM: pre-encapsulation');
        }
 
        wrapTemplatesInTree(body.ownerDocument, env, body);
 
-       if (psd.dumpFlags && (psd.dumpFlags.indexOf("dom:post-encap") !== -1)) {
+       if (psd.dumpFlagSet().has("dom:post-encap")) {
                DU.dumpDOM(body, 'DOM: post-encapsulation');
        }
 }
diff --git a/lib/wt2html/tokenizer.js b/lib/wt2html/tokenizer.js
index 6858d3c..3fc5478 100644
--- a/lib/wt2html/tokenizer.js
+++ b/lib/wt2html/tokenizer.js
@@ -51,8 +51,7 @@
        events.EventEmitter.call(this);
        this.env = env;
        // env can be null during code linting
-       var traceFlags = env ? env.conf.parsoid.traceFlags : null;
-       this.traceTime = traceFlags && traceFlags.indexOf('time') !== -1;
+       this.traceTime = env && env.conf.parsoid.traceFlagSet().has('time');
        this.options = options || {};
        this.offsets = {};
 }
diff --git a/lib/wt2html/tt/TemplateHandler.js 
b/lib/wt2html/tt/TemplateHandler.js
index 926aceb..3af4c0d 100644
--- a/lib/wt2html/tt/TemplateHandler.js
+++ b/lib/wt2html/tt/TemplateHandler.js
@@ -788,8 +788,7 @@
                //  this.manager.env.errCB(err);
        }
 
-       var pConf = this.manager.env.conf.parsoid;
-       if (pConf.dumpFlags && pConf.dumpFlags.indexOf("tplsrc") !== -1) {
+       if (this.manager.env.conf.parsoid.dumpFlagSet().has("tplsrc")) {
                console.log("=================================");
                console.log(tplArgs.name);
                console.log("---------------------------------");

-- 
To view, visit https://gerrit.wikimedia.org/r/398175
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6531dda9d525702c76e4fe1f2911dea9a665b270
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: C. Scott Ananian <canan...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to