3

Spell check your Erlang code with Sheldon

 2 years ago
source link: https://medium.com/erlang-battleground/spell-check-your-erlang-code-with-sheldon-b60e1bb648e5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spell check your Erlang code with Sheldon

“Scissors cuts paper. Paper covers rock. Rock crushes lizard.”

In the article “Sheldon: Erlang spell checker”, I described the fantastic library Sheldon and how it can be used for spell checking stuff. Today, we will discuss the Erlang plugin rebar3_sheldon that uses Sheldon to spell check Erlang source code. It’s a rebar3 plugin that we developed together with

.

What is rebar3_sheldon?

How many times did you get tickets to fix spelling issues? I don’t know about you, but in my career, it happened a lot… It is terrible when a well-known product sends a response message with a spelling error in response to a client request. Or when some vital info provided in code has spelling issues that make it difficult to understand or read. Worry no more! There is a solution for this: The new rebar3 plugin rebar3_sheldon that checks the spelling of your Erlang code!! 🎉

This plugin is built using rebar3_plugin, but we will discuss this template repository in an upcoming article. rebar3_sheldon will spellcheck all binaries, strings, and comments in all source files (or the ones you specify) of any Erlang project by traversing their ASTs.

How to use it?

So, let’s look at how rebar3_sheldon can be used and what we can expect from its output. To test the plugin on your project, follow the following steps:

  • Add the plugin into your rebar.config file:
{project_plugins, [{rebar3_sheldon, "~> 0.3.1"}]}.
  • Run thespellcheck command
$ rebar3 spellcheck
===> Youre welcome. And if he has twins, we can do all kinds of neat experiments on them.:
test/test_SUITE.erl:1: The word "Speling" in binary is unknown. Maybe you wanted to use "speeling" or "speiling" or ....?
test/test_SUITE.erl:2: The word "fdfdf" in string is unknown.
test/test_SUITE.erl:3: The word "Unicode" in comment is unknown. Maybe you wanted to use "uncoded"?

As you can see, the plugin has a friendly output format, akin to those produced by rebar3_format, rebar3_hank, and other famous tools.

More Options

You can check what other attributes can be passed in the shell using the -h command:

$ rebar3 -h spellcheck
Plugin for spellchecking with Sheldon
Usage: rebar3 spellcheck [-f files] [-i ignore] [-r ignore_regex]
[-d default_dictionary]
[-a additional_dictionaries]

-f, --files List of files to check
-i, --ignore List of ignored files
-r, --ignore_regex Regular expressions to ignore
-d, --default_dictionary Set the default dictionary
-a, --additional_dictionaries List of additional dictionaries

Let’s take a deeper look at each one of them…

  • The attribute -f or --files expects a list of paths to files separated by commas. If you provide that argument, only those files will be checked by the plugin.
$ rebar3 spellcheck -f 'src/*.erl, test/*.erl'
# Same as
$ rebar3 spellcheck --files='src/*.erl, test/*erl'
  • The attribute -i or --ignore expects a list of paths to files separated by commas. The plugin will skip those files for spell check.
$ rebar3 spellcheck -i 'test/*.erl, include/*.hrl'
# Same as
$ rebar3 spellcheck --ignore='test/*.erl, include/*.hrl'
  • The attribute -r or --ignore_regexp expects a string containing a regular expression. All strings that match the given regex will be ignored in all files. This allows you, for instance, to ignore strings containing paths or other configuration values.
$ rebar3 spellcheck -i '[_@./#&+-=*]'
# Same as
$ rebar3 spellcheck --ignore='[_@./#&+-=*]'
  • The attribute -d or --default_dictionary expects the path to a custom dictionary which the plugin will use to replace the default one.
$ rebar3 spellcheck -d 'path/to/dictionary.txt'
# Same as
$ rebar3 spellcheck --default_dictionary='path/to/dictionary.txt'
  • The attribute -a or --additional_dictionaries expects a list of paths to additional dictionary files separated by comma to expand the default dictionary.
$ rebar3 spellcheck -a 'dict1.txt, dict2.txt'
# Same as
$ rebar3 spellcheck --additional_dictionaries='dict1.txt, dict2.txt'

Per-Project Configuration

To configure rebar3_sheldon only once, you can add the following into rebar.config:

{spellcheck, [
{files, ["src/*.erl", "src/*/*.erl", "include/*.hrl"]},
{ignore, ["src/*_ignore.erl"]},
{ignore_regex, "[_@./#&+-=%*]"},
{default_dictionary, "path/to/default_dictionary.txt"},
{additional_dictionaries, ["/dict_1.txt", "/dict_2.txt"]}
]}.

At the same time, if these values are provided in rebar.config and also, as command arguments, the duplicated config options will be replaced by the arguments from the command line.

If you don’t set any config option in rebar.config, the plugin will use the default one:

{spellcheck, [
{files, ["include/**/*.[he]rl",
"include/**/*.app.src",
"src/**/*.[he]rl",
"src/**/*.app.src",
"test/**/*.[he]rl",
"test/**/*.app.src",
"{rebar,elvis,sys}.config"]}
]}.

Future Development

The plugin is still in its infancy. We desperately need more beta testers to use it in their projects and report as many bugs as you can find.

In the meantime, we already have some plans to…

  • Improve the way we work with the AST.
  • Allow for the usage of special tags in code to ignore certain pieces of the modules when spellchecking them.
  • Implement better support for Unicode.

If you have any ideas, improvements, or opinions, you can find us in the #sheldon room of the Erlang Slack. We need all the help we can find :)

References


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK