@adonisjs/eslint-plugin 1.1.8 → 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.
package/dist/src/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
- const prefer_lazy_controller_import_1 = require("./rules/prefer_lazy_controller_import");
3
- const prefer_lazy_listener_import_1 = require("./rules/prefer_lazy_listener_import");
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const prefer_lazy_controller_import_1 = __importDefault(require("./rules/prefer_lazy_controller_import"));
6
+ const prefer_lazy_listener_import_1 = __importDefault(require("./rules/prefer_lazy_listener_import"));
4
7
  module.exports = {
5
8
  rules: {
6
9
  'prefer-lazy-controller-import': prefer_lazy_controller_import_1.default,
@@ -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
- if (!isRouteCallExpression(node, routerIdentifier)) {
63
+ const isRouteCallExpr = isRouteCallExpression(node, routerIdentifier);
64
+ const isRouteResourceExpr = isRouteResourceCallExpression(node, routerIdentifier);
65
+ if (!isRouteCallExpr && !isRouteResourceExpr) {
55
66
  return;
56
67
  }
57
68
  /**
58
- * Ensure the second argument is an array
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
- const secondArgument = node.arguments[1];
61
- if (secondArgument.type !== utils_1.AST_NODE_TYPES.ArrayExpression) {
62
- return;
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
- for (const element of secondArgument.elements) {
65
- /**
66
- * If we are dealing with an Identifier that was imported
67
- * through a standard import, then report it as an error
68
- */
69
- if (element.type !== 'Identifier' || !importIdentifiers.includes(element.name)) {
70
- continue;
71
- }
72
- context.report({
73
- node: importNodes[element.name],
74
- messageId: 'preferLazyControllerImport',
75
- fix(fixer) {
76
- const importPath = importNodes[element.name].source.raw;
77
- const newImportDeclaration = `const ${element.name} = () => import(${importPath})`;
78
- return fixer.replaceText(importNodes[element.name], newImportDeclaration);
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
  },
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const runner_1 = require("@japa/runner");
4
7
  const ts_eslint_1 = require("@typescript-eslint/utils/ts-eslint");
5
- const prefer_lazy_controller_import_1 = require("../src/rules/prefer_lazy_controller_import");
8
+ const prefer_lazy_controller_import_1 = __importDefault(require("../src/rules/prefer_lazy_controller_import"));
6
9
  const valids = [
7
10
  `
8
11
  import router from "@adonisjs/core/services/router"
@@ -40,6 +43,23 @@ const invalids = [
40
43
  })
41
44
  `,
42
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
+ ],
43
63
  ];
44
64
  (0, runner_1.test)('Prefer lazy controller import', ({ assert }) => {
45
65
  const ruleTester = new ts_eslint_1.RuleTester({
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const runner_1 = require("@japa/runner");
4
7
  const ts_eslint_1 = require("@typescript-eslint/utils/ts-eslint");
5
- const prefer_lazy_listener_import_1 = require("../src/rules/prefer_lazy_listener_import");
8
+ const prefer_lazy_listener_import_1 = __importDefault(require("../src/rules/prefer_lazy_listener_import"));
6
9
  const valids = [
7
10
  `
8
11
  import emitter from '@adonisjs/core/services/emitter'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/eslint-plugin",
3
3
  "description": "ESLint plugin to enforce AdonisJS app specific linting rules",
4
- "version": "1.1.8",
4
+ "version": "1.2.0",
5
5
  "main": "dist/src/index.js",
6
6
  "files": [
7
7
  "dist"
@@ -13,13 +13,13 @@
13
13
  "@japa/assert": "^1.4.1",
14
14
  "@japa/runner": "^2.5.1",
15
15
  "@japa/spec-reporter": "^1.3.3",
16
- "@types/node": "^20.4.1",
17
- "@typescript-eslint/parser": "^6.0.0",
16
+ "@types/node": "^20.9.4",
17
+ "@typescript-eslint/parser": "^6.12.0",
18
18
  "ts-node": "^10.9.1",
19
- "typescript": "^5.1.6"
19
+ "typescript": "5.2.2"
20
20
  },
21
21
  "dependencies": {
22
- "@typescript-eslint/utils": "^6.0.0"
22
+ "@typescript-eslint/utils": "^6.12.0"
23
23
  },
24
24
  "author": "Julien Ripouteau <julien@ripouteau.com>",
25
25
  "license": "MIT",