ソースを参照

eslint rule to enforce .js on relative import paths

https://github.com/solana-labs/wallet-adapter/issues/546
John Rees 3 年 前
コミット
57d9ca5599
4 ファイル変更204 行追加93 行削除
  1. 32 0
      .eslint/eslint-plugin-relative-files/index.js
  2. 14 1
      .eslintrc
  3. 1 0
      package.json
  4. 157 92
      pnpm-lock.yaml

+ 32 - 0
.eslint/eslint-plugin-relative-files/index.js

@@ -0,0 +1,32 @@
+const { existsSync } = require('fs');
+const { dirname, join } = require('path');
+
+module.exports.rules = {
+    'must-end-with-js': {
+        meta: {
+            fixable: true,
+        },
+        create: function (context) {
+            const fixSourcePath = (node) => {
+                const source = node.source.value;
+                // if it's a relative import or export that doesn't end with .js
+                if (source.startsWith('.') && !source.endsWith('.js')) {
+                    const fullPath = join(dirname(context.getFilename()), node.source.value);
+                    // and a .tsx? file exists
+                    if (existsSync(`${fullPath}.ts`) || existsSync(`${fullPath}.tsx`)) {
+                        context.report({
+                            node,
+                            message: 'Relative imports and exports must end with .js',
+                            fix: (fixer) => fixer.replaceText(node.source, `'${node.source.value}.js'`),
+                        });
+                    }
+                }
+            };
+
+            return {
+                ExportAllDeclaration: fixSourcePath,
+                ImportDeclaration: fixSourcePath,
+            };
+        },
+    },
+};

+ 14 - 1
.eslintrc

@@ -9,6 +9,7 @@
     ],
     "parser": "@typescript-eslint/parser",
     "plugins": [
+        "relative-files",
         "@typescript-eslint",
         "prettier",
         "react",
@@ -26,5 +27,17 @@
         "@typescript-eslint/no-empty-interface": "off",
         "@typescript-eslint/consistent-type-imports": "error",
         "react/no-unescaped-entities": ["error", { "forbid": [">"] }]
-    }
+    },
+    "overrides": [
+        {
+            "files": [
+                "packages/core/**/*.ts*",
+                "packages/ui/**/*.ts*",
+                "packages/wallets/**/*.ts*"
+            ],
+            "rules": {
+                "relative-files/must-end-with-js": "error"
+            }
+        }
+    ]
 }

+ 1 - 0
package.json

@@ -41,6 +41,7 @@
         "eslint-plugin-prettier": "^4.2.1",
         "eslint-plugin-react": "^7.30.1",
         "eslint-plugin-react-hooks": "^4.6.0",
+        "eslint-plugin-relative-files": "file:.eslint/eslint-plugin-relative-files",
         "gh-pages": "^4.0.0",
         "pnpm": "^7.8.0",
         "prettier": "^2.7.1",

ファイルの差分が大きいため隠しています
+ 157 - 92
pnpm-lock.yaml


この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません