eslint-plugin-sf-plugin
Version:
Helpful eslint rules for sf plugins.
50 lines (49 loc) • 2.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.flagMinMaxDefault = 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.flagMinMaxDefault = eslint_utils_1.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Enforce that flags with min/max values have a default value',
recommended: 'stylistic',
},
messages: {
message: 'If your flag has a min or max value, it should have a default value. Otherwise, it will be undefined',
},
type: 'problem',
schema: [],
},
defaultOptions: [],
create(context) {
return (0, commands_1.isInCommandDirectory)(context)
? {
Property(node) {
var _a, _b, _c;
if ((0, flags_1.isFlag)(node) && (0, commands_1.ancestorsContainsSfCommand)(context)) {
if (((_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));
if (props.some((p) => (0, flags_1.flagPropertyIsNamed)('min')(p) || (0, flags_1.flagPropertyIsNamed)('max')(p)) &&
!props.some((p) => (0, flags_1.flagPropertyIsNamed)('default')(p) || (0, flags_1.flagPropertyIsNamed)('defaultValue')(p))) {
context.report({
node,
messageId: 'message',
});
}
}
}
},
}
: {};
},
});
;