@dword-design/eslint-plugin-import-alias 2.0.7 → 3.0.1

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/index.js CHANGED
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _preferAlias = _interopRequireDefault(require("./rules/prefer-alias"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
9
  var _default = {
13
10
  configs: {
14
11
  recommended: {
@@ -4,37 +4,22 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _core = require("@babel/core");
9
-
10
- var _find = _interopRequireDefault(require("@dword-design/functions/dist/find"));
11
-
12
- var _keys = _interopRequireDefault(require("@dword-design/functions/dist/keys"));
13
-
14
- var _replace = _interopRequireDefault(require("@dword-design/functions/dist/replace"));
15
-
16
- var _some = _interopRequireDefault(require("@dword-design/functions/dist/some"));
17
-
18
- var _startsWith = _interopRequireDefault(require("@dword-design/functions/dist/starts-with"));
19
-
8
+ var _find = _interopRequireDefault(require("@dword-design/functions/dist/find.js"));
9
+ var _keys = _interopRequireDefault(require("@dword-design/functions/dist/keys.js"));
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"));
20
13
  var _babelPluginModuleResolver = require("babel-plugin-module-resolver");
21
-
22
14
  var _path = _interopRequireDefault(require("path"));
23
-
24
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
16
  const isParentImport = path => /^(\.\/)?\.\.\//.test(path);
27
-
28
17
  const findMatchingAlias = (sourcePath, currentFile, options) => {
29
18
  const resolvePath = options.resolvePath || _babelPluginModuleResolver.resolvePath;
30
-
31
19
  const absoluteSourcePath = _path.default.resolve(_path.default.dirname(currentFile), sourcePath);
32
-
33
20
  for (const aliasName of (_options$alias = options.alias, (0, _keys.default)(_options$alias))) {
34
21
  var _options$alias, _absoluteSourcePath;
35
-
36
22
  const path = _path.default.resolve(_path.default.dirname(currentFile), resolvePath(`${aliasName}/`, currentFile, options));
37
-
38
23
  if (_absoluteSourcePath = absoluteSourcePath, (0, _startsWith.default)(path)(_absoluteSourcePath)) {
39
24
  return {
40
25
  name: aliasName,
@@ -42,19 +27,14 @@ const findMatchingAlias = (sourcePath, currentFile, options) => {
42
27
  };
43
28
  }
44
29
  }
45
-
46
30
  return undefined;
47
31
  };
48
-
49
32
  var _default = {
50
33
  create: context => {
51
34
  var _babelConfig$plugins;
52
-
53
35
  const currentFile = context.getFilename();
54
-
55
- const folder = _path.default.dirname(currentFile); // can't check a non-file
56
-
57
-
36
+ const folder = _path.default.dirname(currentFile);
37
+ // can't check a non-file
58
38
  if (currentFile === '<text>') return {};
59
39
  const manager = new _core.OptionManager();
60
40
  const babelConfig = manager.init({
@@ -65,35 +45,30 @@ var _default = {
65
45
  const plugin = (_babelConfig$plugins = babelConfig.plugins, (0, _find.default)({
66
46
  key: 'module-resolver'
67
47
  })(_babelConfig$plugins));
68
- const options = { ...(plugin === null || plugin === void 0 ? void 0 : plugin.options),
48
+ const options = {
49
+ ...(plugin === null || plugin === void 0 ? void 0 : plugin.options),
69
50
  ...context.options[0]
70
51
  };
71
52
  const resolvePath = options.resolvePath || _babelPluginModuleResolver.resolvePath;
72
53
  return {
73
54
  ImportDeclaration: node => {
74
55
  var _ref, _options$alias2, _sourcePath2, _importWithoutAlias;
75
-
76
56
  const sourcePath = node.source.value;
77
57
  const hasAlias = (_ref = (_options$alias2 = options.alias, (0, _keys.default)(_options$alias2)), (0, _some.default)(alias => {
78
58
  var _sourcePath;
79
-
80
59
  return _sourcePath = sourcePath, (0, _startsWith.default)(`${alias}/`)(_sourcePath);
81
- })(_ref)); // relative parent
82
-
60
+ })(_ref));
61
+ // relative parent
83
62
  if (_sourcePath2 = sourcePath, isParentImport(_sourcePath2)) {
84
63
  var _P$relative;
85
-
86
64
  const matchingAlias = findMatchingAlias(sourcePath, currentFile, options);
87
-
88
65
  if (!matchingAlias) {
89
66
  return context.report({
90
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.`,
91
68
  node
92
69
  });
93
70
  }
94
-
95
71
  const absoluteImportPath = _path.default.resolve(folder, sourcePath);
96
-
97
72
  const rewrittenImport = `${matchingAlias.name}/${(_P$relative = _path.default.relative(matchingAlias.path, absoluteImportPath), (0, _replace.default)(/\\/g, '/')(_P$relative))}`;
98
73
  return context.report({
99
74
  fix: fixer => fixer.replaceTextRange([node.source.range[0] + 1, node.source.range[1] - 1], rewrittenImport),
@@ -101,9 +76,7 @@ var _default = {
101
76
  node
102
77
  });
103
78
  }
104
-
105
79
  const importWithoutAlias = resolvePath(sourcePath, currentFile, options);
106
-
107
80
  if (!(_importWithoutAlias = importWithoutAlias, isParentImport(_importWithoutAlias)) && hasAlias) {
108
81
  return context.report({
109
82
  fix: fixer => fixer.replaceTextRange([node.source.range[0] + 1, node.source.range[1] - 1], importWithoutAlias),
@@ -111,7 +84,6 @@ var _default = {
111
84
  node
112
85
  });
113
86
  }
114
-
115
87
  return undefined;
116
88
  }
117
89
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dword-design/eslint-plugin-import-alias",
3
- "version": "2.0.7",
3
+ "version": "3.0.1",
4
4
  "description": "An ESLint plugin that enforces the use of import aliases. Also supports autofixing.",
5
5
  "keywords": [
6
6
  "alias",
@@ -34,6 +34,7 @@
34
34
  "scripts": {
35
35
  "checkUnknownFiles": "base checkUnknownFiles",
36
36
  "commit": "base commit",
37
+ "depcheck": "base depcheck",
37
38
  "dev": "base dev",
38
39
  "lint": "base lint",
39
40
  "prepare": "base prepare",
@@ -46,14 +47,14 @@
46
47
  "babel-plugin-module-resolver": "^4.0.0"
47
48
  },
48
49
  "devDependencies": {
49
- "@dword-design/base": "^8.0.0",
50
+ "@dword-design/base": "^9.2.3",
50
51
  "depcheck-package-name": "^2.0.0",
51
- "eslint": "^7.2.0",
52
+ "eslint": "^8.0.0",
52
53
  "output-files": "^2.0.0",
53
54
  "with-local-tmp-dir": "^4.0.0"
54
55
  },
55
56
  "engines": {
56
- "node": ">=12"
57
+ "node": ">=14"
57
58
  },
58
59
  "publishConfig": {
59
60
  "access": "public"