JavaScript插件对函数复杂性和函数长度的误报

JavaScript plugin false positives for function complexity and function length

本文关键字:函数 插件 复杂性 JavaScript      更新时间:2023-12-27

我们的JavaScript插件在函数复杂性和函数长度规则方面不断出现误报问题。

原因是我们有写在函数内部的函数,而外部函数会出现问题。我知道,从技术上讲,复杂性会影响一切,但是,难道没有办法让插件只关注函数本身吗?(除了标记为假阳性)

服务器版本4.5.6JavaScript插件版本2.9

重新部署Ctrl函数存在规则"javascript:FunctionComplexity"(复杂性=25)的复杂性问题。

这就是代码,正如你所看到的,实际的复杂性来自于内部函数。

除了将问题标记为假阳性(并失去内部函数的复杂性问题)或编写自定义规则之外,还有什么方法可以解决这个问题吗?

谢谢。

function redeployCtrl($scope, utilService, $filter, $location, generalSettings, $uibModalInstance, $uibModal, $timeout,scheme) {
$scope.openStart = openStart;
$scope.isSubmitable = isSubmitable;
$scope.ipCheckbox = ipCheckbox;
$scope.deploy = deploy;
$scope.init = init;
$scope.cancel = cancel;
function init() {
    $scope.scheme = scheme;
    $scope.loading = 'false';
    $scope.envSchemes = [];
    $scope.isPermanent = false;
    $scope.permanent = {};
    $scope.scheme.Scheme.Description = null;
    $scope.scheme.Scheme.ExpTime = null;
    var max = generalSettings.CalendarEndDate;
    $scope.maxDate = new Date();
    $scope.maxDate.setMonth($scope.maxDate.getMonth() + max);
    $scope.minDate = new Date();
    $scope.minDate = $scope.minDate.setDate($scope.minDate.getDate() + generalSettings.MinExpirationDate);
    $scope.dateOptions = {
        'year-format': 'yyyy',
        'starting-day': 1
    };
    utilService.post(generalSettings.serverPath + 'envscheme/ListSupervisors/', { })
      .then(function (data) {
          $scope.supervisors = data;
      }).catch(function (data) {
          utilService.setError(data.ExceptionMessage, "Failed to retrieve data", "img_error");
      });

    utilService.post(generalSettings.serverPath + 'envscheme/ListPermReasons/', { })
    .then(function (data) {
        $scope.permReasons = data;
    }).catch(function (data) {
        utilService.setError(data.ExceptionMessage, "Failed to retrieve data", "img_error");
    });
}
function openStart() {
    $timeout(function () {
        $scope.startOpened = true;
    });
}
function deploy(scheme, isPermanent) {
    if (isPermanent) {
        scheme.Scheme.ExpTime = '01/01/9999';
        scheme.Scheme.ApprovedBy = $scope.permanent.approvedBy;
        if ($scope.permanent.mainReason === 'Other') {
            scheme.Scheme.Reason = $scope.permanent.customReason;
        } else {
            scheme.Scheme.Reason = $scope.permanent.mainReason;
        }
    } else {
        $scope.scheme.Scheme.ExpTime = utilService.getFormattedDate($scope.scheme.Scheme.ExpTime);
    }
    $scope.loading = 'true';
    utilService.post(generalSettings.serverPath + 'envscheme/ReCreateEnv', scheme)
     .then(function (data) {
         if (data.Success) {
             utilService.alertAmpm("Deploy started successfuly", "Info", "img_information");
             $location.path("/MyEnvironments");
         }
         else {
             utilService.alertAmpm(data.Reason, "Failed to Re-Deploy", "img_error");
             $scope.loading = 'false';
         }
         if (data.Reason.indexOf("Session was not found") < -1) {
             sessionStorage.clear();
             $scope.loading = 'false';
         }
     }).catch(function (data) {
         utilService.setError(data.ExceptionMessage, "Failed to Re-Deploy", "img_error");
         $scope.loading = 'false';
     });
}
function isSubmitable(invalid, modules) {
    if (!invalid) {
        for (var i = 0; i < modules.length; i++) {
            if (modules[i].ipchkBox) {
                if (!modules[i].OS.Parameters.IP) {
                    return true;
                }
            }
        }
        return false;
    }
    return true;
}
function ipCheckbox(checkBox, name) {
    if (!checkBox) {
        var name1 = "ipText" + name;
        $('[name=' + name1 + ']').val('');
        $scope.scheme.Scheme.modules[name].OS.Parameters = new Object();
    }
}
function cancel() {
    $uibModalInstance.dismiss('cancel');
}

罗伊。

这些规则强制使用较少嵌套的函数进行编码。它们忽略了一些情况,如IIFE或某些框架(AMD、Angular)中接受的实践。但你显然不是这样。

因此,如果你认为这种编码实践不适合你,你唯一能做的就是禁用规则(并可能创建只计算你想要的行数的自定义规则)。