UNPKG

eslint-plugin-sf-plugin

Version:
88 lines (87 loc) 5.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runMatchesClassType = 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 utils_1 = require("@typescript-eslint/utils"); const commands_1 = require("../shared/commands"); const eslint_utils_1 = require("@typescript-eslint/utils/eslint-utils"); exports.runMatchesClassType = eslint_utils_1.RuleCreator.withoutDocs({ meta: { docs: { description: 'The return type of the run method should match the Type passed to sfCommand', recommended: 'recommended', }, messages: { summary: 'The return type of the run method ({{runMethodReturnType}}) does not match the Type passed to sfCommand ({{classTypeParameter}})', }, type: 'problem', schema: [], fixable: 'code', }, defaultOptions: [], create(context) { return (0, commands_1.isInCommandDirectory)(context) ? { // eslint-disable-next-line complexity MethodDefinition(node) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; if ((0, commands_1.isRunMethod)(node) && ((_a = node.value.returnType) === null || _a === void 0 ? void 0 : _a.typeAnnotation.type) === utils_1.AST_NODE_TYPES.TSTypeReference) { // OK, run method has a type annotation. Now we need to check if the class extends SfCommand and get the <type parameter> const ancestors = context.getAncestors(); const classDeclaration = (0, commands_1.getSfCommand)(context); if (classDeclaration) { // get the text for the two nodes const sourceCode = context.sourceCode; const runType = sourceCode.getText((_c = (_b = node.value.returnType) === null || _b === void 0 ? void 0 : _b.typeAnnotation.typeParameters) === null || _c === void 0 ? void 0 : _c.params[0]); const classType = sourceCode.getText((_d = classDeclaration.superTypeParameters) === null || _d === void 0 ? void 0 : _d.params[0]); if (runType && classType && runType !== classType) { if (((_e = classDeclaration.superTypeParameters) === null || _e === void 0 ? void 0 : _e.params[0].type) === utils_1.AST_NODE_TYPES.TSUnknownKeyword) { // When Class Type is "unknown", but the run method has a return type, we can make the Class match the method. const target = (_f = classDeclaration.superTypeParameters) === null || _f === void 0 ? void 0 : _f.params[0].range; context.report({ node, messageId: 'summary', data: { runMethodReturnType: runType, classTypeParameter: classType, }, fix: (fixer) => fixer.replaceTextRange(target, runType), }); } else { const targetNode = (_g = classDeclaration.superTypeParameters) === null || _g === void 0 ? void 0 : _g.params[0]; if (targetNode) { context.report({ node: targetNode, messageId: 'summary', data: { runMethodReturnType: runType, classTypeParameter: classType, }, }); } const targetNode2 = (_j = (_h = node.value.returnType) === null || _h === void 0 ? void 0 : _h.typeAnnotation.typeParameters) === null || _j === void 0 ? void 0 : _j.params[0]; if (targetNode2) { context.report({ node: targetNode2, messageId: 'summary', data: { runMethodReturnType: runType, classTypeParameter: classType, }, }); } } } } } }, } : {}; }, });