@sleavely/eslint-plugin-js-rules
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@sleavely/eslint-plugin-js-rules

Opinionated rules for maintainable projects.

Typescript-specific rules is a separate plugin; @sleavely/eslint-plugin-ts-rules

Installing

npm i --save-dev @sleavely/eslint-plugin-js-rules
// .eslintrc.cjs
module.exports = {
  // ..
  plugins: [
    '@sleavely/js-rules',
  ],
  rules: {
    '@sleavely/js-rules/destructure-env': ['error', 'always'],
    '@sleavely/js-rules/uppercase-env': ['error', 'always'],
  }
}

Rules

js-rules/destructure-env

Environment variables that affect how the application runs should be easy to find.

Setting default values that are strings will ensure that you never have to deal with "possibly undefined" cases.

// ✅ Destructured and have default string values
const {
  ENVIRONMENT = 'dev',
  POTATO = '',
} = process.env
console.log(POTATO)

// ❌ Needs destructuring
console.log(process.env.POTATO)

// ❌ Needs destructuring
const { env } = process
console.log(env.POTATO)

// ❌ Needs destructuring
const { env: renamedEnv } = process
console.log(env.POTATO)

// ❌ Needs destructuring
const env = process.env
console.log(env.POTATO)

// ❌ Needs a default string value
const { POTATO } = process.env
console.log(POTATO)
// ✅
import foo from 'foo'
const {
  ENV_VAR = ''
} = process.env
export const foobar = () => foo()

// ❌ Only import/require may appear prior to process.env
import foo from 'foo'
export const foobar = () => foo()
const {
  ENV_VAR = ''
} = process.env

// ❌ process.env must be destructured in root scope
export const logEnv () => {
  const {
    ENV_VAR = ''
  } = process.env
  console.log(ENV_VAR)
}

js-rules/uppercase-env

Environment variables are constants throughout your applications lifecycle. Uppercasing environment variables helps distinguish them from other identifiers.

// ✅
const {
  POTATO = ""
} = process.env

// ✅
console.log(process.env.POTATO)

// ❌
const {
  potato = ""
} = process.env

// ❌
console.log(process.env.potato)

Package Sidebar

Install

npm i @sleavely/eslint-plugin-js-rules

Weekly Downloads

10

Version

1.0.1

License

MIT

Unpacked Size

13.7 kB

Total Files

8

Last publish

Collaborators

  • sleavely