rule to ensure libs imports

This commit is contained in:
Niels Lyngsø
2023-03-23 23:06:29 +01:00
parent bc1119b0c6
commit fadc5580d0
2 changed files with 64 additions and 14 deletions

View File

@@ -20,18 +20,21 @@ module.exports = {
create: function (context) {
return {
ImportDeclaration: function (node) {
if (node.source.parent.importKind !== 'type' && (node.source.value.endsWith('/models') || node.source.value === 'router-slot/model')) {
if (
node.source.parent.importKind !== 'type' &&
(node.source.value.endsWith('/models') || node.source.value === 'router-slot/model')
) {
const sourceCode = context.getSourceCode();
const nodeSource = sourceCode.getText(node);
context.report({
node,
message: 'Use `import type` instead of `import`.',
fix: fixer => fixer.replaceText(node, nodeSource.replace('import', 'import type')),
fix: (fixer) => fixer.replaceText(node, nodeSource.replace('import', 'import type')),
});
}
},
};
}
},
},
/** @type {import('eslint').Rule.RuleModule} */
@@ -39,9 +42,10 @@ module.exports = {
meta: {
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.',
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.',
category: 'Best Practices',
recommended: true
recommended: true,
},
fixable: 'code',
schema: [],
@@ -50,19 +54,30 @@ module.exports = {
return {
// If methods called on *Resource classes are not already wrapped with `await tryExecuteAndNotify()`, then we should suggest to wrap them.
CallExpression: function (node) {
if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier' && node.callee.object.name.endsWith('Resource') && node.callee.property.type === 'Identifier' && node.callee.property.name !== 'constructor') {
const hasTryExecuteAndNotify = node.parent && node.parent.callee && (node.parent.callee.name === 'tryExecute' || node.parent.callee.name === 'tryExecuteAndNotify');
if (
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name.endsWith('Resource') &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name !== 'constructor'
) {
const hasTryExecuteAndNotify =
node.parent &&
node.parent.callee &&
(node.parent.callee.name === 'tryExecute' || node.parent.callee.name === 'tryExecuteAndNotify');
if (!hasTryExecuteAndNotify) {
context.report({
node,
message: 'Wrap this call with `tryExecuteAndNotify()`. Make sure to `await` the result.',
fix: fixer => [fixer.insertTextBefore(node, 'tryExecuteAndNotify(this, '), fixer.insertTextAfter(node, ')')],
fix: (fixer) => [
fixer.insertTextBefore(node, 'tryExecuteAndNotify(this, '),
fixer.insertTextAfter(node, ')'),
],
});
}
}
}
},
};
},
},
@@ -71,9 +86,10 @@ module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Ensures that the application does not rely on file system paths for imports. Instead, use import aliases or relative imports. This also solves a problem where GitHub fails on the test runner step.',
description:
'Ensures that the application does not rely on file system paths for imports. Instead, use import aliases or relative imports. This also solves a problem where GitHub fails on the test runner step.',
category: 'Best Practices',
recommended: true
recommended: true,
},
schema: [],
},
@@ -83,11 +99,44 @@ module.exports = {
if (node.source.value.startsWith('src/')) {
context.report({
node,
message: 'Prefer using import aliases or relative imports instead of absolute imports. Example: `import { MyComponent } from "src/components/MyComponent";` should be `import { MyComponent } from "@components/MyComponent";`'
message:
'Prefer using import aliases or relative imports instead of absolute imports. Example: `import { MyComponent } from "src/components/MyComponent";` should be `import { MyComponent } from "@components/MyComponent";`',
});
}
},
};
}
},
},
/** @type {import('eslint').Rule.RuleModule} */
'prefer-umbraco-cms-imports': {
meta: {
type: 'suggestion',
docs: {
description: 'Ensure that imports from "libs" namespace are replaced with "@umbraco-cms/backoffice" namespace',
category: 'Best Practices',
recommended: true,
},
fixable: 'code',
schema: [],
},
create: function (context) {
return {
ImportDeclaration: function (node) {
if (node.source.value.startsWith('libs/')) {
const newImportPath = node.source.value.replace(/^libs\//, '@umbraco-cms/backoffice/');
const specifiers = node.specifiers.map((specifier) => specifier.local.name).join(', ');
const newImportStatement = `import { ${specifiers} } from '${newImportPath}';`;
context.report({
node,
message: `Use '${newImportPath}' instead of '${node.source.value}' for imports from 'libs' namespace.`,
fix: function (fixer) {
return fixer.replaceText(node, newImportStatement);
},
});
}
},
};
},
},
};