eslint-plugin-sf-plugin
Version:
Helpful eslint rules for sf plugins.
79 lines (78 loc) • 4.91 kB
JavaScript
/*
* 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCalleePropertyByName = exports.getBaseFlagsStaticPropertyFromCommandClass = exports.getFlagsStaticPropertyFromCommandClass = exports.resolveFlagName = exports.flagPropertyIsNamed = exports.isBaseFlagsStaticProperty = exports.isFlagsStaticProperty = exports.isFlag = void 0;
const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
/** Current node is 'foo' : Flags.x({}) */
const isFlag = (node) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return node.type === typescript_estree_1.AST_NODE_TYPES.Property &&
((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === typescript_estree_1.AST_NODE_TYPES.CallExpression &&
((_c = (_b = node.value) === null || _b === void 0 ? void 0 : _b.callee) === null || _c === void 0 ? void 0 : _c.type) === typescript_estree_1.AST_NODE_TYPES.MemberExpression &&
((_f = (_e = (_d = node.value) === null || _d === void 0 ? void 0 : _d.callee) === null || _e === void 0 ? void 0 : _e.object) === null || _f === void 0 ? void 0 : _f.type) === typescript_estree_1.AST_NODE_TYPES.Identifier &&
((_j = (_h = (_g = node.value) === null || _g === void 0 ? void 0 : _g.callee) === null || _h === void 0 ? void 0 : _h.object) === null || _j === void 0 ? void 0 : _j.name) === 'Flags';
};
exports.isFlag = isFlag;
/** Current node is public static flags = */
const isFlagsStaticProperty = (node) => {
var _a;
return node.type === typescript_estree_1.AST_NODE_TYPES.PropertyDefinition &&
typeof node.accessibility === 'string' &&
node.static &&
((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === typescript_estree_1.AST_NODE_TYPES.ObjectExpression &&
node.key.type === typescript_estree_1.AST_NODE_TYPES.Identifier &&
node.key.name === 'flags' &&
['public', 'protected'].includes(node.accessibility);
};
exports.isFlagsStaticProperty = isFlagsStaticProperty;
const isBaseFlagsStaticProperty = (node) => {
var _a;
return node.type === typescript_estree_1.AST_NODE_TYPES.PropertyDefinition &&
typeof node.accessibility === 'string' &&
node.static &&
((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === typescript_estree_1.AST_NODE_TYPES.ObjectExpression &&
node.key.type === typescript_estree_1.AST_NODE_TYPES.Identifier &&
node.key.name === 'baseFlags' &&
['public', 'protected'].includes(node.accessibility);
};
exports.isBaseFlagsStaticProperty = isBaseFlagsStaticProperty;
const flagPropertyIsNamed = (name) => (node) => (0, exports.resolveFlagName)(node) === name;
exports.flagPropertyIsNamed = flagPropertyIsNamed;
/** pass in a flag Property and it gives back the key name/value depending on type */
const resolveFlagName = (flag) => {
if (flag.key.type === typescript_estree_1.AST_NODE_TYPES.Identifier) {
return flag.key.name;
}
if (flag.key.type === typescript_estree_1.AST_NODE_TYPES.Literal && typeof flag.key.value === 'string') {
return flag.key.value;
}
};
exports.resolveFlagName = resolveFlagName;
const getFlagsStaticPropertyFromCommandClass = (classDeclaration) => {
if (classDeclaration.body.type === typescript_estree_1.AST_NODE_TYPES.ClassBody) {
return classDeclaration.body.body.find(exports.isFlagsStaticProperty);
}
};
exports.getFlagsStaticPropertyFromCommandClass = getFlagsStaticPropertyFromCommandClass;
const getBaseFlagsStaticPropertyFromCommandClass = (classDeclaration) => {
if (classDeclaration.body.type === typescript_estree_1.AST_NODE_TYPES.ClassBody) {
return classDeclaration.body.body.find(exports.isBaseFlagsStaticProperty);
}
};
exports.getBaseFlagsStaticPropertyFromCommandClass = getBaseFlagsStaticPropertyFromCommandClass;
const getCalleePropertyByName = (node, calleePropName) => {
var _a, _b, _c;
return (node.key.type === typescript_estree_1.AST_NODE_TYPES.Identifier || node.key.type === typescript_estree_1.AST_NODE_TYPES.Literal) &&
((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === typescript_estree_1.AST_NODE_TYPES.CallExpression &&
((_b = node.value.callee) === null || _b === void 0 ? void 0 : _b.type) === typescript_estree_1.AST_NODE_TYPES.MemberExpression &&
((_c = node.value.callee.property) === null || _c === void 0 ? void 0 : _c.type) === typescript_estree_1.AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === calleePropName
? node.value.callee.property
: undefined;
};
exports.getCalleePropertyByName = getCalleePropertyByName;
;