npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tagmeme-analyzer

v1.2.0

Published

Static code analyzer and CLI tool for javascript to verify correctness of pattern matching when using tagmeme

Downloads

1

Readme

tagmeme-analyzer

Build Status npm Coverage Status

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:

import { union } from "tagememe" 

const Option = union([ "Some", "None" ])

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.Some("green");

Now that we have a value, we can pattern-match against it:

const colorValue = Option.match(color, {
    Some: value => value, 
    None: () => "blue"
})

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

import { union as makeUnion } from 'tagmeme'
export const Numbers = makeUnion([ 'One', 'Two', 'match' ]);
export const Option = makeUnion([ 'Some', 'None' ]);
export const Result = makeUnion([ 'Ok', 'Error' ]);
export const Deplicates = makeUnion([ 'First', 'First' ]);

{repo}/sample/app.js

import { Option, Result } from './types'

const color = Option.Some('green')

// Correct usage, no errors
const colorValue = Option.match(color, {
    Some: colorName => colorName, 
    None: () => 'blue'
});

// Incorrect union constructor
const invalid = Option.Nome();

// Type name misspelled: 'Option' => 'Opion'
const otherValue = Opion.match(color, {
    Some: colorName => colorName, 
    None: () => 'blue'
});

// Error misspelled => 'Erro'
const firstResult = Result.match(Result.Ok('success'), {
    Ok: value => value, 
    Erro: () => 'blue'
});

// Handling too many cases, case 'Other' is not declared
const secondResult = Result.match(Result.Ok('success'), {
    Ok: value => value, 
    Error: () => 'blue', 
    Other: () => 'too many cases handled'
});

// redundant catchAll argument
const withCatchAll = Option.match(color, {
    Some: colorName => colorName, 
    None: () => 'blue'
}, () => "default-color");

Now running tagmeme-analyzer against {repo}/sample/app.js gives the following:

example

Current Limitations (PRs <=> :heart:)

  • 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