Skip to content

Commit

Permalink
add local eslint rule to enforce file extensions in import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Connum authored and yne committed Mar 9, 2023
1 parent cb0f296 commit 74e189e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 24 additions & 4 deletions eslint-local-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module.exports = {
'ban-foreach': {
meta: {
type: 'suggestion',
docs: {
description: 'Use `for()` loops instead of `.forEach()`',
category: 'Performance',
},
schema: [],
},
create(context) {
Expand All @@ -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
};
},
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"error",
"always"
],
"local-rules/ban-foreach": 2
"local-rules/ban-foreach": 2,
"local-rules/import-extensions": 2
}
},
"dependencies": {
Expand Down

0 comments on commit 74e189e

Please sign in to comment.