【问题标题】:javascript not validating, jslint displaying incorrect errorsjavascript 未验证,jslint 显示不正确的错误
【发布时间】:2015-03-26 07:36:08
【问题描述】:

我的代码在下面,有人可以进入 jslint 并粘贴我的代码并告诉我为什么我的错误会发生并修复。 例如,它说在定义之前使用了“描述”,但在 angularjs 网站上的页面中,它们没有显示任何定义。 另一个错误是“意外字符''”。在第 2 行字符 42 上。也有 false,因为没有缺少空格。

describe('forgotPasswordCtrl', function () {
    beforeEach(module('forgotPasswordApp'));    
    var $controller;
    var returnMsg = 'Forgot password response message';
    beforeEach(inject(function(_$controller_){
        $controller = _$controller_;
    }));

    describe('$scope.finish', function(){
        it('returns a -forgot password response- message', function(){
            var $scope = {};
            var controller = $controller('forgotPasswordCtrl', { $scope: $scope });
            $scope.finish();
            expect($scope.globalError).toEqual(returnMsg);

            });
        });



    describe('$scope.cancel', function(){
        it('assigns a url depending on a customer match happens', function(){
            var $scope = {};
            var controller = $controller('forgotPasswordCtrl', { $scope: $scope });
            $scope.cancel();
            expect(window.location.assign).toEqual("/web/tfgm_customer");

            });
        });

【问题讨论】:

  • describe 定义为全局变量。你将不得不为很多全局的事情做这件事。
  • 你如何定义 describe 为全局?
  • 显示全球的文本框...
  • 在选项中...大的白色文本框,上面写着:predefine global variables here
  • 不确定您在哪里谈论 tbh m8。它不在 JSLint 中,也不在记事本 ++ 或命令行中,它们是我用于此的唯一 3 件事

标签: javascript angularjs jslint


【解决方案1】:

哇。好的,所以您并不总是能够让库正确 lint。如果这段代码应该在上下文中工作,那么就会有很多意大利面条。

这是我对你给出的最好的猜测。让我知道我做错了什么。

注意:您需要在文件顶部添加一些 /*jslint ... */ 指令。

  1. sloppy:true: 允许你去掉'use strict'
  2. white:true: 允许不分青红皂白的空格
  3. nomen:true:允许在变量名的开头和结尾使用下划线 (_)。
  4. browser:true:这里,允许使用window

还要注意/*global ... */ 指令,您可以在其中放置所有这些由于某些奇怪的原因不在命名空间中的函数。

我们开始吧:

/*jslint sloppy:true, white:true, nomen:true, browser:true */
/*global describe, module, beforeEach, inject, it, expect */

// You have some bad scoping below. I *think* you expect
// $controller, controller, & returnMsg to exist in full 
// script scope.
var returnMsg, $controller = null, controller = null;

describe('forgotPasswordCtrl', function () {
    beforeEach(module('forgotPasswordApp'));

    returnMsg = 'Forgot password response message';
    beforeEach(inject(function(_$controller_){
        // You're never doing anything with this.
        // JSLint wants to know why not.
        $controller = _$controller_;
    }));
});


describe('$scope.finish', function(){
    it('returns a -forgot password response- message', function(){
        var $scope = {};
        controller = $controller('forgotPasswordCtrl', { $scope: $scope });
        $scope.finish();
        expect($scope.globalError).toEqual(returnMsg);
    });
});

describe('$scope.cancel', function(){
    it('assigns a url depending on a customer match happens', function(){
        var $scope = {};
        controller = $controller('forgotPasswordCtrl', { $scope: $scope });
        $scope.cancel();
        expect(window.location.assign).toEqual("/web/tfgm_customer");
    });
});

$controller.usedSomehow();      // to make JSLint happy. Please use.
controller.usedSomehowAsWell(); // to make JSLint happy. Please use.

老实说,这里发生了很多古怪的事情,我不知道该怎么做。我认为您需要更好地描述您认为自己在做什么(这显然是(?)单元测试的东西),但诸如...

var $scope = {};
controller = $controller('forgotPasswordCtrl', { $scope: $scope });

没有多大意义。 $scope 是你传递给一个属性的对象,在传递给 $controller 的匿名对象中也称为 $scope?为什么?为什么不...

controller = $controller('forgotPasswordCtrl', { $scope: {} });

... ?我的猜测是,在您真正开始 linting 代码之前,您需要在不同的 scopes 中围绕此代码发生很多事情。

也就是说,它是否按原样工作


评论后编辑:

如果你得到这样的东西......

Unexpected character '(space)'.

    beforeEach(module('forgotPasswordApp'));    

...那是因为您在 JSLint 指令中没有 white:true。查看JSLint instructions page 了解有关所有这些选项的更多信息,其中提到了white

true 如果应该忽略严格的空格规则。

您看到的错误有点误导。如果您在行尾有 任何 个尾随空格,则可能会引发该错误。

【讨论】:

  • 我认为这可能是因为我不太了解范围。我需要它向我解释,就好像我是一个 5 岁的孩子一样。也许我有时。但说真的,我想知道发生了什么。我问这个问题的主要目的是因为当我运行 karma start 时,我收到一个错误消息 invalid character .,即使它指向的行上没有句号。 (我认为第 5 行)
  • 如果您看到的是“意外字符'(空格)',我进行了快速编辑以解决。如果有更多代码,请创建pastebin,我可以看看。但问题仍然存在,我认为:您能告诉我们您希望代码做什么吗?
  • 我会在一秒钟内制作一个 pastebin,我目前有一个不同的错误,说 Uncaught ReferenceError: registerInterceptors is not defined
【解决方案2】:

我看看 JSHint 是怎么说的。 JSHint 没有 JSLint 严格,更易于阅读,但功能较少。

您缺少括号和括号。它对很多事情说“未定义”,因为您没有定义很多事情,从“描述”函数开始。您正在使用尚未声明的全局函数和变量。如果这是由另一个脚本/库添加的,jslint 或 jshint 不会为您猜测它。如果您希望它停止抱怨,只需在顶部添加一行 var describe, module, beforeEach, inject, it, expect;

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 2016-04-24
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多