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-relative-imports-when-same-folder

v0.0.4

Published

An eslint plugin to convert any absolute import paths to relative ones if files are imported from within the same folder

Downloads

1,632

Readme

eslint-plugin-relative-imports-when-same-folder

npm version example workflow

An eslint plugin that converts any absolute import paths to relative ones if a file is imported from within the same directory.

Resolves absolute paths to paths on disk by parsing a tsconfig.json expected to be found in the root of the repository using this plugin.

// ## Import sibling

// From within a file 
// src/Foo/Foo.js
// we want to import
// src/Foo/Baz.js

// OK
import Bar from "./Baz"; // valid relative path

// NOT OK
import Something from "src/Foo/Baz";
// ## Import descendant

// From within a file 
// src/Foo/Foo.js
// we want to import
// src/Foo/Bar/Bar.js

// OK
import Bar from "./Bar/Bar"; // valid relative path

// NOT OK
import Something from "src/Foo/Bar/Bar";

Prerequisites / Limitations

Module resolution must be defined in tsconfig.json

A modern JS/TS-based project can not only define some sort of baseUrl like src, it can also define import aliases using webpack`s resolve option or path mappings specified tsconfig.

As an example, ~ can be an alias for all code under <repository-root>/src.

⚠️ This plugin only resolves module resolution configs baseUrl and paths defined in a single tsconfig.json in the repository root. ⚠️ It only supports the common Node Module resolution strategy. There is no support for

  • rootDirs
  • multiple tsconfigs that extend each other.

Contributions to make this more flexible or to retrieve module resolution mappings out of a webpack config are welcome.

As a workaround to this limitation, a project can re-use or re-declare import aliases that are defined in a webpack config in the project`s tsconfig.

Default set of file extensions used when resolving paths

When an alias is mapped to a single path, we assume that another mechanism (e.g. TypeScript or no-unresolved) already ensures that the de-aliased path is valid.

When an alias is mapped to more than one path, we check what de-aliased path can be resolved. Here we assume a default set of file extensions.

[
    '.js',
    '.ts',
    '.jsx',
    '.tsx',
    '.scss',
    '.css',
    '.svg',
    '.json',
]

Based on demand, this can be made configurable.

How to contribute

See the contributing guide for broad instructions on how to get started with this project.

Setup Instructions

To install, run

yarn add -D eslint-plugin-relative-imports-when-same-folder

respectively

npm install --save-dev eslint-plugin-relative-imports-when-same-folder

Then update your eslint config by adding relative-imports-when-same-folder to the list of plugins, and turn on the main rule no-relative-imports-when-same-folder of this plugin.

// .eslintrc.js
  plugins: [
    // other plugins ..
    "relative-imports-when-same-folder",
  ],
  rules: {
    // other rules ..
    "relative-imports-when-same-folder/no-relative-imports-when-same-folder": "error",
  }

Example Repo

Check out https://github.com/s-pic/fixmy.frontend/tree/use_eslint-plugin-relative-imports-when-same-folder. It is a not too small, real-world project.

How this was born

In a codebase, we wanted to have three kinds of import blocks:

  1. Third party modules (e.g. lodash)
  2. Imports from outside the folder the current file is in (e.g. src/utils/some_util)
  3. Imports from within the same file, as relative imports (./some_sub_component)

We utilized import/order for the ordering as well as eslint-plugin-no-relative-import-paths with allowSameFolder) to only allow imports from within the same file. Using that setup we could enforce allmost all of the mentioned constraints we wanted to apply:

  • imports where ordered in those mentioned blocks
  • relative imports where only allowed from within the same folder

What was missing was automation to refactor all absolute imports that can be relative to actually be relative, so they are moved to the last block.

TODOs

  • [ ] check eslint settings to tell clients that this is a plugin for ts-parser
  • [ ] Support windows -> e.g. read and use platform specific segment separator
  • [ ] Performance testing, see https://www.darraghoriordan.com/2021/11/06/how-to-write-an-eslint-plugin-typescript/
  • [ ] migrate to typescript !
  • [ ] Add proper unit test using RuleTester from typescript-eslint
  • [ ] Check for a lib that helps with dealing with globs instead of verbosely hand-rolling the string manipulation logic
  • [ ] Solidify reverse mapping of path aliases with more tests, preferably using real world configs
  • [ ] Try to find a lib to reverse-map tsconfig module resolution configs. This must have been solved somewhere else already. Eventually we can feed aliases to enhanced-resolve

Acknowledgements

Thanks SMG for letting me work on company time.