eslint-plugin-sf-plugin
Version:
Helpful eslint rules for sf plugins.
77 lines (76 loc) • 4.09 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.flagCrossReferences = 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");
// properties that reference other flags by name
const propertyNames = ['dependsOn', 'exactlyOne', 'exclusive'];
exports.flagCrossReferences = eslint_utils_1.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Enforce flag cross references for dependOn,exclusive,exactlyOne',
recommended: 'recommended',
},
messages: {
missingFlag: 'There is no flag named {{flagName}}',
},
type: 'problem',
schema: [],
},
defaultOptions: [],
create(context) {
return (0, commands_1.isInCommandDirectory)(context)
? {
Property(node) {
var _a;
const ancestors = context.getAncestors();
if (node.key.type === utils_1.AST_NODE_TYPES.Identifier &&
node.value.type === utils_1.AST_NODE_TYPES.ArrayExpression &&
node.value.elements.every((e) => (e === null || e === void 0 ? void 0 : e.type) === utils_1.AST_NODE_TYPES.Literal && (e === null || e === void 0 ? void 0 : e.raw)) &&
propertyNames.includes(node.key.name) &&
(0, commands_1.ancestorsContainsSfCommand)(context) &&
ancestors.some((a) => (0, flags_1.isFlag)(a))) {
const flagsNode = ancestors
.filter(utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.PropertyDefinition))
.find((a) => (0, flags_1.isFlagsStaticProperty)(a));
const arrayValues = node.value.elements
.map((e) => ((e === null || e === void 0 ? void 0 : e.type) === utils_1.AST_NODE_TYPES.Literal ? e.value : undefined))
.filter(Boolean);
if ((flagsNode === null || flagsNode === void 0 ? void 0 : flagsNode.key.type) === utils_1.AST_NODE_TYPES.Identifier &&
((_a = flagsNode.value) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.ObjectExpression) {
// get the names of all the flags as an array
const flagNames = flagsNode.value.properties.map((flagProp) => {
if (flagProp.type === utils_1.AST_NODE_TYPES.Property) {
if (flagProp.key.type === utils_1.AST_NODE_TYPES.Identifier) {
return flagProp.key.name;
}
else if (flagProp.key.type === utils_1.AST_NODE_TYPES.Literal) {
return flagProp.key.value;
}
}
});
// for each of the _Literal_ values in the dependOn/exactlyOne/exclusive array
arrayValues.forEach((value) => {
if (!flagNames.includes(value)) {
context.report({
node,
messageId: 'missingFlag',
data: { flagName: value },
});
}
});
}
}
},
}
: {};
},
});
;