UNPKG

eslint-plugin-sf-plugin

Version:
86 lines (85 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.noThisOrg = 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 utils_2 = require("@typescript-eslint/utils"); const commands_1 = require("../../shared/commands"); const expressions_1 = require("../../shared/expressions"); exports.noThisOrg = eslint_utils_1.RuleCreator.withoutDocs({ meta: { docs: { description: 'Fix references to this.org (property on SfdxCommand)', recommended: 'recommended', }, messages: { noThisOrg: 'SfCommand does not have a this.org property. Make sure you parse the org flag.', useFlags: "change this.org to flags['target-org']", instanceProp: 'create a this.org property on SfCommand', setThisOrg: 'this org is defined on the class, but never set. Set it equal to the org flag.', }, hasSuggestions: true, type: 'suggestion', schema: [], fixable: 'code', }, defaultOptions: [], create(context) { return (0, commands_1.isInCommandDirectory)(context) ? { MemberExpression(node) { var _a; if ((0, expressions_1.MemberExpressionIsThisDotFoo)(node, 'org') && (0, commands_1.ancestorsContainsSfCommand)(context)) { // it's ok if there's a this.org on the class... const classAbove = (0, commands_1.getSfCommand)(context); if (!classAbove) { return; } const runMethod = (0, commands_1.getRunMethod)(classAbove); if (!runMethod) { return; } if (classAbove === null || classAbove === void 0 ? void 0 : classAbove.body.body.find((b) => b.type === utils_2.AST_NODE_TYPES.PropertyDefinition && b.key.type === utils_2.AST_NODE_TYPES.Identifier && b.key.name === 'org')) { // ...as long as it's been set in the run method const flagsParse = (runMethod === null || runMethod === void 0 ? void 0 : runMethod.type) === utils_2.AST_NODE_TYPES.MethodDefinition ? (_a = runMethod.value.body) === null || _a === void 0 ? void 0 : _a.body.filter(utils_1.ASTUtils.isNodeOfType(utils_2.AST_NODE_TYPES.VariableDeclaration)).find((b) => context.sourceCode.getText(b).includes('this.parse')) : undefined; const source = context.sourceCode.getText(); if (flagsParse && !source.includes('this.org = ')) { context.report({ node, messageId: 'setThisOrg', fix: (fixer) => fixer.insertTextAfter(flagsParse, "this.org = flags['target-org'];"), }); } } else { context.report({ node, messageId: 'noThisOrg', suggest: [ { messageId: 'useFlags', fix: (fixer) => fixer.replaceText(node, "flags['target-org']"), }, { messageId: 'instanceProp', fix: (fixer) => fixer.insertTextBefore(runMethod, 'private org: Org;\n'), }, ], }); } } }, } : {}; }, });