UNPKG

eslint-plugin-sf-plugin

Version:
57 lines (56 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.noDefaultDependsOnFlags = void 0; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ const utils_1 = require("@typescript-eslint/utils"); const eslint_utils_1 = require("@typescript-eslint/utils/eslint-utils"); const commands_1 = require("../shared/commands"); const flags_1 = require("../shared/flags"); exports.noDefaultDependsOnFlags = eslint_utils_1.RuleCreator.withoutDocs({ meta: { docs: { description: 'Do not allow creation of a flag with default value and dependsOn', recommended: 'recommended', }, messages: { message: 'Cannot create a flag with a default value and dependsOn', }, type: 'problem', schema: [], }, defaultOptions: [], create(context) { return (0, commands_1.isInCommandDirectory)(context) ? { Property(node) { var _a, _b, _c, _d, _e, _f; // is a flag if ((0, flags_1.isFlag)(node) && (0, commands_1.ancestorsContainsSfCommand)(context) && ((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.CallExpression && ((_c = (_b = node.value.arguments) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.type) === utils_1.AST_NODE_TYPES.ObjectExpression) { const props = node.value.arguments[0].properties.filter(utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.Property)); const dependsOnProperty = props.find((0, flags_1.flagPropertyIsNamed)('dependsOn')); const defaultValueProperty = props.find((0, flags_1.flagPropertyIsNamed)('default')); // @ts-expect-error from the node (flag), go up a level (parent) and find the dependsOn flag definition, see if it has a default const dependsOnFlagDefaultValue = (_f = (_e = (_d = node.parent.properties .find((f) => { var _a, _b; // @ts-expect-error value type on dependsOn return f.type === utils_1.AST_NODE_TYPES.Property && f.key.name === ((_b = (_a = dependsOnProperty === null || dependsOnProperty === void 0 ? void 0 : dependsOnProperty.value.elements) === null || _a === void 0 ? void 0 : _a.at(0)) === null || _b === void 0 ? void 0 : _b.value); })) === null || _d === void 0 ? void 0 : _d.value.arguments) === null || _e === void 0 ? void 0 : _e.at(0)) === null || _f === void 0 ? void 0 : _f.properties.find((p) => p.key.name === 'default'); if (dependsOnProperty && defaultValueProperty && !dependsOnFlagDefaultValue) { context.report({ node: dependsOnProperty, messageId: 'message', }); } } }, } : {}; }, });