@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
53 lines (52 loc) • 2.38 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Analysis
* @description Analyzer
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkedAnalyzer = void 0;
const parse_script_1 = require("../parse/script/parse-script");
const find_all_1 = require("./find-all");
const find_one_1 = require("./find-one");
class MarkedAnalyzer {
static fromSourceAsync(sourceCode, language) {
return __awaiter(this, void 0, void 0, function* () {
const parsedResult = yield (0, parse_script_1.parseScript)(sourceCode, language);
return new MarkedAnalyzer(parsedResult);
});
}
constructor(parsedResult) {
this._parsed = parsedResult;
}
findOneNodeOrNull(type) {
const result = (0, find_one_1.findOneESTNodeOrNull)(this._parsed.estree, type);
if (result === null || typeof result === "undefined") {
return null;
}
return Object.assign(Object.assign({}, result), { loc: this._findUpdatedLocation(result) });
}
findAllNodes(type) {
const result = (0, find_all_1.findAllESTNodes)(this._parsed.estree, type);
return result.map((each) => (Object.assign(Object.assign({}, each), { loc: this._findUpdatedLocation(each) })));
}
_findUpdatedLocation(node) {
const originalLocation = node.loc;
const startPosition = this._parsed.locationFinder.findSourceLocation(originalLocation.start, node);
const endPosition = this._parsed.locationFinder.findSourceLocation(originalLocation.end, node);
return {
start: startPosition,
end: endPosition,
};
}
}
exports.MarkedAnalyzer = MarkedAnalyzer;