Adding documentation to subexp.js

This commit is contained in:
Jeff Avallone 2015-04-23 19:50:01 -04:00
parent 661e7fa6fb
commit ff9e84f20e

View File

@ -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];
return this.labelMap[this.properties.capture.textValue];
} else {
this._label = 'group #' + (this.state.groupCounter++);
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;
}