Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No-relative-import fixer doesn't actually work #9

Open
anand-sundaram-zocdoc opened this issue Aug 20, 2019 · 1 comment
Open

No-relative-import fixer doesn't actually work #9

anand-sundaram-zocdoc opened this issue Aug 20, 2019 · 1 comment

Comments

@anand-sundaram-zocdoc
Copy link

I ran eslint --fix on my monorepo and it correctly identified a relative import as an error but did not fix it, even though this plugin says the relative import error is autofixable.

@jrwpatterson
Copy link

@anand-sundaram-zocdoc little late I know but if you never solved this or anybody else that has this problem. I have created a pr for this #30 don't know if the library is still being maintained. if not add the following code into your library.

I this also adds a fixer for the no-internal-import

I have created a file patches/eslint-plugin-monorepo.patch
Then put that code in there
then in my package.json I have:

    "eslint-plugin-monorepo": "patch:eslint-plugin-monorepo@0.3.2#patches/eslint-plugin-monorepo.patch"
diff --git a/lib/rules/no-internal-import.js b/lib/rules/no-internal-import.js
index c56792a399c37553d92891a26347d218bba74037..a801eb08c2d694721f98adbab80b77e70245303b 100644
--- a/lib/rules/no-internal-import.js
+++ b/lib/rules/no-internal-import.js
@@ -34,7 +34,8 @@ var _fs2 = _interopRequireDefault(_fs);
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
 var meta = exports.meta = {
-  schema: [(0, _moduleVisitor.makeOptionsSchema)({})]
+  schema: [(0, _moduleVisitor.makeOptionsSchema)({})],
+  fixable: 'code'
 };
 
 var withoutExtension = function withoutExtension(importFile, fileEntry) {
@@ -102,7 +103,11 @@ var create = exports.create = function create(context) {
 
     context.report({
       node: node,
-      message: 'Import for monorepo package \'' + name + '\' is internal.'
+      message: 'Import for monorepo package \'' + name + '\' is internal.',
+      fix: function fix(fixer) {
+        const path = _path.parse(node.value).dir
+        return fixer.replaceText(node, '\'' + path + '\'');
+      }
     });
   }, moduleUtilOptions);
 };
diff --git a/lib/rules/no-relative-import.js b/lib/rules/no-relative-import.js
index 1298280d1e67e7753fc8bc7d9fc8dcbf1d8477dd..f2c048be13f221e1e12cdc012210fd0a5159c476 100644
--- a/lib/rules/no-relative-import.js
+++ b/lib/rules/no-relative-import.js
@@ -33,12 +33,17 @@ var _getMonorepoPackages2 = _interopRequireDefault(_getMonorepoPackages);
 
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
-var meta = exports.meta = {
-  schema: [(0, _moduleVisitor.makeOptionsSchema)({})],
-  fixable: 'code'
-};
-
-var create = exports.create = function create(context) {
+module.exports = {
+ meta: {
+  docs: {
+      description: "disallow relative imports",
+      category: "Possible Errors",
+      recommended: true
+  },
+  schema: [],
+  fixable: "code",
+ },
+ create: function create(context) {
   var _context$options = _slicedToArray(context.options, 1),
       moduleUtilOptions = _context$options[0];
 
@@ -47,6 +52,7 @@ var create = exports.create = function create(context) {
 
   return (0, _moduleVisitor2.default)(function (node) {
     var resolvedPath = (0, _resolve2.default)(node.value, context);
+
     var packageDir = getPackageDir(sourceFsPath, packages);
 
     if (!packageDir || !resolvedPath || (0, _pathIsInside2.default)(resolvedPath, packageDir)) {
@@ -61,15 +67,19 @@ var create = exports.create = function create(context) {
     }
 
     var subPackagePath = _path2.default.relative(pkg.location, resolvedPath);
-    context.report({
+    return context.report({
       node: node,
       message: 'Import for monorepo package \'' + pkg.package.name + '\' should be absolute.',
       fix: function fix(fixer) {
-        fixer.replaceText(node, '' + pkg.package.name + (subPackagePath !== '.' ? '/' + subPackagePath : ''));
+        const basePath = '' + pkg.package.name + (subPackagePath !== '.' ? '/' + subPackagePath : '')
+        const path = _path.parse(basePath).dir
+        const name = _path.parse(basePath).name
+        return fixer.replaceText(node, '\'' + (name !== '.' && name !== 'index' ? path + '/' + name : path) + '\'');
       }
     });
   }, moduleUtilOptions);
-};
+ }
+}
 
 var getPackageDir = function getPackageDir(filePath, packages) {
   var match = packages.find(function (pkg) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants