Skip to content

Commit

Permalink
0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
leonsilicon committed Apr 13, 2024
1 parent 50fad5f commit 77b2379
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tunnel/eslint-plugin-no-relative-import-paths",
"version": "0.0.3",
"version": "0.0.4",
"main": "index.js",
"scripts": {
"format": "dprint fmt"
Expand Down
108 changes: 108 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# @tunnel/eslint-plugin-no-relative-import-paths

Moving a file to different folder, could result in changing all imports statement in that file. This will not happen is the import paths are absolute. The eslint rule helps enforcing having absolute import paths.
Support eslint --fix to automatically change imports to absolute paths.

# Installation

Install [ESLint](https://www.github.com/eslint/eslint) either locally or globally. (Note that locally, per project, is strongly preferred)

```sh
npm install eslint --save-dev
```

If you installed `ESLint` globally, you have to install this plugin globally too. Otherwise, install it locally.

```sh
npm install @tunnel/eslint-plugin-no-relative-import-paths --save-dev
```

# Configuration

Add the plugin to the plugins section, and configure the rule options.

```json
{
"plugins": ["no-relative-import-paths"],
"rules": {
"@tunnel/no-relative-import-paths/no-relative-import-paths": [
"warn",
{ "allowSameFolder": true }
]
}
}
```

## Rule options

```json
...
"@tunnel/no-relative-import-paths/no-relative-import-paths": [
"warn",
{ "allowSameFolder": true, "rootDir": "src", "prefix": "" }
]
...
```

- `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
- `allowSameFolder`: optional boolean set to `true` to allow relative import paths for imported files from the same folder (default to `false`).

### `allowSameFolder`

When `true` the rule will ignore relative import paths for imported files from the same folder

Examples of code for this rule:

```js
// when true this will be ignored
// when false this will generate a warning
import Something from './something';

// will always generate a warning
import Something from '../modules/something';
```

### `rootDir`

Useful when auto-fixing and the rootDir should not be included in the absolute path.

Examples of code for this rule:

```js
// when not configured:
import Something from '../../components/something';

// will result in
import Something from 'src/components/something';
```

```js
// when configured as { "rootDir": "src" }
import Something from '../../components/something';

// will result in
import Something from 'components/something';
// ^- no 'src/' prefix is added
```

### `prefix`

Useful when auto-fixing and a prefix should be included in the absolute path.

Examples of code for this rule:

```js
// when not configured:
import Something from '../../components/something';

// will result in
import Something from 'src/components/something';
```

```js
// when configured as { "prefix": "@" }
import Something from '../../components/something';

// will result in
import Something from '@/components/something';
```

0 comments on commit 77b2379

Please sign in to comment.