ts-lit-plugin
Version:
Typescript plugin that adds type checking and code completion to lit-html
79 lines (78 loc) • 2.71 kB
JavaScript
;
var __values = (this && this.__values) || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
Object.defineProperty(exports, "__esModule", { value: true });
var ts_module_1 = require("../../ts-module");
var translate_range_1 = require("./translate-range");
var lit_analyzer_1 = require("lit-analyzer");
function translateCodeFixes(codeFixes, file) {
return codeFixes.map(function (codeFix) { return translateCodeFix(file, codeFix); });
}
exports.translateCodeFixes = translateCodeFixes;
function translateCodeFix(file, codeFix) {
return {
fixName: codeFix.kind.toLowerCase(),
description: codeFix.message,
changes: codeFix.actions.map(function (action) { return translateCodeFixAction(file, action); })
};
}
exports.translateCodeFix = translateCodeFix;
function translateCodeFixAction(file, action) {
switch (action.kind) {
case lit_analyzer_1.CodeActionKind.DOCUMENT_TEXT_CHANGE:
return {
fileName: file.fileName,
textChanges: [
{
span: translate_range_1.translateRange(action.change.range),
newText: action.change.newText
}
]
};
case lit_analyzer_1.CodeActionKind.IMPORT_COMPONENT: {
// Get the import path and the position where it can be placed
var lastImportIndex = getLastImportIndex(file);
return {
fileName: file.fileName,
textChanges: [
{
span: { start: lastImportIndex, length: 0 },
newText: "\nimport \"" + action.importPath + "\";"
}
]
};
}
}
}
/**
* Returns the position of the last import line.
* @param sourceFile
*/
function getLastImportIndex(sourceFile) {
var e_1, _a;
var lastImportIndex = 0;
try {
for (var _b = __values(sourceFile.statements), _c = _b.next(); !_c.done; _c = _b.next()) {
var statement = _c.value;
if (ts_module_1.tsModule.ts.isImportDeclaration(statement)) {
lastImportIndex = statement.getEnd();
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return lastImportIndex;
}