Coder Social home page Coder Social logo

electroma / tslint-forbidden-imports Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 161 KB

TSLint rule to control dependencies between project modules

License: The Unlicense

JavaScript 7.91% TypeScript 92.09%
tslint fitness-function dependency-analysis typescript

tslint-forbidden-imports's Introduction

npm version Build Status Coverage Status

tslint-forbidden-imports

This rule is useful in larger code bases to control dependencies between project modules. Rule allows to specify source file patterns and list of path patterns which should not be allowed.

Example use case

For example project code has the directory layout like:

src/
    client/
    server/
    common/

Directories client, server and common represent code scopes:

  • client - application UI
  • server - server-side logic
  • common - shared constants and interfaces

In this setup imports between following directories should not be allowed:

  • client -> server (something like import server from '../../server/myAPIServer')
  • server -> client (something like import Crux from '../../client/components/Crux')
  • common -> client
  • common -> server

It may be implemented by adding the following rule configuration:

{
  "forbidden-imports": [true, {
          "client/**": ["server/**"],
          "server/**": ["client/**"],
          "common/**": ["client/**", "server/**"]
  }]
}

Installation

yarn install -d tslint-forbidden-imports

Configuration

  1. Add node_modules/tslint-forbidden-imports/rules to rulesDirectory parameter of your tslint.json.
  2. Enable rule forbidden-imports and pass mapping "file pattern" -> ["fordidden import patterns"...]

The pattern syntax may use any features supported by micromatch.

Supported import pattern types:

  • Node package (i.e. lodash or ui-*)
  • GLOB file path relative to project root (i.e. src/client/** or plugins/*/src/ui/**)

Advanced configuration: placeholders

Library has support for placeholder replacement. This rule will not allow imports between child directories of plugins directory

{
    'plugins/*/src/**': ['plugins/!(%0%)/**']
}

Development

To run tests, just run yarn test.

Project tests are based on test helper from tslint-microsoft-contrib.

References

tslint-forbidden-imports's People

Contributors

electroma avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

tslint-forbidden-imports's Issues

Does not support path aliases

Awesome plugin, exactly what we were looking for, but the plugin does not seem to resolve import paths which use aliases defined in tsconfig.

In our project we import like:

 import SomeStore from 'stores/SomeStore'

With tsconfig containing the following:

{
  "compilerOptions": {
    "paths": {
      "stores/*": ["src/js/stores/*"]
    }
  }
}

Angular 7 support

Upgrading from Angular 6 to 7 produces this error:

ng update @angular/core
Package "tslint-forbidden-imports" has an incompatible peer dependency to "typescript" (requires "^2.8.3", would install "3.1.6").
Incompatible peer dependencies found. See above.

rulesDirectory setup doesn't work

Using node_modules/tslint-forbidden-imports in tslint.json rulesDirectory didn't work for me.

Output from running ng lint was:

Could not find implementations for the following rules specified in the configuration:
    forbidden-imports
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.

I had to change it to node_modules/tslint-forbidden-imports/dist for it to work.

tslint version: 5.9.1

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.