@vanta-inc/eslint-plugin-vanta
Version:
Custom ESLint rules for Vanta
53 lines (52 loc) • 1.85 kB
JavaScript
;
/**
* @fileoverview Prefer Maybe<T> to T | null or T | undefined.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("@typescript-eslint/utils");
var NOTHING_TYPES = [
utils_1.AST_NODE_TYPES.TSNullKeyword,
utils_1.AST_NODE_TYPES.TSUndefinedKeyword,
];
var rule = utils_1.ESLintUtils.RuleCreator(function (ruleName) {
return "https://github.com/VantaInc/eslint-plugin-vanta/blob/main/docs/rules/" + ruleName + ".md";
})({
name: "prefer-maybe",
meta: {
fixable: "code",
docs: {
description: "Prefer Maybe<T> to T | null or T | undefined.",
recommended: "error",
},
messages: {
default: "Prefer Maybe<T> to T | null or T | undefined.",
},
type: "suggestion",
schema: [],
},
defaultOptions: [],
create: function (context) {
var sourceCode = context.getSourceCode();
return {
TSUnionType: function (node) {
var unionWithoutNothing = node.types.filter(function (t) { return !NOTHING_TYPES.includes(t.type); });
if (node.types.length === unionWithoutNothing.length) {
return;
}
var unionWithoutNothingString = unionWithoutNothing
.map(function (t) { return sourceCode.getText(t); })
.join(" | ");
var replacement = "Maybe<" + unionWithoutNothingString + ">";
context.report({
node: node,
messageId: "default",
fix: function (fixer) {
return [fixer.replaceText(node, replacement)];
},
});
},
};
},
});
module.exports = rule;
exports.default = rule;