about summary refs log tree commit diff
path: root/eslint/use-prefixed-imports.js
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/use-prefixed-imports.js
parent22410a3cee459761a9eb2d1a52de99557a424797 (diff)
downloadvoidsky-f42d44112d268588f0d25f81715d01190b7047ea.tar.zst
Add eslint rule to fix imports without the `#/` path alias (#5175)
Diffstat (limited to 'eslint/use-prefixed-imports.js')
-rw-r--r--eslint/use-prefixed-imports.js39
1 files changed, 39 insertions, 0 deletions
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}'`)
+            },
+          })
+        }
+      },
+    }
+  },
+}