eslint-plugin-sf-plugin
Version:
Helpful eslint rules for sf plugins.
77 lines (76 loc) • 4.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.flagSummary = 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 eslint_utils_1 = require("@typescript-eslint/utils/eslint-utils");
const utils_1 = require("@typescript-eslint/utils");
const commands_1 = require("../shared/commands");
const flags_1 = require("../shared/flags");
exports.flagSummary = eslint_utils_1.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Enforce that flags have a summary property and that longDescription is renamed to description',
recommended: 'recommended',
},
messages: {
message: 'Flags should have a summary property',
longDescription: 'use "description" instead of "longDescription"',
},
type: 'problem',
schema: [],
fixable: 'code',
},
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) &&
((_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 propertyArguments = node.value.arguments[0].properties.filter(utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.Property));
if (!propertyArguments.some((0, flags_1.flagPropertyIsNamed)('summary'))) {
const descriptionProp = propertyArguments.find((0, flags_1.flagPropertyIsNamed)('description'));
const range = descriptionProp && 'key' in descriptionProp ? descriptionProp === null || descriptionProp === void 0 ? void 0 : descriptionProp.key.range : undefined;
return context.report({
node,
messageId: 'message',
...(range
? {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
fix: (fixer) => fixer.replaceTextRange(range, 'summary'),
}
: {}),
});
}
if (!propertyArguments.some((0, flags_1.flagPropertyIsNamed)('description'))) {
// if there is no description, but there is a longDescription, turn that into the description
const longDescriptionProp = propertyArguments.find((0, flags_1.flagPropertyIsNamed)('longDescription'));
if (!longDescriptionProp) {
return;
}
const range = longDescriptionProp && 'key' in longDescriptionProp ? longDescriptionProp === null || longDescriptionProp === void 0 ? void 0 : longDescriptionProp.key.range : undefined;
return context.report({
node: longDescriptionProp,
messageId: 'longDescription',
...(range
? {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
fix: (fixer) => fixer.replaceTextRange(range, 'description'),
}
: {}),
});
}
}
},
}
: {};
},
});
;