|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.5 KB
|
| Line | |
|---|
| 1 | # import/no-absolute-path
|
|---|
| 2 |
|
|---|
| 3 | 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Node.js allows the import of modules using an absolute path such as `/home/xyz/file.js`. That is a bad practice as it ties the code using it to your computer, and therefore makes it unusable in packages distributed on `npm` for instance.
|
|---|
| 8 |
|
|---|
| 9 | This rule forbids the import of modules using absolute paths.
|
|---|
| 10 |
|
|---|
| 11 | ## Rule Details
|
|---|
| 12 |
|
|---|
| 13 | ### Fail
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | import f from '/foo';
|
|---|
| 17 | import f from '/some/path';
|
|---|
| 18 |
|
|---|
| 19 | var f = require('/foo');
|
|---|
| 20 | var f = require('/some/path');
|
|---|
| 21 | ```
|
|---|
| 22 |
|
|---|
| 23 | ### Pass
|
|---|
| 24 |
|
|---|
| 25 | ```js
|
|---|
| 26 | import _ from 'lodash';
|
|---|
| 27 | import foo from 'foo';
|
|---|
| 28 | import foo from './foo';
|
|---|
| 29 |
|
|---|
| 30 | var _ = require('lodash');
|
|---|
| 31 | var foo = require('foo');
|
|---|
| 32 | var foo = require('./foo');
|
|---|
| 33 | ```
|
|---|
| 34 |
|
|---|
| 35 | ### Options
|
|---|
| 36 |
|
|---|
| 37 | By default, only ES6 imports and CommonJS `require` calls will have this rule enforced.
|
|---|
| 38 |
|
|---|
| 39 | You may provide an options object providing true/false for any of
|
|---|
| 40 |
|
|---|
| 41 | - `esmodule`: defaults to `true`
|
|---|
| 42 | - `commonjs`: defaults to `true`
|
|---|
| 43 | - `amd`: defaults to `false`
|
|---|
| 44 |
|
|---|
| 45 | If `{ amd: true }` is provided, dependency paths for AMD-style `define` and `require`
|
|---|
| 46 | calls will be resolved:
|
|---|
| 47 |
|
|---|
| 48 | ```js
|
|---|
| 49 | /*eslint import/no-absolute-path: [2, { commonjs: false, amd: true }]*/
|
|---|
| 50 | define(['/foo'], function (foo) { /*...*/ }) // reported
|
|---|
| 51 | require(['/foo'], function (foo) { /*...*/ }) // reported
|
|---|
| 52 |
|
|---|
| 53 | const foo = require('/foo') // ignored because of explicit `commonjs: false`
|
|---|
| 54 | ```
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.