about summary refs log tree commit diff
path: root/eslint
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-09-20 09:11:29 +0100
committerGitHub <noreply@github.com>2024-09-20 09:11:29 +0100
commitf42d44112d268588f0d25f81715d01190b7047ea (patch)
treecc5a7240650fd17bcca4a08c3bb3770f9cad7e69 /eslint
parent22410a3cee459761a9eb2d1a52de99557a424797 (diff)
downloadvoidsky-f42d44112d268588f0d25f81715d01190b7047ea.tar.zst
Add eslint rule to fix imports without the `#/` path alias (#5175)
Diffstat (limited to 'eslint')
-rw-r--r--eslint/index.js1
-rw-r--r--eslint/use-exact-imports.js8
-rw-r--r--eslint/use-prefixed-imports.js39
3 files changed, 44 insertions, 4 deletions
diff --git a/eslint/index.js b/eslint/index.js
index cf5d41225..6f75f1bc3 100644
--- a/eslint/index.js
+++ b/eslint/index.js
@@ -5,5 +5,6 @@ module.exports = {
     'avoid-unwrapped-text': require('./avoid-unwrapped-text'),
     'use-exact-imports': require('./use-exact-imports'),
     'use-typed-gates': require('./use-typed-gates'),
+    'use-prefixed-imports': require('./use-prefixed-imports'),
   },
 }
diff --git a/eslint/use-exact-imports.js b/eslint/use-exact-imports.js
index 06723043f..26e688563 100644
--- a/eslint/use-exact-imports.js
+++ b/eslint/use-exact-imports.js
@@ -1,4 +1,3 @@
-/* eslint-disable bsky-internal/use-exact-imports */
 const BANNED_IMPORTS = [
   '@fortawesome/free-regular-svg-icons',
   '@fortawesome/free-solid-svg-icons',
@@ -6,11 +5,12 @@ const BANNED_IMPORTS = [
 
 exports.create = function create(context) {
   return {
-    Literal(node) {
-      if (typeof node.value !== 'string') {
+    ImportDeclaration(node) {
+      const source = node.source
+      if (typeof source.value !== 'string') {
         return
       }
-      if (BANNED_IMPORTS.includes(node.value)) {
+      if (BANNED_IMPORTS.includes(source.value)) {
         context.report({
           node,
           message:
diff --git a/eslint/use-prefixed-imports.js b/eslint/use-prefixed-imports.js
new file mode 100644
index 000000000..141d53648
--- /dev/null
+++ b/eslint/use-prefixed-imports.js
@@ -0,0 +1,39 @@
+const BANNED_IMPORT_PREFIXES = [
+  'alf/',
+  'components/',
+  'lib/',
+  'locale/',
+  'logger/',
+  'platform/',
+  'state/',
+  'storage/',
+  'view/',
+]
+
+module.exports = {
+  meta: {
+    type: 'suggestion',
+    fixable: 'code',
+  },
+  create(context) {
+    return {
+      ImportDeclaration(node) {
+        const source = node.source
+        if (typeof source.value !== 'string') {
+          return
+        }
+        if (
+          BANNED_IMPORT_PREFIXES.some(banned => source.value.startsWith(banned))
+        ) {
+          context.report({
+            node: source,
+            message: `Use '#/${source.value}'`,
+            fix(fixer) {
+              return fixer.replaceText(source, `'#/${source.value}'`)
+            },
+          })
+        }
+      },
+    }
+  },
+}