@adonisjs/eslint-plugin 1.1.9 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -29,6 +29,13 @@ exports.default = (0, utils_2.createEslintRule)({
|
|
|
29
29
|
node.callee.property.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
30
30
|
httpMethods.includes(node.callee.property.name));
|
|
31
31
|
}
|
|
32
|
+
function isRouteResourceCallExpression(node, identifier) {
|
|
33
|
+
return (node.callee.type === utils_1.AST_NODE_TYPES.MemberExpression &&
|
|
34
|
+
node.callee.object.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
35
|
+
node.callee.object.name === identifier &&
|
|
36
|
+
node.callee.property.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
37
|
+
node.callee.property.name === 'resource');
|
|
38
|
+
}
|
|
32
39
|
return {
|
|
33
40
|
/**
|
|
34
41
|
* Track all imported identifiers
|
|
@@ -50,35 +57,54 @@ exports.default = (0, utils_2.createEslintRule)({
|
|
|
50
57
|
CallExpression(node) {
|
|
51
58
|
/**
|
|
52
59
|
* Check if we are calling router.get() or any other http method
|
|
60
|
+
* OR if we are calling router.resource that also takes a controller
|
|
61
|
+
* as an argument
|
|
53
62
|
*/
|
|
54
|
-
|
|
63
|
+
const isRouteCallExpr = isRouteCallExpression(node, routerIdentifier);
|
|
64
|
+
const isRouteResourceExpr = isRouteResourceCallExpression(node, routerIdentifier);
|
|
65
|
+
if (!isRouteCallExpr && !isRouteResourceExpr) {
|
|
55
66
|
return;
|
|
56
67
|
}
|
|
57
68
|
/**
|
|
58
|
-
*
|
|
69
|
+
* Now let's extract the controller identifier from the call expression
|
|
70
|
+
*
|
|
71
|
+
* In the case of router.get/post/put.. we have to extract
|
|
72
|
+
* the first element from the array
|
|
73
|
+
*
|
|
74
|
+
* router.get("/", [HomeController, 'index'])
|
|
59
75
|
*/
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
76
|
+
let controller;
|
|
77
|
+
if (isRouteCallExpr) {
|
|
78
|
+
const secondArgument = node.arguments[1];
|
|
79
|
+
if (secondArgument.type !== utils_1.AST_NODE_TYPES.ArrayExpression)
|
|
80
|
+
return;
|
|
81
|
+
controller = secondArgument.elements[0];
|
|
63
82
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
},
|
|
80
|
-
});
|
|
83
|
+
/**
|
|
84
|
+
* In the case of router.resource, we just have to extract the first argument
|
|
85
|
+
*
|
|
86
|
+
* router.resource("foo", UserController)
|
|
87
|
+
*/
|
|
88
|
+
if (isRouteResourceExpr) {
|
|
89
|
+
controller = node.arguments[1];
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* If we are dealing with an Identifier that was imported
|
|
93
|
+
* through a standard import, then report it as an error
|
|
94
|
+
*/
|
|
95
|
+
const element = controller;
|
|
96
|
+
if (element.type !== 'Identifier' || !importIdentifiers.includes(element.name)) {
|
|
97
|
+
return;
|
|
81
98
|
}
|
|
99
|
+
context.report({
|
|
100
|
+
node: importNodes[element.name],
|
|
101
|
+
messageId: 'preferLazyControllerImport',
|
|
102
|
+
fix(fixer) {
|
|
103
|
+
const importPath = importNodes[element.name].source.raw;
|
|
104
|
+
const newImportDeclaration = `const ${element.name} = () => import(${importPath})`;
|
|
105
|
+
return fixer.replaceText(importNodes[element.name], newImportDeclaration);
|
|
106
|
+
},
|
|
107
|
+
});
|
|
82
108
|
},
|
|
83
109
|
};
|
|
84
110
|
},
|
|
@@ -43,6 +43,23 @@ const invalids = [
|
|
|
43
43
|
})
|
|
44
44
|
`,
|
|
45
45
|
],
|
|
46
|
+
/**
|
|
47
|
+
* When used with route.resource
|
|
48
|
+
*/
|
|
49
|
+
[
|
|
50
|
+
`
|
|
51
|
+
import router from "@adonisjs/core/services/router"
|
|
52
|
+
import ProjectThreadsController from "./controller"
|
|
53
|
+
|
|
54
|
+
router.resource("project/:id/threads", ProjectThreadsController)
|
|
55
|
+
`,
|
|
56
|
+
`
|
|
57
|
+
import router from "@adonisjs/core/services/router"
|
|
58
|
+
const ProjectThreadsController = () => import("./controller")
|
|
59
|
+
|
|
60
|
+
router.resource("project/:id/threads", ProjectThreadsController)
|
|
61
|
+
`,
|
|
62
|
+
],
|
|
46
63
|
];
|
|
47
64
|
(0, runner_1.test)('Prefer lazy controller import', ({ assert }) => {
|
|
48
65
|
const ruleTester = new ts_eslint_1.RuleTester({
|