Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting typos in JavaScript code

In Python world, one of the most widely-used static code analysis tools, pylint has a special check, that detects typos in comments and docstrings.

Is there a way to detect typos in JavaScript code using static code analysis?


To make the question more specific, here is an example code I want to cause a warning:

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

Here credntials is misspelled.

like image 331
alecxe Avatar asked Jul 05 '15 23:07

alecxe


People also ask

How do I check for JavaScript errors?

Right-click anywhere in the webpage and then select Inspect. Or, press F12 . DevTools opens next to the webpage. In the top right of DevTools, the Open Console to view errors button displays an error about the webpage.

How do you use typo in JavaScript?

To use Typo, simply load it like so: var Typo = require("typo-js"); var dictionary = new Typo(lang_code); Typo includes by default a dictionary for the en_US lang_code.


1 Answers

There is a eslint plugin built specifically for the task - eslint-plugin-spellcheck:

eslint plugin to spell check words on identifiers, Strings and comments of javascript files.

It is based on the hunspell-spellchecker library that knows how to parse and use Hunspell dictionaries of known words:

Hunspell is a spell checker and morphological analyzer designed for languages with rich morphology and complex word compounding and character encoding, originally designed for the Hungarian language.

Here is what it outputs for the example code provided in the question:

$ 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

The plugin is easily customizable and can check in comments, identifiers and strings.

like image 147
alecxe Avatar answered Sep 24 '22 02:09

alecxe