add local eslint rule to force certain imports from router-slot

This commit is contained in:
Jacob Overgaard
2023-04-04 16:47:44 +02:00
parent 630b727a02
commit 27499d1344
2 changed files with 35 additions and 1 deletions

View File

@@ -43,6 +43,7 @@
"local-rules/prefer-import-aliases": "error",
"local-rules/enforce-element-suffix-on-element-class-name": "error",
"local-rules/prefer-umbraco-cms-imports": "error",
"local-rules/no-external-imports": "error",
"@typescript-eslint/no-non-null-assertion": "off"
},
"settings": {

View File

@@ -43,7 +43,7 @@ module.exports = {
type: 'suggestion',
docs: {
description:
'Ensures that any API resources from the `@umbraco-cms/backend-api` module are not used directly. Instead you should use the `tryExecuteAndNotify` function from the `@umbraco-cms/resources` module.',
'Ensures that any API resources from the `@umbraco-cms/backoffice/backend-api` module are not used directly. Instead you should use the `tryExecuteAndNotify` function from the `@umbraco-cms/resources` module.',
category: 'Best Practices',
recommended: true,
},
@@ -107,6 +107,7 @@ module.exports = {
};
},
},
/** @type {import('eslint').Rule.RuleModule} */
'enforce-element-suffix-on-element-class-name': {
meta: {
@@ -138,6 +139,7 @@ module.exports = {
};
},
},
// TODO: Its not bullet proof, but it will catch most/some cases.
/** @type {import('eslint').Rule.RuleModule} */
'prefer-umbraco-cms-imports': {
@@ -172,4 +174,35 @@ module.exports = {
};
},
},
/** @type {import('eslint').Rule.RuleModule} */
'no-external-imports': {
meta: {
type: 'problem',
docs: {
description:
'Ensures that the application does not rely on imports from external packages. Instead, use the @umbraco-cms/backoffice libs.',
recommended: true,
},
fixable: 'code',
schema: [],
},
create: function (context) {
return {
ImportDeclaration: function (node) {
// Check for imports from "router-slot"
if (node.source.value.startsWith('router-slot')) {
context.report({
node,
message:
'Use the `@umbraco-cms/backoffice/router` package instead of importing directly from "router-slot" because we might change that dependency in the future.',
fix: (fixer) => {
return fixer.replaceTextRange(node.source.range, `'@umbraco-cms/backoffice/router'`);
},
});
}
},
};
},
},
};