Skip to content

Latest commit

 

History

History
155 lines (120 loc) · 4.6 KB

README.md

File metadata and controls

155 lines (120 loc) · 4.6 KB

eslint-config-gbv

NPM package name License

Internal linting rules for GBV/coli-conc projects.

Table of Contents

Install

npm i -D eslint-config-gbv@"~2.4"

eslint-config-gbv v2.x only works with ESLint v9.x and the new flat config format. It is recommended to use the same minor version of ESLint in your project as is used in this project (as minor updates in ESLint can include rule changes). Current, this is ESLint v9.14:

npm i -D eslint@"~9.14"

For older versions of ESLint (using the previous config format), use eslint-config-gbv v1.1.0.

Usage

Create a eslint.config.js file with the following content:

import gbv from "eslint-config-gbv"
export default gbv

If you need to include your own rules, or to add Vue.js-specific rules, do this:

import gbv from "eslint-config-gbv"
import vue from "eslint-config-gbv/vue"
// import vue from "eslint-config-gbv/vue2" // use this for Vue.js 2

export default [
  ...gbv,
  ...vue,
  // Your own rules or other configs/plugins here:
  {
    // ...
  },
]

See also: https://eslint.org/docs/latest/use/configure/combine-configs

Please enforce linting before a commit in all projects.

Enforce Linting

You can enforce linting before a commit on all staged files by utilizing the packages pre-commit and lint-staged:

npm i -D pre-commit lint-staged

Then add the following to package.json:

{
  "scripts": {
    "lint-staged": "lint-staged"
  },
  "lint-staged": {
    "**/*.js": [
      "eslint --fix"
    ],
    "*.js": [
      "eslint --fix"
    ]
  },
  "pre-commit": "lint-staged"
}

Make sure to add other file extensions as necessary.

Additionally, you can add linting to your Mocha tests as well. Create a file test/eslint.js (or test/eslint.mjs in CommonJS projects) with the following content:

import assert from "node:assert"
import { loadESLint } from "eslint"

const DefaultESLint = await loadESLint()
const eslint = new DefaultESLint()
const results = await eslint.lintFiles([
  "**/*.js",
  // Add more file patterns here if necessary
])

describe("ESLint Errors", () => {

  results.forEach(result => {
    it(result.filePath, (done) => {
      assert(
        result.errorCount === 0,
        "\n" + result.messages.map(m => `\t\t${m.line}:${m.column}\terror\t${m.message}\t${m.ruleId}`).join("\n"),
      )
      done()
    })
  })

})

Each file will be its own test case and errors will be appropriately reported.

Rules

General

Using ESLint's recommended rules as a base, with the following overriding rules:

  • Indentation: Two spaces, indent case in switch statements.
  • Unix style line endings.
  • Double quotes.
  • Disallow quotes around object literal property names if they are not needed.
  • No semicolons.
  • Enforce comma dangle on multiline statements (see this).
  • Allow console.
  • Control statement brace style:
    • Enforce curly braces for all control statements.
    • Disallow single-line curly braces.
  • Allow caught error arguments and argument variables starting with _ to be unused.
  • Require let or const instead of var.

Vue.js

Using Vue.js's flat/recommended/flat/vue2-recommended as a base, with the following overriding rules:

  • Allow v-html.
  • No closing bracket newline.
  • One space for closing brackets on self-closing tags.
  • Only warn (instead of error) for single word component names.

Maintainers

Contribute

PRs are welcome, but we only use this for our own project and it shouldn't be of much interest for anyone else.

If editing the README, please conform to the standard-readme specification.

License

MIT @2024 Verbundzentrale des GBV (VZG)