JavaScript index-naming-hell, what is it and how to avoid it

I used to fall into the same trap…

screenshot of Visusal Studio Code with many index.tsx files open
Look at all those index files

This is how it looks like in the explorer

This works great and in practice it’s easy to use. You can just do

import MyComponent from './components/MyComponent

but when you are editing them in your editor it’s ugly and takes up too much space. It also wrecks quick file navigation since all your files are named… guess what – index.

In this case it’s fine to add some redundancy. If your component is called BirthdayItem, then feel free to rename all your files to BirthdayItem with the proper extension then create an index file that exports your component. So you don’t need to

import MyComponent from './components/MyComponent/MyComponent

Let’s see how it looks in action:

A lot better, now you can actually see what you are editing
Explorer is clean as well

As you can see both the open editors and explorer is a lot more readable. Quick file search also reacts better and easier to find what you are looking for.

Now let’s talk about Intellisense. Whenever I type BirthdayList, VS Code wants to import it from ./components/BirthdayList/BirthdayList. Before the restructure it imported it from the directory like this ./components/BirthdayList. You can clearly see that the latter is the better one. You might think about using the following code and you will get the bad result – just like me.

export { default } from "./BirthdayList";

How do we fix this? In my codebases I try to avoid export default as much as possible. So you can either do it by importing then reexporting as default, like this:

import BirthdayList from "./BirthdayList";
export default BirthdayList;

But as I said, I’d rather avoid export default at all costs:

import BirthdayList from "./BirthdayList";
export { BirthdayList };

This way you will get awesome autocompletion and auto imports without the filename.

Linting markdown files using Node.js

Install dependencies

# Yarn
yarn add -D markdownlint markdownlint-cli2

# NPM
npm install -D markdownlint markdownlint-cli2

Add script to your package.json

"lint:md": "markdownlint-cli2 README.md"

Feel free to replace README.md with any files/globs that you want to lint

Using it with Prettier

Install dependencies

# Yarn
yarn add -D prettier

# NPM
npm install -D prettier

Update your script

"lint:md": "prettier --check README.md && markdownlint-cli2 README.md"

If you have long lines then you might find that Prettier doesn’t error, but markdownlint does. You can either disable line-height rule in markdown lint or enable prose-wrap in your Prettier configuration. You can do this by adding the following part to your package.json

"prettier": {
  "proseWrap": "always"
},

Linting package.json using Node.js

Install dependencies

# Yarn
yarn add -D npm-package-json-lint npm-package-json-lint-config-default

# NPM
npm install -D npm-package-json-lint npm-package-json-lint-config-default

Configure npm-package-json-lint by adding it to your package.json

"npmpackagejsonlint": {
  "extends": "npm-package-json-lint-config-default"
}

Add script to your package.json

"lint:package": "npmPkgJsonLint ."

Check with Prettier

Install dependencies

# Yarn
yarn add -D prettier

# NPM
npm install -D prettier

Include Prettier check in your script

"lint:package": "prettier --check package.json && npmPkgJsonLint ."

Linting CSS in Create React App project

Install dependencies

# Yarn
yarn add -D stylelint stylelint-config-standard

# NPM
npm install -D stylelint stylelint-config-standard

Configure your stylelint

"stylelint": {
  "extends": [
    "stylelint-config-standard"
  ]
}

Add script to your package.json

"lint:css": "stylelint src/**/*.css"

Stylelint with Prettier

Install dependencies

# Yarn
yarn add -D stylelint-prettier stylelint-config-prettier

# NPM
npm install -D stylelint-prettier stylelint-config-prettier

Configure your stylelint with prettier rules

"stylelint": {
  "extends": [
    "stylelint-config-standard",
    "stylelint-prettier/recommended"
  ]
}

Setting up Prettier for Create React App

Install dependencies

# Yarn
yarn add -D prettier eslint-config-prettier eslint-plugin-prettier

# NPM
npm install -D prettier eslint-config-prettier eslint-plugin-prettier

By default your eslintConfig looks like this:

  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  }

Let’s add Prettier and Prettier React to the extends array:

"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest",
      "plugin:prettier/recommended",
      "prettier/react"
    ]
}

Your editor should pick up the eslint config change after saving package.json. Let’s add two scripts to our package.json, one for formatting and one for linting.

"format:js": "prettier --write src/**/*.{js,jsx,ts,tsx}",
"lint:js": "eslint src/**/*.{js,jsx,ts,tsx}"

(NOTE: I used js,jsx,ts,tsx in the patterns above. If you don’t have any files that match any of those patterns, then you will get an error. Feel free to remove extensions that you aren’t using)

If you are using TypeScript then you might want to add type checking to the lint script as well:

"lint:js": "tsc --noEmit && eslint src/**/*.{js,jsx,ts,tsx}"

Automagically lint before git commit

You might want to automatically run the lint script before git commits. This can be achieved easily by using husky and lint-staged. Let’s start by installing them as dev dependencies:

# Yarn
yarn add -D husky lint-staged

# NPM
npm install -D husky lint-staged

Next you just need to add a few lines to your package.json:

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx}": [
      "yarn lint:js"
    ]
  }