tslint-to-eslint-config
Version:
Converts your TSLint configuration to the closest reasonable ESLint equivalent.
68 lines (67 loc) • 2.56 kB
JavaScript
;
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceFileComments = void 0;
const typescript_1 = __importDefault(require("typescript"));
const conversionError_1 = require("../../errors/conversionError");
const utils_1 = require("../../utils");
const replaceFileComments = (content, comments, converters, ruleCommentsCache, ruleEquivalents) => {
const getNewRuleLists = (ruleName) => {
var _a, _b, _c;
const cached =
(_a = ruleEquivalents.get(ruleName)) !== null && _a !== void 0
? _a
: ruleCommentsCache.get(ruleName);
if (cached !== undefined) {
return cached;
}
const converter = converters.get(ruleName);
if (converter === undefined) {
ruleCommentsCache.set(ruleName, []);
return undefined;
}
const converted = converter({ ruleArguments: [] });
if (converted instanceof conversionError_1.ConversionError) {
return undefined;
}
const equivalents =
(_c =
(_b = converted.rules) === null || _b === void 0
? void 0
: _b.map((conversion) => conversion.ruleName)) !== null && _c !== void 0
? _c
: [];
ruleCommentsCache.set(ruleName, equivalents);
return equivalents.join(", ");
};
for (const comment of [...comments].reverse()) {
const directive = comment.directive.replace("tslint:", "eslint-");
const ruleLists = (0, utils_1.uniqueFromSources)(
comment.ruleNames.map(getNewRuleLists),
).filter(Boolean);
const [left, right] =
comment.commentKind === typescript_1.default.SyntaxKind.SingleLineCommentTrivia &&
comment.directive !== "tslint:disable" &&
comment.directive !== "tslint:enable"
? ["// ", ""]
: ["/* ", " */"];
content = [
content.slice(0, comment.pos),
left,
directive,
ruleLists.length !== 0 && " ",
ruleLists.join(", "),
right,
content.slice(comment.end),
]
.filter(Boolean)
.join("");
}
return content;
};
exports.replaceFileComments = replaceFileComments;
//# sourceMappingURL=replaceFileComments.js.map