From ff9e84f20e68ab1e7cc0696185ad86c92919d85f Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Thu, 23 Apr 2015 19:50:01 -0400 Subject: [PATCH] Adding documentation to subexp.js --- src/js/parser/javascript/subexp.js | 32 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 4ef405a..c4595ba 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -1,9 +1,14 @@ +// Subexp nodes are for expressions inside of parenthesis. It is rendered as a +// labeled box around the contained expression if a label is required. + import _ from 'lodash'; export default { type: 'subexp', definedProperties: { + // Default anchor is overridden to move it down to account for the group + // label and outline box. _anchor: { get: function() { var anchor = this.regexp.getBBox(), @@ -24,37 +29,38 @@ export default { '?!': 'negative lookahead' }, + // Renders the subexp into the currently set container. _render() { - // NOTE: this.label() MUST be called here, in _render and before any child - // nodes are rendered. This is to keep the group numbers in the correct - // order. + // **NOTE:** `this.label()` **MUST** be called here, in _render, and before + // any child nodes are rendered. This is to keep the group numbers in the + // correct order. var label = this.label(); + // Render the contained regexp. return this.regexp.render(this.container.group()) .then(() => { + // Create the labeled box around the regexp. return this.renderLabeledBox(label, this.regexp, { padding: 10 }); }); }, + // Returns the label for the subexpression. label() { - if (typeof this._label === 'undefined') { - if (_.has(this.labelMap, this.properties.capture.textValue)) { - this._label = this.labelMap[this.properties.capture.textValue]; - } else { - this._label = 'group #' + (this.state.groupCounter++); - } + if (_.has(this.labelMap, this.properties.capture.textValue)) { + return this.labelMap[this.properties.capture.textValue]; + } else { + return `group #${this.state.groupCounter++}`; } - - return this._label; }, setup() { - // NOTE: DO NOT call this.label() in setup. It will lead to groups being - // numbered in reverse order + // **NOTE:** **DO NOT** call `this.label()` in setup. It will lead to + // groups being numbered in reverse order. this.regexp = this.properties.regexp; + // If there is no need for a label, then proxy to the nested regexp. if (this.properties.capture.textValue == '?:') { this.proxy = this.regexp; }