Coder Social home page Coder Social logo

bradennapier / eslint-plugin-ts-import Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 3.02 MB

Provide import-patterns that are expected to be followed within the rep

License: MIT License

JavaScript 11.40% TypeScript 88.60%
eslintplugin eslint typescript

eslint-plugin-ts-import's Introduction




eslint-plugin-ts-import





npm package
Define specific import rules for files within your Typescript project. Adapted and enhanced from VSCode's code-import-patterns




yarn add --dev eslint-plugin-ts-import





Features

  • Provide import patterns that restrict imports of certain files based on location.
  • Ensure imports meet the expected guidelines within your repo.
  • Adapted from VSCode's rule code-import-patterns.
  • Works with configured paths from your tsconfig.json
  • Provide custom eslint messaging for each pattern if needed.
  • Useful in monorepos and most Typescript projects which utilize incremental builds.

Simple Example

// .eslintrc.js

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  rules: {
    'ts-import/patterns': [
      'error',
      {
        // these rules apply to any files which are within the src/services directory
        target: '**/src/services/*/**',

        // allow any imports to node_modules.  We confirm that the node_module exists in case the use of paths / absolute imports is used so that
        // we can tell the difference
        modules: true,

        // allow imports of node core modules?  defaults to true.  if false, imports like fs, path, stream will cause an error
        node: true,

        allowed: [
          // anything in src/services/{service}/** may import config `import config from 'config'`
          'config',

          // anything in src/services/{service}/** may import from core `import someModule from 'core/someModule'`
          'core/**',

          // run target.replace(arr[0], arr[1]) to build pattern
          // any service may import from itself - so src/services/rest-api/** may always import from `src/services/rest-api/**`
          // whether using relative or absolute imports.  However it will not be able to import from `../api-client/**` or `services/api-client/**`
          [/.*\/src\/services\/([^/]*)\/.*/, '**/src/services/$1/**'],

          // allow any imports whether relative or absolute as long as they are not higher than /src/services
          [/(.*\/src\/services).*/, '$1/**'],

          // this rule without the above rule would only allow the files to import themselves or higher and would restrict `../`
          './**',
        ],
        message:
          'Optional custom message to display when violated',
      },
    ],
  },
  plugins: ['ts-import']
}

Special Thanks

eslint-plugin-ts-import's People

Contributors

bradennapier avatar dependabot[bot] avatar semantic-release-bot avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.