Skip to content

Commit

Permalink
feat: no-restricted-imports option added allowImportNames (#16196)
Browse files Browse the repository at this point in the history
* feat: [no-restricted-imports] added option `excludeImportNames`

* Fix: Removed question marks from checks in `no-restricted-imports`

* Fix: [no-restricted-imports] `excludeImportNames`

* docs: Update no-restricted-imports.md with new option

Documentation added for option `excludeImportNames`.

* docs: Update no-restricted-imports option exludeImportNames to allowImportNames

* fix: Renamed `no-restricted-imports` option `excludeImportNames` to `allowImportNames`

* fix: `no-restricted-imports` comments for options `allowImportNames` to pass test

* fix: `no-restricted-imports` option `allowImportNames` for importing `*`

* fix: `no-restricted-imports` rules added for option `allowImportNames`

* fix: `no-restricted-imports` tests added for option `allowImportNames`

* Lint

* Lint Fix

* Check Rules

* fix: `no-restricted-imports`

* fix: `no-restricted-imports`: Add tests for allowImportNamePattern

* docs: `no-restricted-imports` updated

* feat: refactor no-restricted-imports and add tests

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* fix: `no-restricted-imports`: requested changes

* docs: `no-restricted-imports`: added `allowImportNames` to `patterns`

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* docs: `no-restricted-imports` message variation

* feat: validate schema

* docs: `no-restricted-imports`: disallow using both `importNames` and `allowImportNames`

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* fix: `no-restricted-imports`: Review suggestions

* feat: add more validations to schema

* docs: add validate options name

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* fix: `no-restricted-imports`: tests updated accordingly

* feat: add return

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/no-restricted-imports.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* feat: `no-restricted-imports`: added custom message to tests

* fix: `no-restricted-imports`: remove test case

* fix: `no-restricted-imports`

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

* Update docs/src/rules/no-restricted-imports.md

Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>

---------

Co-authored-by: Tanuj Kanti <tanujkanti4441@gmail.com>
Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
4 people committed Feb 27, 2024
1 parent 491a1d1 commit 11144a2
Show file tree
Hide file tree
Showing 3 changed files with 581 additions and 47 deletions.
145 changes: 145 additions & 0 deletions docs/src/rules/no-restricted-imports.md
Expand Up @@ -230,6 +230,58 @@ import { AllowedObject as DisallowedObject } from "foo";

:::

#### allowImportNames

This option is an array. Inverse of `importNames`, `allowImportNames` allows the imports that are specified inside this array. So it restricts all imports from a module, except specified allowed ones.

Note: `allowImportNames` cannot be used in combination with `importNames`.

```json
"no-restricted-imports": ["error", {
"paths": [{
"name": "import-foo",
"allowImportNames": ["Bar"],
"message": "Please use only Bar from import-foo."
}]
}]
```

Examples of **incorrect** code for `allowImportNames` in `paths`:

Disallowing all import names except 'AllowedObject'.

::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{
name: "foo",
allowImportNames: ["AllowedObject"],
message: "Please use only 'AllowedObject' from 'foo'."
}]}]*/

import { DisallowedObject } from "foo";
```

:::

Examples of **correct** code for `allowImportNames` in `paths`:

Disallowing all import names except 'AllowedObject'.

::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{
name: "foo",
allowImportNames: ["AllowedObject"],
message: "Only use 'AllowedObject' from 'foo'."
}]}]*/

import { AllowedObject } from "foo";
```

:::

### patterns

This is also an object option whose value is an array. This option allows you to specify multiple modules to restrict using `gitignore`-style patterns.
Expand Down Expand Up @@ -445,6 +497,54 @@ import { hasValues } from 'utils/collection-utils';

:::

#### allowImportNames

You can also specify `allowImportNames` on objects inside of `patterns`. In this case, the specified names are applied only to the specified `group`.

Note: `allowImportNames` cannot be used in combination with `importNames`, `importNamePattern` or `allowImportNamePattern`.

```json
"no-restricted-imports": ["error", {
"patterns": [{
"group": ["utils/*"],
"allowImportNames": ["isEmpty"],
"message": "Please use only 'isEmpty' from utils."
}]
}]
```

Examples of **incorrect** code for `allowImportNames` in `patterns`:

::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
group: ["utils/*"],
allowImportNames: ['isEmpty'],
message: "Please use only 'isEmpty' from utils."
}]}]*/

import { hasValues } from 'utils/collection-utils';
```

:::

Examples of **correct** code for `allowImportNames` in `patterns`:

::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
group: ["utils/*"],
allowImportNames: ['isEmpty'],
message: "Please use only 'isEmpty' from utils."
}]}]*/

import { isEmpty } from 'utils/collection-utils';
```

:::

#### importNamePattern

This option allows you to use regex patterns to restrict import names:
Expand Down Expand Up @@ -518,6 +618,51 @@ import isEmpty, { hasValue } from 'utils/collection-utils';

:::

#### allowImportNamePattern

This is a string option. Inverse of `importNamePattern`, this option allows imports that matches the specified regex pattern. So it restricts all imports from a module, except specified allowed patterns.

Note: `allowImportNamePattern` cannot be used in combination with `importNames`, `importNamePattern` or `allowImportNames`.

```json
"no-restricted-imports": ["error", {
"patterns": [{
"group": ["import-foo/*"],
"allowImportNamePattern": "^foo",
}]
}]
```

Examples of **incorrect** code for `allowImportNamePattern` option:

::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
group: ["utils/*"],
allowImportNamePattern: '^has'
}]}]*/

import { isEmpty } from 'utils/collection-utils';
```

:::

Examples of **correct** code for `allowImportNamePattern` option:

::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
group: ["utils/*"],
allowImportNamePattern: '^is'
}]}]*/

import { isEmpty } from 'utils/collection-utils';
```

:::

## When Not To Use It

Don't use this rule or don't include a module in the list for this rule if you want to be able to import a module in your project without an ESLint error or warning.

0 comments on commit 11144a2

Please sign in to comment.