【问题标题】:How to supress the error : Illegal space before opening round brace at from jsrc?如何抑制错误:在从 jsrc 打开圆括号之前存在非法空间?
【发布时间】:2023-09-24 20:31:01
【问题描述】:

在我们的 angularjs 项目中,在 gulp3 升级到 4 之后,当我运行时:gulp lint

C:\filepath>gulp lint
[12:59:30] Using gulpfile ~\filepath\gulpfile.js
[12:59:30] Starting 'lint'...
app\js\app.config.js: line 4, col 1, Use the function form of "use strict".

1 error
Illegal space before opening round brace at C:\filepath\app\js\app.config.js :
     7 |angular
     8 |    .module('test')
     9 |    .config(['localStorageServiceProvider', function (localStorageServiceProvider) {
C:\filepath>gulp lint
[12:59:30] Using gulpfile ~\filepath\gulpfile.js
[12:59:30] Starting 'lint'...
app\js\app.config.js: line 4, col 1, Use the function form of "use strict".

1 error
Illegal space before opening round brace at C:\filepath\app\js\app.config.js :
     7 |angular
     8 |    .module('test')
     9 |    .config(['localStorageServiceProvider', function (localStorageServiceProvider) {

我无法弄清楚,之前一切正常。只有在 gulp 升级到最新版本之后,构建才会失败。谁能帮我解决这个问题。

我也关注了这个链接:How can I suppress the JSHint "JSCS: Illegal Space" warnings in Visual Studio 2013?

根据上面的链接,我尝试在 .jshintrc 文件中添加以下行:

"requireSpacesInAnonymousFunctionExpression": {
    "beforeOpeningCurlyBrace": false
},
"disallowSpacesInNamedFunctionExpression": {
    "beforeOpeningRoundBrace": false
},
"disallowSpacesInFunctionDeclaration": {
    "beforeOpeningRoundBrace": false
}

继上一个之后抛出更多错误:

6 code style errors found.
app\js\app.module.js: line 0, col 0, Bad option: 'requireSpacesInAnonymousFunctionExpression'.
app\js\app.module.js: line 0, col 0, Bad option: 'disallowSpacesInNamedFunctionExpression'.
app\js\app.module.js: line 0, col 0, Bad option: 'disallowSpacesInFunctionDeclaration'.
app\js\app.module.js: line 4, col 1, Use the function form of "use strict".

我需要的是:

我的编辑器 webstorm 自动为对齐应用空间,如下所述:

function (localStorageServiceProvider) // with space

但是,应该是:

function(localStorageServiceProvider) // without space

我需要在 .jshintrc 文件或任何其他修复的地方应用什么规则?

【问题讨论】:

    标签: javascript angularjs gulp jshint lint


    【解决方案1】:

    如果您愿意,可以将 Webstorm 配置为不插入空格。我这里没有 Webstorm,但在 IntelliJ IDEA(相同的底层 IDE)中,该选项位于

    Preferences->Editor->Code Style->Javascript->Spaces->Before Parentheses->In function expression

    【讨论】:

    • 非常感谢您@CupawnTae 的回复。但是文件太多了,到目前为止我们还没有遇到过这样的问题。超过 100 人在我们的项目中工作,我不能说他们更改 webstorm 中的设置,这现在听起来很不靠谱。我也面临错误:Line must be at most 120 characters at C:\filepath\somanyfiles.js。对 jshint 全局抑制的任何帮助将不胜感激,并再次感谢您
    【解决方案2】:

    您需要配置的 lint 规则是:

    "requireSpacesInAnonymousFunctionExpression": {
        "beforeOpeningRoundBrace": true,
        "beforeOpeningCurlyBrace": true
    }
    

    "requireSpacesInFunctionExpression": {
        "beforeOpeningRoundBrace": true,
        "beforeOpeningCurlyBrace": true
    }
    

    【讨论】:

    • 错误:2 errors filepath\file.js: line 0, col 0, Bad option: 'requireSpacesInAnonymousFunctionExpression'. filepath\file.js: line 0, col 0, Bad option: 'requireSpacesInFunctionExpression'.
    • 你必须加载这些规则。你可以在这里找到规则:github.com/jscs-dev/node-jscs/tree/master/lib/rules
    【解决方案3】:

    我解决了这个添加:"excludeFiles": ["**/*"].jscrc 文件中。 .jscrc 文件的最终版本如下所示:

    {
      "preset": "google",
      "fileExtensions": [ ".js" ],
    
      "maximumLineLength": 120,
      "validateIndentation": 4,
    
      "disallowSpacesInAnonymousFunctionExpression": null,
    
      "excludeFiles": ["**/*"]
    }
    

    【讨论】:

    • 现在你什么都没有了。最好完全摆脱 jshint。