@jitterbit/soql-parser-js
Version:
Salesforce.com SOQL parser and composer
237 lines (236 loc) • 10.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Formatter = void 0;
var utils_1 = require("../utils");
var Formatter = (function () {
function Formatter(enabled, options) {
var _a, _b, _c, _d, _e;
this.currIndent = 1;
this.enabled = enabled;
this.options = {
numIndent: (_a = options.numIndent) !== null && _a !== void 0 ? _a : 1,
fieldMaxLineLength: (_b = options.fieldMaxLineLength) !== null && _b !== void 0 ? _b : 60,
fieldSubqueryParensOnOwnLine: (_c = options.fieldSubqueryParensOnOwnLine) !== null && _c !== void 0 ? _c : true,
whereClauseOperatorsIndented: true,
newLineAfterKeywords: (_d = options.newLineAfterKeywords) !== null && _d !== void 0 ? _d : false,
logging: (_e = options.logging) !== null && _e !== void 0 ? _e : false,
};
if (this.options.newLineAfterKeywords) {
this.options.fieldSubqueryParensOnOwnLine = true;
}
}
Formatter.prototype.log = function (data) {
if (this.options.logging) {
console.log(data);
}
};
Formatter.prototype.getIndent = function (additionalIndent) {
if (additionalIndent === void 0) { additionalIndent = 0; }
return this.repeatChar((this.currIndent + additionalIndent) * (this.options.numIndent || 1), '\t');
};
Formatter.prototype.repeatChar = function (numTimes, char) {
return new Array(numTimes).fill(char).join('');
};
Formatter.prototype.setSubquery = function (isSubquery) {
this.currIndent = isSubquery ? (this.currIndent += 1) : (this.currIndent -= 1);
};
Formatter.prototype.stepCurrIndex = function (num) {
this.currIndent += num;
};
Formatter.prototype.formatFields = function (fieldData) {
var _this = this;
function trimPrevSuffix(currIdx) {
if (fieldData.fields[currIdx - 1]) {
fieldData.fields[currIdx - 1].suffix = fieldData.fields[currIdx - 1].suffix.trim();
}
}
fieldData.fields.forEach(function (field, i) {
field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
});
if (this.enabled) {
var lineLen_1 = 0;
var newLineAndIndentNext_1 = false;
fieldData.fields.forEach(function (field, i) {
if (field.isSubquery) {
trimPrevSuffix(i);
field.prefix = "\n".concat(_this.getIndent());
field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
lineLen_1 = 0;
newLineAndIndentNext_1 = true;
}
else if (Array.isArray(field.typeOfClause)) {
trimPrevSuffix(i);
field.prefix = "\n".concat(_this.getIndent());
newLineAndIndentNext_1 = true;
}
else if ((0, utils_1.isNumber)(_this.options.fieldMaxLineLength)) {
lineLen_1 += field.text.length + field.suffix.length;
if (lineLen_1 > _this.options.fieldMaxLineLength || newLineAndIndentNext_1) {
trimPrevSuffix(i);
if (!_this.options.newLineAfterKeywords || i > 0) {
field.prefix += "\n".concat(_this.getIndent());
}
lineLen_1 = 0;
newLineAndIndentNext_1 = false;
}
}
_this.log(field);
});
}
};
Formatter.prototype.formatTyeOfField = function (text, typeOfClause) {
var _this = this;
if (this.enabled && this.options.newLineAfterKeywords) {
return typeOfClause
.map(function (part, i) {
if (i === 0) {
return part;
}
else if (i === typeOfClause.length - 1) {
return "".concat(_this.getIndent()).concat(part);
}
else {
return "".concat(_this.getIndent(), "\t").concat(part);
}
})
.join('\n');
}
return text;
};
Formatter.prototype.formatTypeofFieldCondition = function (condition) {
var output = '';
var fields = condition.fieldList.join(', ');
if (this.enabled && this.options.newLineAfterKeywords) {
var indent = this.getIndent();
output = "".concat(condition.type);
if (condition.objectType) {
output += "\n".concat(indent, "\t\t").concat(condition.objectType, "\n").concat(indent, "\tTHEN\n").concat(indent, "\t\t").concat(fields);
}
else {
output += "\n".concat(indent, "\t\t").concat(fields);
}
}
else {
output = condition.type;
if (condition.objectType) {
output += " ".concat(condition.objectType, " THEN ").concat(fields);
}
else {
output += " ".concat(fields);
}
}
return output;
};
Formatter.prototype.formatSubquery = function (queryStr, numTabs, incrementTabsWhereClauseOpIndent) {
if (numTabs === void 0) { numTabs = 2; }
if (incrementTabsWhereClauseOpIndent === void 0) { incrementTabsWhereClauseOpIndent = false; }
if (incrementTabsWhereClauseOpIndent) {
numTabs++;
}
var leftParen = '(';
var rightParen = ')';
if (this.enabled) {
if (this.options.fieldSubqueryParensOnOwnLine || this.options.newLineAfterKeywords) {
queryStr = queryStr.replace(/\n/g, "\n".concat(this.repeatChar(numTabs, '\t')));
leftParen = "(\n".concat(this.repeatChar(numTabs, '\t'));
rightParen = "\n".concat(this.repeatChar(numTabs - 1, '\t'), ")");
}
else {
queryStr = queryStr.replace(/\n/g, '\n\t');
}
}
return "".concat(leftParen).concat(queryStr).concat(rightParen);
};
Formatter.prototype.formatClause = function (clause) {
if (this.enabled) {
return this.options.newLineAfterKeywords ? "\n".concat(clause, "\n\t") : "\n".concat(clause);
}
return " ".concat(clause);
};
Formatter.prototype.formatText = function (text) {
return this.enabled && (this.options.newLineAfterKeywords || text.startsWith('\n')) ? text : " ".concat(text);
};
Formatter.prototype.formatWithIndent = function (text) {
return this.enabled ? "".concat(this.getIndent()).concat(text) : text;
};
Formatter.prototype.formatOrderByArray = function (groupBy) {
var _this = this;
if (this.enabled) {
var currLen_1 = 0;
var output_1 = '';
groupBy.forEach(function (token, i) {
var nextToken = groupBy[i + 1];
currLen_1 += token.length;
if (nextToken && (currLen_1 + nextToken.length > (_this.options.fieldMaxLineLength || 0) || _this.options.newLineAfterKeywords)) {
output_1 += "".concat(token, ",\n\t");
currLen_1 = 0;
}
else {
output_1 += "".concat(token).concat(nextToken ? ', ' : '');
}
});
return output_1;
}
else {
return groupBy.join(', ');
}
};
Formatter.prototype.formatParens = function (count, character, leadingParenInline) {
if (leadingParenInline === void 0) { leadingParenInline = false; }
var output = '';
if ((0, utils_1.isNumber)(count) && count > 0) {
if (this.enabled) {
if (character === '(') {
for (var i = 0; i < count; i++) {
if (leadingParenInline && i === count - 1) {
output += '(';
}
else {
if (i === 0) {
output += '(\n';
}
else {
this.currIndent++;
output += "".concat(this.getIndent(), "(\n");
}
}
}
if (!leadingParenInline || count > 1) {
this.currIndent++;
}
}
else {
for (var i = count - 1; i >= 0; i--) {
if (leadingParenInline && i === count - 1) {
output += ')';
}
else {
this.currIndent--;
output += "\n".concat(this.getIndent(), ")");
}
}
}
}
else {
output += (0, utils_1.generateParens)(count, character);
}
}
return output;
};
Formatter.prototype.formatWhereClauseOperators = function (operator, whereClause, additionalIndent) {
if (additionalIndent === void 0) { additionalIndent = 0; }
var skipNewLineAndIndent = operator === 'NOT';
if (this.enabled && !skipNewLineAndIndent) {
return "\n".concat(this.getIndent(additionalIndent)).concat(operator, " ").concat(whereClause);
}
else {
return "".concat(skipNewLineAndIndent ? '' : ' ').concat(operator, " ").concat(whereClause);
}
};
Formatter.prototype.formatAddNewLine = function (alt, skipNewLineAndIndent) {
if (alt === void 0) { alt = ' '; }
return this.enabled && !skipNewLineAndIndent ? "\n" : alt;
};
return Formatter;
}());
exports.Formatter = Formatter;