UNPKG

eslint-plugin-sf-plugin

Version:
90 lines (89 loc) 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.idFlagSuggestions = 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.idFlagSuggestions = eslint_utils_1.RuleCreator.withoutDocs({ meta: { docs: { description: 'Create better salesforceId flags with length and startsWith properties', recommended: 'stylistic', }, hasSuggestions: true, messages: { message: 'Suggestion: salesforceId flags have additional properties to validate the Id. Consider using them.', lengthSuggestion15: 'require the ID to be 15 characters', lengthSuggestion18: 'require the ID to be 18 characters', lengthSuggestionBoth: 'require the ID to be 15 or 18 characters', typeSuggestion: 'require the ID to start with a 3-character prefix', }, type: 'suggestion', schema: [], fixable: 'code', }, defaultOptions: [], create(context) { return (0, commands_1.isInCommandDirectory)(context) ? { Property(node) { var _a, _b, _c, _d, _e; if ((0, flags_1.isFlag)(node) && (0, commands_1.ancestorsContainsSfCommand)(context)) { if ((node.key.type === utils_1.AST_NODE_TYPES.Identifier || node.key.type === utils_1.AST_NODE_TYPES.Literal) && ((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.CallExpression && ((_b = node.value.callee) === null || _b === void 0 ? void 0 : _b.type) === utils_1.AST_NODE_TYPES.MemberExpression && ((_c = node.value.callee.property) === null || _c === void 0 ? void 0 : _c.type) === utils_1.AST_NODE_TYPES.Identifier && node.value.callee.property.name === 'salesforceId' && ((_e = (_d = node.value.arguments) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.type) === utils_1.AST_NODE_TYPES.ObjectExpression) { const argProps = node.value.arguments[0].properties.filter(utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.Property)); const hasStartsWith = argProps.some((0, flags_1.flagPropertyIsNamed)('startsWith')); const hasLength = argProps.some((0, flags_1.flagPropertyIsNamed)('length')); if (!hasStartsWith || !hasLength) { const existing = context.sourceCode.getText(node); const fixedStartsWith = existing.replace('salesforceId({', "salesforceId({startsWith: '000',"); const fixer15 = existing.replace('salesforceId({', 'salesforceId({length: 15,'); const fixer18 = existing.replace('salesforceId({', 'salesforceId({length: 18,'); const fixerBoth = existing.replace('salesforceId({', "salesforceId({length: 'both',"); context.report({ node: node.key, messageId: 'message', suggest: (!hasStartsWith ? [ { // I think this is a TS problem in the utils messageId: 'typeSuggestion', fix: (fixer) => fixer.replaceText(node, fixedStartsWith), }, ] : []).concat(!hasLength ? [ { messageId: 'lengthSuggestionBoth', fix: (fixer) => fixer.replaceText(node, fixerBoth), }, { messageId: 'lengthSuggestion15', fix: (fixer) => fixer.replaceText(node, fixer15), }, { messageId: 'lengthSuggestion18', fix: (fixer) => fixer.replaceText(node, fixer18), }, ] : []), }); } } } }, } : {}; }, });