tagmeme-analyzer
Static code analyzer and CLI tool for javascript to verify correctness of pattern matching when using tagmeme by andrejewski
The problem
When using tagmeme
, you have to define the union cases as strings, here is an example:
const Option =
We are declaring the Option
type that can either be Some
or None
. These two cases are considered constructors when creating a value of the Option
type:
const color = Option;
Now that we have a value, we can pattern-match against it:
const colorValue = Option
I really like the API but because this is javascript, it can be very error prone when working with a large application, there are many things can go wrong in runtime causing the match
to throw an exception:
- When forgetting to handle a case (or misspelling the case name)
- When handling a case that wasn't declared (handling too many cases)
- When there is a redundant
catchAll
argument that will never match (see docs) - Using
match
as a union case - Using duplicate union cases in the declaration
- Misspelling the union case when constructing a value
Solution: Static code analysis
Because there are known variables where things could go wrong at "compile" time, why not write a program that checks the correctness of pattern matching? This is what this project provides implemented as a cli tool for easy integration with existing projects:
npm install --save-dev tagmeme-analyzer
Let's see it in action: here is a sample code with the errors that get generated:
{repo}/sample/types.js
const Numbers = ;const Option = ;const Result = ;const Deplicates = ;
{repo}/sample/app.js
const color = Option // Correct usage, no errorsconst colorValue = Option; // Incorrect union constructorconst invalid = Option; // Type name misspelled: 'Option' => 'Opion'const otherValue = Opion; // Error misspelled => 'Erro'const firstResult = Result; // Handling too many cases, case 'Other' is not declaredconst secondResult = Result; // redundant catchAll argumentconst withCatchAll = Option;
Now running tagmeme-analyzer
against {repo}/sample/app.js
gives the following:
Current Limitations (PRs <=> ❤️)
- Analyzer runs against one root file (local imports are traversed just to find declarations)
- Imported union type declarations cannot be aliased
- Only ES6 exports for now
- Types of union case data are not checked