eslint-plugin-import-srclimits

1.0.1 • Public • Published

eslint-plugin-import-srclimits

stable_version >= 1.0.0

Limit import source from some special folder in configuration

version  Build Status  GitHub issues  license

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-import-srclimits:

$ npm install eslint-plugin-import-srclimits --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-import-srclimits globally.

Usage

Add import-srclimit to the plugins section of your .eslintrc.js configuration file. You can omit the eslint-plugin- prefix:

Set parserOptions.sourceType value as module in your configuration.

module.exports = {
    // ...
    "plugins": [
        "import-srclimits"
    ],
    "parserOptions": { 
        "ecmaVersion": 2015, 
        "sourceType": "module" 
    }
    // ...
};

Then configure the rules you want to use under the rules section.

The effect is: The files match in files pattern, can only import target match in source pattern

module.exports = {
  // ...
  "rules": {
    "import-srclimits/srclimits": [
      "error", 
      {
        "files": ["./src/**/*.{js,vue,jsx}"],
        "source": ["./src/**/*.*", "node_modules/**", "!./mock/**/*.*"], 
        "errMsg": "The code of the files in the src folder, can't import source file in ./mock/**"
      }
    ]
  }
  // ...
};

option files & source can accepts glob-pattern, regular-expression, function(str){/* return fasle|true; */}

module.exports = {
  // ...
  "rules": {
    "import-srclimits/srclimits": [
      "warn", 
      {
        "files": ["./src/**/*.{js,vue,jsx}"],
        "source": [
          (str) => (str.indexOf('util') == -1),
          /os/g
        ], 
        "errMsg": "Browser code could not use node module 'util' & 'os'!!!"
      }
    ]
  }
  // ...
};

see more: anymatch

Test

srclimits
    valid
      ✓ import qs from 'qs'; (48ms)
      ✓ import abc from 'qs/abc';
      ✓ import kapp from '@scopename/kapp';
      ✓ import abc from '@scopename/bbb/abc';
      ✓ import clone from '../utils/clone.js';
      ✓ import '../utils/clone.js';
      ✓ import '../utils/clone';
      ✓ import { clone } from '../utils/index.js';
      ✓ import { clone } from '../utils/';
      ✓ import { clone } from '../utils';
      ✓ import { clone as copy } from '../utils';
      ✓ import * as _ from '../utils/index.js';
    invalid
      ✓ import '../../mock/storage.js';
      ✓ import '../../mock/storage';
      ✓ import storage from '../../mock/storage.js';
      ✓ import storage from '../../mock/storage';
      ✓ import '../../mock/utils';
      ✓ import '../../mock/utils/';
      ✓ import * as _ from '../../mock/utils/index.js';
      ✓ import { addStyleTag } from '../../mock/utils/';
      ✓ import { addStyleTag as injectStyleDom } from '../../mock/utils/index';
      ✓ // multiple lines
        import { clone } from '../utils/';    // ✓
        import { base } from '../base/index'; // ✓
        import { addStyleTag as injectStyleDom } from '../../mock/utils/index'; // ✘
22 passing

Others

Created by yeoman tool.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    2
    • latest

Version History

Package Sidebar

Install

npm i eslint-plugin-import-srclimits

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

19.1 kB

Total Files

11

Last publish

Collaborators

  • froguard