Git Product home page Git Product logo

eslint-plugin-absolute-imports's Introduction

eslint-plugin-import-typescript

An ESLint plugin that auto-fixes relative imports to absolute imports. Support baseUrl and paths config in TSConfig.

A typical project starts out using relative imports, but once it has gotten bigger, the relative imports grows longer and messier. This plugin is useful when you want to migrate the relative imports to absolute imports, and keep it that way through auto-fixes.

Built on Jeff Chen's Absolute Imports plugin.

Prerequisites

You must define baseUrl and paths (optional) in either tsconfig.json or jsconfig.json.

Setup

  • npm i --save-dev eslint-plugin-import-typescript
  • Add eslint-plugin-import-typescript to your eslint plugins section
  • Add one of the supported rules below to your eslint rules section.

Example:

  plugins: ['eslint-plugin-import-typescript'],
  rules: [
    'import-typescript/no-relative-parent-imports': ['error'],
  ]

Rules

Rule Description
no-relative-imports Autofix relative imports to absolute import or paths import
no-relative-parent-imports Autofix relative parent imports to absolute import or paths import

The rule no-relative-parent-imports is useful when you still want to allow relative import within the same folder but disallow relative import in a parent folder. It's similar to eslint-plugin-import/no-relative-parent-imports rule, except it supports auto-fix. It is the recommended rule.

The rules supports baseUrl and paths, and will prioritize paths import if it exists. It's possible to disable one of the rewritting rules by setting the corresponding options

Options Description
onlyAbsoluteImport Ignore paths config if it exists
onlyPathsImport Do not autofix absolute import. Only fix import if there is a path matching paths config

Example:

    'import-typescript/no-relative-parent-imports': [
        'error', { onlyAbsoluteImport: true } 
    ],

Related readings

While baseUrl and paths are understood and supported by Typescript, it doesn't rewrite the import paths when it compiles the typescript code to javascript. Belows are some related readings and library to get typescript absolute imports works:

Contributions

Contributions are welcome!

License

MIT

eslint-plugin-absolute-imports's People

Contributors

hdoan741 avatar jchen1 avatar justinbarclay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.