UNPKG

eslint-plugin-obsidian

Version:

ESLint rules for Obsidian

69 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeValidator = void 0; const lodash_1 = require("lodash"); const functionalComponent_1 = require("../../dto/functionalComponent"); const regex_1 = require("../../utils/regex"); const typeLiteral_1 = require("../../dto/types/typeLiteral"); const redundantTypeError_1 = require("./result/redundantTypeError"); const success_1 = require("./result/success"); const missingTypeError_1 = require("./result/missingTypeError"); class TypeValidator { constructor(options) { this.options = options; } validate(injectedComponent, generics) { var _a; if (!injectedComponent) return new success_1.Success(); const componentProps = new functionalComponent_1.FunctionalComponent(injectedComponent.arrowFunction).props.type; const injectComponentGenerics = (_a = generics === null || generics === void 0 ? void 0 : generics.types) !== null && _a !== void 0 ? _a : []; return this.areTypesValid(componentProps, injectComponentGenerics); } areTypesValid(componentProps, injectComponentGenerics) { if (this.typesAreEqual(componentProps, injectComponentGenerics) && this.isInjected(componentProps)) { return new redundantTypeError_1.RedundantTypeError(injectComponentGenerics); } if (this.hasInlineType(injectComponentGenerics) || (this.typesAreEqual(componentProps, injectComponentGenerics) && !this.isInjected(componentProps)) || ((0, lodash_1.isEmpty)(injectComponentGenerics) && this.isInjected(componentProps)) || this.typesAreInCorrectOrder(injectComponentGenerics, componentProps) || (this.isInjected(componentProps) && injectComponentGenerics.length === 2)) return new success_1.Success(); const injected = this.getInjectedTypes(componentProps); const own = this.getOwnTypes(componentProps); if (!own && !injected) return new success_1.Success(); return new missingTypeError_1.MissingTypeError(own, injected !== null && injected !== void 0 ? injected : []); } hasInlineType(injectComponentGenerics) { return injectComponentGenerics.some(typeLiteral_1.TypeLiteral.isTypeLiteral); } typesAreEqual(componentProps, injectComponentGenerics) { return componentProps.equals(injectComponentGenerics); } isInjected(componentProps) { return componentProps.size() === 1 && !!this.getInjectedTypes(componentProps); } getInjectedTypes(componentProps) { return componentProps.toString().join(',').match((0, regex_1.stringToRegex)(this.injectedPattern)); } getOwnTypes(componentProps) { return componentProps.toString().join(',').match((0, regex_1.stringToRegex)(this.ownPattern)); } typesAreInCorrectOrder(injectComponentGenerics, componentProps) { var _a; const isInjectSecond = !!((_a = injectComponentGenerics[1]) === null || _a === void 0 ? void 0 : _a.toString()[0].match((0, regex_1.stringToRegex)(this.injectedPattern))); return isInjectSecond && componentProps.size() === injectComponentGenerics.length && componentProps.includes(injectComponentGenerics); } get injectedPattern() { return this.options[0].injectedPropsPattern; } get ownPattern() { return this.options[0].ownPropsPattern; } } exports.TypeValidator = TypeValidator; //# sourceMappingURL=typeValidator.js.map