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-type-only-import

v0.9.0

Published

ESLint plugin to enforce `type` modifier to imports/exports targeting files with certain naming pattern.

Downloads

579

Readme

Import/Export Type-only Fixer - ESLint Plugin

Overview

The ESLint plugin eslint-plugin-type-only-import allows automation of managing type modifier for imports/exports matching a given pattern. If such import/export is detected, and it doesn't have type modifier (either fully or any of its components), this plugin will attempt to fix the situation by adding type modifier to the import/export.

The most typical usecase for this plugin is when one is creating a library with TypeScript, and it has some files which should contain only types. One option is to use .d.ts extension, but that has special meaning in TypeScript, causing e.g. compiler to leave that file out from the final emitting result. And if just using .ts extension, then e.g. code coverage tools will report those files as not covered.

The approach support by this plugin and which also fits nice with code coverage tools, is that these types-only .ts files will have some certain naming pattern, e.g. xyz.types.ts. The code coverage tools can be configured to exclude such files from their analysis, and this ESLint plugin can be configured to enforce type modifier for any import or export statements that target such files.

Assuming the plugin would be configured to add type modifier to all imports/exports which have target string literal matching pattern *.types , here are few examples about valid and invalid code | code | is valid | auto-fixed to | | :-------------------------------------------: | :---------------------: | :--------------------------------------------------------------------------------------: | | import * as code from "./code.types" | :x: | import type * as code from "./code.types" | | import * as code from "./code" | :ballot_box_with_check: | Target string literal does not match given pattern and thus the statement is unaffected. | | import type * as code from "./code.types" | :white_check_mark: | | | import code, { type X } from "./code.types" | :x: | import type { default as code, X } from "./code.types" | | import code, { X } from "./code.types" | :x: | import type { default as code, X } from "./code.types" | | import type { X } from "./code.types" | :white_check_mark: | | | import { type X } from "./code.types" | :white_check_mark: | | | import code from "./code.types" | :x: | import type { default as code } from "./code.types" | | export * from "./code.types" | :x: | export type * from "./code.types" | | export { X } from "./code.types" | :x: | export type { X } from "./code.types" |

Installation

Assuming that ESLint has already been installed

yarn add --dev eslint@latest

, install this plugin:

yarn add --dev eslint-plugin-type-only-import@latest

Configuration

In the plugins section of the ESLint configuration, specify the newly installed plugin:

{
  "plugins": [
    "type-only-import"
  ]
}

Then, extend the recommended set (which configures both rules of this plugin to be treated as error):

{
  "extends": [
    "plugin:type-only-import/recommended"
  ]
}

Alternatively, it is possible to configure each rule individually:

{
  "rules": {
    "type-only-import/require-type-only-import": "error", // Recommended
    "type-only-import/require-type-only-import": "error" // Recommented
  }
}

Options

Both rules exposed by this plugin take optional individual options. These options adher to single schema, which is a JSON object with the following properties:

  • pattern of type string : the pattern, as RegExp source, to match the import/export target string literals against. Is treated verbatim, so to match relative-only imports/exports, use \. as first character. If using extensions in imports/exports, these must be included here too. Default: \..+\.types$.
  • disableFixer of type boolean : If true, then none of the detected errors by this rule will be auto-fixed. Default: false.

For more information, see JSON schema specification and TS type in source code.

Rules

All of the rules currently are fixable, and can be fixed automatically by running ESLint CLI with --fix flag.

| recommended | fixable | rule | description | | :----------------: | :------: | :--------------------------------------------------------------: | :-----------------------------------------------------------------: | | :white_check_mark: | :wrench: | require-type-only-export | Checks and fixes the target string literals in export statements. | | :white_check_mark: | :wrench: | require-type-only-import | Checks and fixes the target string literals in import statements. |