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-headers

v1.2.1

Published

A flexible and `--fix`able rule for checking, inserting, and formatting file headers.

Downloads

12,423

Readme

eslint-plugin-headers

A flexible and --fixable rule for checking, inserting, and formatting file headers.

Supports variable content injection, configurable usage of block or line comments, custom comment block prefixes and suffixes, custom line prefixes, and spacing between the header and code.

Useful for inserting, enforcing, and updating copyright or licensing notices while preserving pragma expressions in leading content blocks.

This plugin is best used with a formatter like prettier.

Motivation

This plugin aims to be a successor to the popular but defunct plugin eslint-plugin-header. eslint-plugin-headers strives to implement the same features as eslint-plugin-header where possible, while sensibly extending features, supporting modern JavaScript frameworks, and maintaining compatibility with other common tools.

Installation

You'll first need to install ESLint:

npm i eslint --save-dev

Next, install eslint-plugin-headers:

npm install eslint-plugin-headers --save-dev

Usage

Flat config

Import the default export from the estlin-plugin-headers module. Add the headers key to the plugins section of your config object in the configuration file. You can omit the eslint-plugin- prefix:

import headers from "eslint-plugin-headers";

export default [{
  plugins: {
    headers
  }
}]

Then configure the rules you want to use under the rules section.

import headers from "eslint-plugin-headers";

export default [{
  plugins: {
    headers
  }
  rules: {
    "headers/header-format": [
      "error",
      {
        source: "string",
        content: "Copyright 2024. All rights reserved."
      }
    ]
  }
}]

Legacy .eslintrc config

Add headers to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["headers"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "headers/header-format": [
      "error",
      {
        "source": "string",
        "content": "Copyright 2024. All rights reserved."
      }
    ]
  }
}

Usage with Vue

This project supports parsing Vue files via the AST generated by the vue-eslint-parser package. To properly apply this plugin's rules to Vue files, you must:

  1. Install the vue-eslint-parser package as a dev dependency.
  2. Specify the vue-eslint-parser in the configuration's languageOptions.parser field (or the "parser" field for legacy configurations), and
  3. Set the enableVueSupport flag for the appropriate rules.

Example configuration:

import headers from "eslint-plugin-headers";
import vueEslintParser from "vue-eslint-parser";

export default [
  {
    plugins: {
      headers 
    },
    files: ["**/*.vue"],
    rules: {
      "headers/header-format": [
        "error",
        {
          source: "string",
          content: "This is a header.",
          enableVueSupport: true,
        }
      ]
    },
    languageOptions: {
      parser: vueEslintParser,
    },
  }
];

With this configuration the following file would be transformed like so:

Before:

<template>
  <p>This is an example Vue file.</p>
</template>

After applying the fix:

<!--
  This is a header.
-->
<template>
  <p>This is an example Vue file.</p>
</template>

Since the vue-eslint-parser package strives to maintain compatibility with rules targeting JavaScript, it exposes Vue AST tokens to rules in different ways than the default ESlint parser. The means of exposing HTML Comment nodes are of specific interest to this plugin, and the mechanism to access these are substantially different than how a plugin would typically read a comment node, making it necessary to both specify this particular parser as well as setting the flag.

Examples

Example 0:

Example Configuration:

{
  "rules": {
    "headers/header-format": [
      "error",
      {
        "source": "string",
        "content": "Copyright 2024. All rights reserved."
      }
    ]
  }
}

Using the above configuration, here's a file without a matching header:

module.exports = 42;

When the fix is applied, the file now appears so:

/**
 * Copyright 2024. All rights reserved.
 */
module.exports = 42;

Example 1:

Using the same configuration, this file already has a header, this one containing pragmas:

/**
 * @fileoverview This file contains a magic number.
 * @author Rob Misasi
 */
module.exports = 42;

When the fix is applied, the file now appears so:

/**
 * Copyright 2024. All rights reserved.
 *
 * @fileoverview This file contains a magic number.
 * @author Rob Misasi
 */
module.exports = 42;

Example 2: Using the following configuration, variable values can be injected into the provided header:

{
  "rules": {
    "headers/header-format": [
      "error",
      {
        "source": "string",
        "content": "Copyright {year} {company}. All rights reserved.",
        "variables": {
          "year": "2077",
          "company": "Software Incorporated"
        }
      }
    ]
  }
}

We can then apply a fix to the following file:

/**
 * Copyright 1947 Hardware LLC. All rights reserved.
 */
module.exports = 42;

And get the resulting header:

/**
 * Copyright 2077 Software Incorporated. All rights reserved.
 */
module.exports = 42;

Options

Options are supplied through a single object with the following properties:

| Name | Type | Required | Default | Description | | ---------------- | -------------------- | ----------------------- | ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | source | "file" \| "string" | Yes | | Indicates how the header content is supplied. | | style | "line" \| "jsdoc" | No | "jsdoc" | Indicates the comment style to enforce. A leading line-style comment block will only include adjacent line comments, although a line comment's content may be empty. No effect if enableVueSupport: true. | | content | string | When source: "string" | | The string to enforce in the header comment. | | path | string | When source: "file" | | The path to a file containing the header content to enforce. | | preservePragmas | boolean | No | true | Preserves existing pragma expressions in leading comments when updating header. No effect when style: "line". | | blockPrefix | string | No | See below | Content at the start of the leading comment block. | | blockSuffix | string | No | See below | Content at the end of the leading comment block. | | linePrefix | string | No | See below | Content prepended to the start of each line of content. | | trailingNewlines | number | No | | Number of empty lines to enforce after the leading comment. | | variables | object | No | | The keys to find and values to fill when formatting the provided header. Values must be strings. | | enableVueSupport | boolean | No | false | EXPERIMENTAL! Enable support for parsing .vue files. Must be used with vue-eslint-parser. See above for details. |

Default Prefixes and Suffixes

Example configuration:

export default [
  {
    // ...
    rules: {
      "headers/header-format": [
        "error",
        {
          source: "string",
          content: "This is a header.",
          // ...{Additional Configuration}
        }
      ]
    },
  }
]

The subsequent section titles contain the additional configuration inserted above, and the resulting comment that will be produced.

style: "line"

Expected/produced header:

// This is a header.
  • Default block prefix: None
  • Default block suffix: None
  • Default line prefix: " "
style: "jsdoc"

Expected/produced header:

/**
 * This is a header.
 */
  • Default block prefix: "*\n"
  • Default block suffix: "\n "
  • Default line prefix: " * "
enableVueSupport: true

Expected/produced header:

<!--
  This is a header.
-->
  • Default block prefix: "\n"
  • Default block suffix: "\n"
  • Default line prefix: " "

Future

  • Add support for common pragma expressions that don't utilize the @ symbol (e.g. eslint-disable)

Rules

🔧 Automatically fixable by the --fix CLI option.

| Name          | Description | 🔧 | | :------------------------------------------- | :----------------------------------------------------------------- | :- | | header-format | Verifies the content and format of a file's leading comment block. | 🔧 |