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

eslint-plugin-alphabetize

v1.1.1

Published

Enforce that any arbitrary code block is alphabetized

Downloads

7

Readme

eslint-plugin-alphabetize

Enforce that any arbitrary code block is alphabetized.

Usage

When to Use This

If you have a one-off section of your code where you'd like every statement to be alphabetized. For instance:

Without this plugin, you have to use comments to enforce your convention. It's easily missed or incorrectly followed.

readConfig('./general-config.json');

// Please keep this section alphabetized
readConfig('./config-site-a.json')
readConfig('./config-site-c.json')
readConfig('./config-site-b.json')
// Please keep this section alphabetized

With this plugin, you can enforce it through lint:

readConfig('./general-config.json');

// start-enforce-alphabetization
readConfig('./config-site-a.json')
readConfig('./config-site-b.json')
readConfig('./config-site-c.json')
// end-enforce-alphabetization

When Not to Use This

Don't use it for code where you consistently alphabetize. For instance, do not use this for import or require statements at the top of your file. It would be a pain to wrap every import block in your codebase in this rule's directives. Instead, use something like eslint-plugin-import.

If you want to enforce that object keys are sorted for only certain objects, use sort-keys:

/* eslint sort-keys:error */
/* eslint-disable sort-keys */

const unsortedObject = {Z, B, A};

/* eslint-enable sort-keys */
const sortedObject = { A, B, C};
/* eslint-disable sort-keys */

Usage

Wrap the section you want to be alphabetized in a pair of directive comments:

// start-enforce-alphabetization
function a() {
  // Alphabetization is only enforced at the top level where the directive comments occur.
  // The following is not a violation:
  let z;
  let a;
}
function b() {}
function c() {}
// end-enforce-alphabetization

Numeric Sorting

Sometimes, you want to sort by a number that appears on each line, instead of considering the entire string lexicographically. Imagine you have a series of requires for AB tests:

require('test-123-no-more-buttons');
require('test-456-hella-buttons');
require('test-1234-all-buttons-are-blink-tags');

Lexicographic sort would consider 1234 to come before 456. But in this case, readers may find it more intuitive to sort the test id numerically, producing the ordering above. To achieve this, use the :numeric directive:

// start-enforce-alphabetization:numeric
require('test-123-no-more-buttons');
require('test-456-hella-buttons');
require('test-1234-all-buttons-are-blink-tags');
// end-enforce-alphabetization

// What it would be without numeric:
// start-enforce-alphabetization
require('test-123-no-more-buttons');
require('test-1234-all-buttons-are-blink-tags');
require('test-456-hella-buttons');
// end-enforce-alphabetization

The numeric directive works by looking for the first match of /(\d?)/ in each statement. If the result of parseInt(regexMatch) is NaN, then as a fallback, the entire statement is sorted as a string.

Design Considerations

I'm not thrilled with introducing the custom (start|end)-enforce-alphabetization directives. However, if we only use eslint directives, it's pretty cumbersome to use:

/* eslint alphabetize/_: error */
/* eslint-disable alphabetize/_ */

let e;
let outOfOrder;
let b; 

/* eslint-enable alphabetize/_ */
const d = 2;
const c = 1; 
/* eslint-disable alphabetize/_ */

const a = 1; 

Limitations

  1. Sort criteria is not configurable.
  2. Nesting sorted blocks is not permitted.
  3. Sorting only applies to the top-level node in the scope in which the comment appears. It also does not apply to comments. For instance, the following will be considered valid:
    // start-enforce-alphabetization
    readConfig('./config-site-a.json')
    // readConfig('./config-site-z.json')
    readConfig('./config-site-c.json')
    // end-enforce-alphabetization
  4. Doesn't do a nice diff of the lines that are out of sort order; only the first unsorted line will be flagged.
  5. No fixer.
  6. The feedback to the user explaining the problem is not great. It works better when you have an already-sorted block and you need to avoid messing it up. It's more cumbersome when you're sorting a previously unsorted big block.

Future Ideas

  1. Should EOF implicitly be an endDirective?

Local Dev

If you're developing this module locally, run yarn run set-up-demo.