"use strict";
function indexOfAllDefinitionsInText(text, searchingText) {
return new DefinitionInTextFinder(text).indexOfAll(searchingText);
}
exports.indexOfAllDefinitionsInText = indexOfAllDefinitionsInText;
var DefinitionInTextFinder = (function () {
function DefinitionInTextFinder(text) {
this.text = text;
this.stringCharStack = [];
this.currentIndex = 0;
this.validVarNameChars = /[A-Za-z0-9_]/;
}
DefinitionInTextFinder.prototype.indexOfAll = function (searchingText) {
var _this = this;
var currentMatchIndex = 0;
var currentCharMatches = function () { return currentMatchIndex < searchingText.length && _this.getCurrentChar() === searchingText[currentMatchIndex]; };
var isValidFirstChar = function () { return !_this.isValidVariableNameChar(_this.getLastChar()) && _this.getLastChar() !== "."; };
var foundIndexes = [];
for (this.currentIndex = 0; this.currentIndex < this.text.length; this.currentIndex++) {
this.handleStringChar();
if (!this.isInString() && currentCharMatches() && (currentMatchIndex !== 0 || isValidFirstChar())) {
currentMatchIndex++;
}
else {
currentMatchIndex = 0;
}
if (currentMatchIndex === searchingText.length) {
if (!this.isValidVariableNameChar(this.getNextChar())) {
foundIndexes.push((this.currentIndex + 1) - searchingText.length);
}
currentMatchIndex = 0;
}
}
return foundIndexes;
};
DefinitionInTextFinder.prototype.isValidVariableNameChar = function (char) {
return char != null && this.validVarNameChars.test(char);
};
DefinitionInTextFinder.prototype.handleStringChar = function () {
if (this.isCurrentStringChar()) {
var lastStringChar = this.getLastStringCharOnStack();
var currentChar = this.getCurrentChar();
if (currentChar === lastStringChar) {
this.stringCharStack.pop();
}
else {
this.stringCharStack.push(currentChar === "{" ? "}" : currentChar);
}
}
};
DefinitionInTextFinder.prototype.isCurrentStringChar = function () {
var lastChar = this.getLastChar();
var currentChar = this.getCurrentChar();
var lastStringChar = this.getLastStringCharOnStack();
if (lastChar === "\\") {
return false;
}
else if (lastStringChar == null) {
return currentChar === "`" || currentChar === "'" || currentChar === "\"";
}
else if (lastStringChar === "`" && lastChar === "$" && currentChar === "{") {
return true;
}
else if (lastStringChar === "}" && currentChar === "}") {
return true;
}
else {
return currentChar === lastStringChar;
}
};
DefinitionInTextFinder.prototype.isInString = function () {
return this.stringCharStack.length > 0 && this.stringCharStack[this.stringCharStack.length - 1] !== "}";
};
DefinitionInTextFinder.prototype.getLastStringCharOnStack = function () {
if (this.stringCharStack.length > 0) {
return this.stringCharStack[this.stringCharStack.length - 1];
}
else {
return null;
}
};
DefinitionInTextFinder.prototype.getLastChar = function () {
return this.currentIndex - 1 < 0 ? null : this.text[this.currentIndex - 1];
};
DefinitionInTextFinder.prototype.getCurrentChar = function () {
return this.text[this.currentIndex];
};
DefinitionInTextFinder.prototype.getNextChar = function () {
return this.currentIndex + 1 >= this.text.length ? null : this.text[this.currentIndex + 1];
};
return DefinitionInTextFinder;
}());
//# sourceMappingURL=indexOfAllDefinitionsInText.js.map
|