【问题标题】:Detecting typos in JavaScript code检测 JavaScript 代码中的拼写错误
【发布时间】:2015-09-23 00:23:39
【问题描述】:

在 Python 世界中,使用最广泛的静态代码分析工具之一,pylint 有一个 special check,可以检测 cmets 和 docstrings 中的拼写错误。

有没有办法使用静态代码分析检测 JavaScript 代码中的拼写错误


为了让问题更具体,这里是一个我想引起警告的示例代码:

// enter credntials and log in
scope.loginPage.logIn(browser.params.regularUser.login, browser.params.regularUser.password);

这里credntials 拼写错误。

【问题讨论】:

    标签: javascript comments jshint eslint static-code-analysis


    【解决方案1】:

    您可以使用cspell。这是一个非常方便的命令行工具,可以发现 JavaScript、TypeScript、Python、PHP、C#、C++、LaTex、Go、HTML 和 CSS 源代码中的拼写错误。

    示例的输出

    cspell ./src/code.js
    code.js:1:10 - Unknown word (credntials)
    

    ./src/code.js

    // enter credntials and log in
    scope.loginPage.logIn(browser.params.regularUser.login, browser.params.regularUser.password);
    

    【讨论】:

      【解决方案2】:

      有一个专门为该任务构建的eslint 插件-eslint-plugin-spellcheck

      eslint 插件,用于对 javascript 文件的标识符、字符串和 cmets 进行拼写检查。

      它基于hunspell-spellchecker 库,该库知道如何解析和使用Hunspell 已知单词的字典:

      Hunspell 是一个拼写检查器和形态分析器,专为 具有丰富形态和复杂词复合的语言和 字符编码,最初是为匈牙利语设计的。

      这是问题中提供的示例代码的输出:

      $ node_modules/eslint/bin/eslint.js -c eslint.json showCase.spec.js
      25:8   warning  You have a misspelled word: credntials on Comment  spellcheck/spell-checker
      

      该插件易于定制,可以签入 cmets、标识符和字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多