ts-lit-plugin
Version:
Typescript plugin that adds type checking and code completion to lit-html
175 lines (174 loc) • 7.28 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decorateLanguageService = void 0;
var logger_js_1 = require("./logger.js");
function decorateLanguageService(languageService, plugin) {
var e_1, _a, e_2, _b;
var languageServiceExtension = {
getCompletionsAtPosition: plugin.getCompletionsAtPosition.bind(plugin),
getCompletionEntryDetails: plugin.getCompletionEntryDetails.bind(plugin),
getSemanticDiagnostics: plugin.getSemanticDiagnostics.bind(plugin),
getDefinitionAndBoundSpan: plugin.getDefinitionAndBoundSpan.bind(plugin),
getCodeFixesAtPosition: plugin.getCodeFixesAtPosition.bind(plugin),
getQuickInfoAtPosition: plugin.getQuickInfoAtPosition.bind(plugin),
getJsxClosingTagAtPosition: plugin.getJsxClosingTagAtPosition.bind(plugin),
getRenameInfo: plugin.getRenameInfo.bind(plugin),
findRenameLocations: plugin.findRenameLocations.bind(plugin),
getSignatureHelpItems: plugin.getSignatureHelpItems.bind(plugin)
//getOutliningSpans: plugin.getOutliningSpans.bind(plugin)
//getFormattingEditsForRange: plugin.getFormattingEditsForRange.bind(plugin)
};
var decoratedLanguageService = __assign(__assign({}, languageService), languageServiceExtension);
var _loop_1 = function (methodName) {
var newMethod = decoratedLanguageService[methodName];
var oldMethod = languageService[methodName];
decoratedLanguageService[methodName] = function () {
if (plugin.context.config.disable && oldMethod != null) {
return oldMethod.apply(void 0, __spreadArray([], __read(arguments), false));
}
return wrapTryCatch(newMethod, oldMethod, methodName).apply(void 0, __spreadArray([], __read(arguments), false));
};
};
try {
// Make sure to call the old service if config.disable === true
for (var _c = __values(Object.getOwnPropertyNames(languageServiceExtension)), _d = _c.next(); !_d.done; _d = _c.next()) {
var methodName = _d.value;
_loop_1(methodName);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_1) throw e_1.error; }
}
try {
// Wrap all method calls to the service in logging and performance measuring
for (var _e = __values(Object.getOwnPropertyNames(decoratedLanguageService)), _f = _e.next(); !_f.done; _f = _e.next()) {
var methodName = _f.value;
//const isDecorated = languageServiceExtension[methodName] != null;
var isDecorated = decoratedLanguageService[methodName] != null;
if (isDecorated) {
var method = decoratedLanguageService[methodName];
decoratedLanguageService[methodName] = wrapLog(methodName, method, plugin);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
}
finally { if (e_2) throw e_2.error; }
}
return decoratedLanguageService;
}
exports.decorateLanguageService = decorateLanguageService;
/**
* Wraps a function in try catch in order to debug the plugin.
* If the function throws, this function logs the error.
* @param newMethod
* @param oldMethod
* @param methodName
*/
function wrapTryCatch(newMethod, oldMethod, methodName) {
return (function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
try {
return newMethod.apply(void 0, __spreadArray([], __read(args), false));
}
catch (e) {
var details = void 0;
if (e instanceof Error) {
details = "".concat(e.message, "\n").concat(e.stack);
}
else {
details = String(e);
}
logger_js_1.logger.error("Error [".concat(methodName, "]: ").concat(details), e);
// Always return the old method if anything fails
// Don't crash everything :-)
return oldMethod === null || oldMethod === void 0 ? void 0 : oldMethod.apply(void 0, __spreadArray([], __read(args), false));
}
});
}
/**
* Wraps a function so that it is logged every time the function called.
* @param name
* @param proxy
* @param plugin
*/
function wrapLog(name, proxy, plugin) {
return (function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (plugin.context.config.logging === "verbose") {
/**/
var startTime = Date.now();
logger_js_1.logger.verbose("[".concat(name, "] Called"));
var result = proxy.apply(void 0, __spreadArray([], __read(args), false));
var time = Math.round(Date.now() - startTime);
logger_js_1.logger.verbose("[".concat(name, "] Finished (").concat(time, "ms): Result: "), result == null ? "undefined" : Array.isArray(result) ? "Array: ".concat(result.length, " length") : "defined");
if (time > 100) {
logger_js_1.logger.warn("[".concat(name, "] took long time to complete! (").concat(time, "ms)"));
}
return result;
}
else {
return proxy.apply(void 0, __spreadArray([], __read(args), false));
}
});
}