GithubHelp home page GithubHelp logo

tslint-rules-bunch's Introduction

tslint-rules-bunch

is a module that contains a set of the tslint rules.

Build Status: Linux / MacOS

Rules

no-import-zones

Forbids specific imports for the specified targets using micromatch patterns matching.

Imagine you have the following directory structure:

└── project
    ├── dist
    ├── src
    │   ├── lib
    │   └── test
    └── tslint.json

You keep the built/compiled/transpiled/release code in the dist directory, source code in src/lib and tests code in src/test. For example you want to make sure that code under src/test directory doesn't import anything from the src/lib as you want tests code does import only from the dist (release code). But as an exclusion you want to allow tests code import stuff from the src/lib/module-1. You can enforce such scenario putting the no-import-zones rule into your tslint.json:

{
  "rulesDirectory": [
    "node_modules/tslint-rules-bunch/rules"
  ],
  "rules": {
    "no-import-zones": [
      true,
      {
        "zones": [
          {
            "patterns": [
              {
                "target": "src/test/**/*",
                "from": [
                  "src/lib",
                  "src/lib/**/*",
                  "!src/lib/module-1"
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

With the basePath value set to src, no-import-zones block would look like this (pattern values don't start from the src anymore):

    "no-import-zones": [
      true,
      {
        "basePath": "src",
        "verbose": true,
        "zones": [
          {
            "patterns": [
              {
                "target": "test/**/*",
                "from": [
                  "lib",
                  "lib/**/*",
                  "!lib/module-1"
                ]
              }
            ]
          }
        ]
      }
    ]

Above code related notes:

  • basePath: defines the base path value that is used for resolving <pattern object>.target and <pattern object>.from values relative to the process.cwd(). It can be set on the top level and also on the specific zone level. The property is optional, by default process.cwd() is used as the base path value.
  • verbose: flag value that toggles detailed failure output, default value is undefined, means false;
  • <pattern object>.target: can be a single value or array of the path pattern to which the forbidding logic should be applied.
  • <pattern object>.from: can be a single value or array of the import patterns that are not allowed to be used by the files matched against the <pattern object>.target pattern.

tslint-rules-bunch's People

Contributors

vladimiry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tslint-rules-bunch's Issues

Path resolution is wrong when tsconfig baseUrl is used

My project's tsconfig has a baseUrl set to src and the folder structure looks like this:

src/
  submodule1/
  submodule2/
  ...

Some example import patterns:

// in submodule2/subfolder/*.ts
import "submodule1/someFile";
import "../../submodule1/someOtherFile";
import "../someOtherFile";
import "submodule2/someOtherFile";
import "./sameFolderFile.ts";

I looked at the rule's source code and noticed the following: when the linting rule iterates the import statements and sees something that doesn't start with . (import paths like submodule1/*), then it insists on doing something like path.relative("src", "submodule1/someFile"), which just ends up resolving to ../submodule1 instead of submodule1. This is regardless of setting the basePath option or not.

I see this behavior is also reflected in the test fixtures, with imports like import("src/...."). This however doesn't make much sense to me. TypeScript would not even resolve such imports unless
a) the baseUrl is set to the parent of src, not src itself
b) There is an entry in tsconfig paths mapping src to some other folder
c) src is an actual node module
d) the moduleResolution option is set to classic instead of node (which has been deprecated for a long time)

Either way, depending on the type of import path (relative or not) we end up with two different behaviors, which is IMHO a bug.

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.