From f42d44112d268588f0d25f81715d01190b7047ea Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Fri, 20 Sep 2024 09:11:29 +0100 Subject: Add eslint rule to fix imports without the `#/` path alias (#5175) --- eslint/use-prefixed-imports.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 eslint/use-prefixed-imports.js (limited to 'eslint/use-prefixed-imports.js') 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}'`) + }, + }) + } + }, + } + }, +} -- cgit 1.4.1