[Code Quality] CSpell

2023-03-11 hit count image

Let's see how to check typo by CSpell.

Outline

Sometimes, the typo makes errors when we program. Also, some typos are too similar, so we spend a lot of time to find them. In this blog post, I will explain how to use CSpell to find typos.

You can see the full source code on the following link.

Install Node

To use the CSpell, you need to install Node. Install Node for your system by following.

macOS

Homebrew is a package manager for macOS to install and manage packages. You can simple install packages with Homebrew. Execute the command below to install Homebrew.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After installing, execute the following command to check Homebrew installed well.

brew --version

If the version is now shown up, exit Termital and restart it. If you already installed Homebrew, you can skip this step. Next, execute the command below to install Node.

brew install node

Windows

In Windows, Chocolatey is a package manager. Open the CMD or Powershell with Administrator privileges. Execute the command below to install Chocolatey.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

After installing, execute the following command to check Chocolatey installed well.

choco –version

If the version of Chocolatey is not shown up, exit CMD or Powershell and restart it. After installing Chocolatey, execute the command below to install Node.

choco install -y nodejs.install

Create package.json

CSpell works on the Node development environment. So, we need to create the package.json file to manage the Node packages. Execute the following command to create the package.json file.

npm init

After executing the command, you can see the many questions. If you don’t need to change the default value, just press Enter key for all of them.

package name: (cspell-example)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)

After that, you can see the package.json file created like the below.

{
  "name": "cspell-example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Install CSpell

Next, execute the following command to install CSpell.

npm install --save-dev cspell

Create script

Next, let’s create the executable script of CSpell. Open the package.json file and modify it like the below.

{
  ...
  "scripts": {
    "check:spell": "cspell '**' --gitignore --no-progress "
  },
  ...
}

We’ll use CSpell to check all files(**), and set .gitignore to the --gitignore option to ignore the files that are under the .gitignore file. Also, we set --no-progress to hide the process of checking.

Execution

To check CSpell works well, create the index.js file and modify it like the below.

function template() {
  console.log('deku');
}

And then, execute the following command to check CSpell works well.

npm run check:spell

If the CSpell works well, you can see the following message.

/study-linters/cspell-example/index.js:2:16 - Unknown word (templete)
  CSpell: Files checked: 2, Issues found: 1 in 1 files

Except specific words

If you want to except some words not checked by CSpell, you can create a dictionary and register them to it. Create the .cspell.json file and add some words that you want to except like the below.

{
  "words": ["deku"]
}

And then, execute the following command to re-check the CSpell.

npm run check:spell

Unlike before, you can see there is no error.

CSpell: Files checked: 2, Issues found: 0 in 0 files

Except specific file format

You can also except some specific file formats not checked by CSpell like the build results or snapshot files. To except specific files, open the .cspell.json file and modify it like the below.

{
  "words": ["deku"],
  "ignorePaths": ["*.snap"]
}

From now, the snapshot result file(.snap) will be ignored.

Complated

Done! we’ve seen how to use CSpell to check typo. we can’t find all typos by CSpell, but we can solve some typo issues with it. So, please try it!

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts