Detecting typos in JavaScript code
Asked Answered
G

2

7

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.

Gharry answered 5/7, 2015 at 23:24 Comment(0)
G
7

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.

Gharry answered 5/7, 2015 at 23:24 Comment(0)
S
5

You can use cspell. It's a very handy command line tool and finds spelling mistakes in JavaScript, TypeScript, Python, PHP, C#, C++, LaTex, Go, HTML and CSS sources.

Output for your example:

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);
Springspringboard answered 7/4, 2020 at 14:54 Comment(1)
Upvoting because they have incredible documentation (cspell.org) and they have an eslint plugin that's really easier to configure. Worked right out of the box for me.Shaefer

© 2022 - 2024 — McMap. All rights reserved.