@dword-design/eslint-plugin-import-alias 4.0.6 → 4.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs-fallback.cjs +4 -0
- package/dist/index.js +4 -13
- package/dist/rules/prefer-alias.js +31 -40
- package/package.json +14 -8
package/dist/index.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var _preferAlias = _interopRequireDefault(require("./rules/prefer-alias"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
-
var _default = {
|
|
1
|
+
import preferAlias from "./rules/prefer-alias.js";
|
|
2
|
+
export default {
|
|
10
3
|
configs: {
|
|
11
4
|
recommended: {
|
|
12
5
|
plugins: ['@dword-design/import-alias'],
|
|
@@ -16,8 +9,6 @@ var _default = {
|
|
|
16
9
|
}
|
|
17
10
|
},
|
|
18
11
|
rules: {
|
|
19
|
-
'prefer-alias':
|
|
12
|
+
'prefer-alias': preferAlias
|
|
20
13
|
}
|
|
21
|
-
};
|
|
22
|
-
exports.default = _default;
|
|
23
|
-
module.exports = exports.default;
|
|
14
|
+
};
|
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var _replace = _interopRequireDefault(require("@dword-design/functions/dist/replace.js"));
|
|
11
|
-
var _some = _interopRequireDefault(require("@dword-design/functions/dist/some.js"));
|
|
12
|
-
var _startsWith = _interopRequireDefault(require("@dword-design/functions/dist/starts-with.js"));
|
|
13
|
-
var _babelPluginModuleResolver = require("babel-plugin-module-resolver");
|
|
14
|
-
var _path = _interopRequireDefault(require("path"));
|
|
15
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1
|
+
import { OptionManager } from '@babel/core';
|
|
2
|
+
import find from "@dword-design/functions/dist/find.js";
|
|
3
|
+
import keys from "@dword-design/functions/dist/keys.js";
|
|
4
|
+
import replace from "@dword-design/functions/dist/replace.js";
|
|
5
|
+
import some from "@dword-design/functions/dist/some.js";
|
|
6
|
+
import startsWith from "@dword-design/functions/dist/starts-with.js";
|
|
7
|
+
import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver';
|
|
8
|
+
import deepmerge from 'deepmerge';
|
|
9
|
+
import P from 'path';
|
|
16
10
|
const isParentImport = path => /^(\.\/)?\.\.\//.test(path);
|
|
17
11
|
const findMatchingAlias = (sourcePath, currentFile, options) => {
|
|
18
|
-
const resolvePath = options.resolvePath ||
|
|
19
|
-
const absoluteSourcePath =
|
|
20
|
-
for (const aliasName of (_options$alias = options.alias, (
|
|
12
|
+
const resolvePath = options.resolvePath || defaultResolvePath;
|
|
13
|
+
const absoluteSourcePath = P.resolve(P.dirname(currentFile), sourcePath);
|
|
14
|
+
for (const aliasName of (_options$alias = options.alias, keys(_options$alias))) {
|
|
21
15
|
var _options$alias, _absoluteSourcePath;
|
|
22
|
-
const path =
|
|
23
|
-
if (_absoluteSourcePath = absoluteSourcePath, (
|
|
16
|
+
const path = P.resolve(P.dirname(currentFile), resolvePath(`${aliasName}/`, currentFile, options));
|
|
17
|
+
if (_absoluteSourcePath = absoluteSourcePath, startsWith(path)(_absoluteSourcePath)) {
|
|
24
18
|
return {
|
|
25
19
|
name: aliasName,
|
|
26
20
|
path
|
|
@@ -29,47 +23,46 @@ const findMatchingAlias = (sourcePath, currentFile, options) => {
|
|
|
29
23
|
}
|
|
30
24
|
return undefined;
|
|
31
25
|
};
|
|
32
|
-
|
|
26
|
+
export default {
|
|
33
27
|
create: context => {
|
|
34
28
|
var _babelConfig$plugins;
|
|
35
29
|
const currentFile = context.getFilename();
|
|
36
|
-
const folder =
|
|
30
|
+
const folder = P.dirname(currentFile);
|
|
37
31
|
// can't check a non-file
|
|
38
32
|
if (currentFile === '<text>') return {};
|
|
39
|
-
const manager = new
|
|
33
|
+
const manager = new OptionManager();
|
|
40
34
|
const babelConfig = manager.init({
|
|
41
35
|
babelrc: true,
|
|
42
36
|
filename: currentFile,
|
|
43
37
|
root: folder
|
|
44
38
|
});
|
|
45
|
-
const plugin = (_babelConfig$plugins = babelConfig.plugins, (
|
|
39
|
+
const plugin = (_babelConfig$plugins = babelConfig.plugins, find({
|
|
46
40
|
key: 'module-resolver'
|
|
47
41
|
})(_babelConfig$plugins));
|
|
48
|
-
const options = {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
const options = deepmerge.all([{
|
|
43
|
+
alias: []
|
|
44
|
+
}, (plugin === null || plugin === void 0 ? void 0 : plugin.options) || {}, context.options[0] || {}]);
|
|
45
|
+
if (options.alias.length === 0) {
|
|
46
|
+
throw new Error('No alias configured. You have to define aliases by either passing them to the babel-plugin-module-resolver plugin in your Babel config, or directly to the prefer-alias rule.');
|
|
47
|
+
}
|
|
48
|
+
const resolvePath = options.resolvePath || defaultResolvePath;
|
|
53
49
|
return {
|
|
54
50
|
ImportDeclaration: node => {
|
|
55
51
|
var _ref, _options$alias2, _sourcePath2, _importWithoutAlias;
|
|
56
52
|
const sourcePath = node.source.value;
|
|
57
|
-
const hasAlias = (_ref = (_options$alias2 = options.alias, (
|
|
53
|
+
const hasAlias = (_ref = (_options$alias2 = options.alias, keys(_options$alias2)), some(alias => {
|
|
58
54
|
var _sourcePath;
|
|
59
|
-
return _sourcePath = sourcePath, (
|
|
55
|
+
return _sourcePath = sourcePath, startsWith(`${alias}/`)(_sourcePath);
|
|
60
56
|
})(_ref));
|
|
61
57
|
// relative parent
|
|
62
58
|
if (_sourcePath2 = sourcePath, isParentImport(_sourcePath2)) {
|
|
63
59
|
var _P$relative;
|
|
64
60
|
const matchingAlias = findMatchingAlias(sourcePath, currentFile, options);
|
|
65
61
|
if (!matchingAlias) {
|
|
66
|
-
return
|
|
67
|
-
message: `Unexpected parent import '${sourcePath}'. No matching alias found to fix the issue. You have to define aliases by either passing them to the babel-plugin-module-resolver plugin in your Babel config, or directly to the prefer-alias rule.`,
|
|
68
|
-
node
|
|
69
|
-
});
|
|
62
|
+
return undefined;
|
|
70
63
|
}
|
|
71
|
-
const absoluteImportPath =
|
|
72
|
-
const rewrittenImport = `${matchingAlias.name}/${(_P$relative =
|
|
64
|
+
const absoluteImportPath = P.resolve(folder, sourcePath);
|
|
65
|
+
const rewrittenImport = `${matchingAlias.name}/${(_P$relative = P.relative(matchingAlias.path, absoluteImportPath), replace(/\\/g, '/')(_P$relative))}`;
|
|
73
66
|
return context.report({
|
|
74
67
|
fix: fixer => fixer.replaceTextRange([node.source.range[0] + 1, node.source.range[1] - 1], rewrittenImport),
|
|
75
68
|
message: `Unexpected parent import '${sourcePath}'. Use '${rewrittenImport}' instead`,
|
|
@@ -101,6 +94,4 @@ var _default = {
|
|
|
101
94
|
}],
|
|
102
95
|
type: 'suggestion'
|
|
103
96
|
}
|
|
104
|
-
};
|
|
105
|
-
exports.default = _default;
|
|
106
|
-
module.exports = exports.default;
|
|
97
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dword-design/eslint-plugin-import-alias",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.8",
|
|
4
4
|
"description": "An ESLint plugin that enforces the use of import aliases. Also supports autofixing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"alias",
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"funding": "https://github.com/sponsors/dword-design",
|
|
28
28
|
"license": "MIT",
|
|
29
29
|
"author": "Sebastian Landwehr <info@sebastianlandwehr.com>",
|
|
30
|
-
"
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "dist/cjs-fallback.cjs",
|
|
31
32
|
"files": [
|
|
32
33
|
"dist"
|
|
33
34
|
],
|
|
@@ -43,15 +44,20 @@
|
|
|
43
44
|
},
|
|
44
45
|
"dependencies": {
|
|
45
46
|
"@babel/core": "^7.10.2",
|
|
46
|
-
"@dword-design/functions": "^
|
|
47
|
-
"babel-plugin-module-resolver": "^5.0.0"
|
|
47
|
+
"@dword-design/functions": "^5.0.22",
|
|
48
|
+
"babel-plugin-module-resolver": "^5.0.0",
|
|
49
|
+
"deepmerge": "^4.3.1",
|
|
50
|
+
"jiti": "^1.18.2"
|
|
48
51
|
},
|
|
49
52
|
"devDependencies": {
|
|
50
|
-
"@dword-design/base": "^
|
|
51
|
-
"
|
|
53
|
+
"@dword-design/base": "^10.1.0",
|
|
54
|
+
"@dword-design/tester": "^2.0.19",
|
|
55
|
+
"@dword-design/tester-plugin-tmp-dir": "^2.1.26",
|
|
56
|
+
"depcheck-package-name": "^3.0.1",
|
|
52
57
|
"eslint": "^8.0.0",
|
|
53
|
-
"
|
|
54
|
-
"
|
|
58
|
+
"execa": "^7.1.1",
|
|
59
|
+
"fs-extra": "^11.1.1",
|
|
60
|
+
"output-files": "^2.0.0"
|
|
55
61
|
},
|
|
56
62
|
"engines": {
|
|
57
63
|
"node": ">=16"
|