From 74e189e10d5d7c263ae1095544b38cc8fb59ead5 Mon Sep 17 00:00:00 2001 From: Connum Date: Mon, 6 Mar 2023 19:17:49 +0100 Subject: [PATCH] add local eslint rule to enforce file extensions in import paths --- eslint-local-rules.js | 28 ++++++++++++++++++++++++---- package.json | 3 ++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/eslint-local-rules.js b/eslint-local-rules.js index a3ec1f66..fdcc8538 100644 --- a/eslint-local-rules.js +++ b/eslint-local-rules.js @@ -4,10 +4,6 @@ module.exports = { 'ban-foreach': { meta: { type: 'suggestion', - docs: { - description: 'Use `for()` loops instead of `.forEach()`', - category: 'Performance', - }, schema: [], }, create(context) { @@ -20,4 +16,28 @@ module.exports = { } }, }, + 'import-extensions': { + meta: { + type: 'problem', + schema: [] + }, + create(context) { + const checkImportPath = (node) => { + const importPath = node.source.value; + const isRelative = importPath.startsWith('.') || importPath.startsWith('/'); + const extensionMissing = require('path').extname(importPath) === ''; + if (!isRelative || !extensionMissing) { + return; + } + context.report({ + node: node.source, + message: 'Import paths require a file extension to work in browser module context' + }); + }; + + return { + ImportDeclaration: checkImportPath + }; + }, + } }; \ No newline at end of file diff --git a/package.json b/package.json index 3381dc8f..13f48871 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,8 @@ "error", "always" ], - "local-rules/ban-foreach": 2 + "local-rules/ban-foreach": 2, + "local-rules/import-extensions": 2 } }, "dependencies": {