@adonisjs/eslint-plugin 1.0.1

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.
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /*
3
+ * @adonisjs/mail
4
+ *
5
+ * (c) AdonisJS
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ const assert_1 = require("@japa/assert");
12
+ const spec_reporter_1 = require("@japa/spec-reporter");
13
+ const runner_1 = require("@japa/runner");
14
+ /*
15
+ |--------------------------------------------------------------------------
16
+ | Configure tests
17
+ |--------------------------------------------------------------------------
18
+ |
19
+ | The configure method accepts the configuration to configure the Japa
20
+ | tests runner.
21
+ |
22
+ | The first method call "processCliArgs" process the command line arguments
23
+ | and turns them into a config object. Using this method is not mandatory.
24
+ |
25
+ | Please consult japa.dev/runner-config for the config docs.
26
+ */
27
+ (0, runner_1.configure)({
28
+ ...(0, runner_1.processCliArgs)(process.argv.slice(2)),
29
+ ...{
30
+ plugins: [(0, assert_1.assert)()],
31
+ reporters: [(0, spec_reporter_1.specReporter)()],
32
+ importer: (filePath) => Promise.resolve(`${filePath}`).then(s => require(s)),
33
+ files: ['tests/**/*.spec.ts'],
34
+ },
35
+ });
36
+ /*
37
+ |--------------------------------------------------------------------------
38
+ | Run tests
39
+ |--------------------------------------------------------------------------
40
+ |
41
+ | The following "run" method is required to execute all the tests.
42
+ |
43
+ */
44
+ (0, runner_1.run)();
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ const prefer_lazy_controller_import_1 = require("./rules/prefer_lazy_controller_import");
3
+ module.exports = {
4
+ rules: {
5
+ 'prefer-lazy-controller-import': prefer_lazy_controller_import_1.default,
6
+ },
7
+ };
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@typescript-eslint/utils");
4
+ const utils_2 = require("../utils");
5
+ const httpMethods = ['get', 'post', 'put', 'delete', 'patch'];
6
+ exports.default = (0, utils_2.createEslintRule)({
7
+ name: 'prefer-lazy-controller-import',
8
+ defaultOptions: [],
9
+ meta: {
10
+ type: 'problem',
11
+ fixable: 'code',
12
+ docs: {
13
+ description: 'Prefer lazy controller import over eager import',
14
+ recommended: 'error',
15
+ },
16
+ schema: [],
17
+ messages: {
18
+ preferLazyControllerImport: 'Prefer lazy controller import over eager import',
19
+ },
20
+ },
21
+ create: function (context) {
22
+ let importIdentifiers = [];
23
+ let routerIdentifier = '';
24
+ let importNodes = [];
25
+ function isRouteCallExpression(node, routerIdentifier) {
26
+ return (node.callee.type === utils_1.AST_NODE_TYPES.MemberExpression &&
27
+ node.callee.object.type === utils_1.AST_NODE_TYPES.Identifier &&
28
+ node.callee.object.name === routerIdentifier &&
29
+ node.callee.property.type === utils_1.AST_NODE_TYPES.Identifier &&
30
+ httpMethods.includes(node.callee.property.name));
31
+ }
32
+ return {
33
+ /**
34
+ * Track all imported identifiers
35
+ * Also get the local name of the router import
36
+ */
37
+ ImportDeclaration(node) {
38
+ for (const specifier of node.specifiers) {
39
+ if (specifier.type === 'ImportDefaultSpecifier' || specifier.type === 'ImportSpecifier') {
40
+ importIdentifiers.push(specifier.local.name);
41
+ importNodes[specifier.local.name] = node;
42
+ }
43
+ }
44
+ if (node.source.value === '@adonisjs/core/services/router') {
45
+ if (node.specifiers[0] && node.specifiers[0].type === 'ImportDefaultSpecifier') {
46
+ routerIdentifier = node.specifiers[0].local.name;
47
+ }
48
+ }
49
+ },
50
+ /**
51
+ * Check if we are calling router.get() or any other http method
52
+ */
53
+ CallExpression(node) {
54
+ if (isRouteCallExpression(node, routerIdentifier)) {
55
+ const secondArgument = node.arguments[1];
56
+ if (secondArgument.type !== utils_1.AST_NODE_TYPES.ArrayExpression) {
57
+ return;
58
+ }
59
+ for (const element of secondArgument.elements) {
60
+ if (element.type !== 'Identifier' || !importIdentifiers.includes(element.name)) {
61
+ continue;
62
+ }
63
+ context.report({
64
+ node: importNodes[element.name],
65
+ messageId: 'preferLazyControllerImport',
66
+ fix(fixer) {
67
+ const importPath = importNodes[element.name].source.raw;
68
+ const newImportDeclaration = `const ${element.name} = () => import(${importPath})`;
69
+ return fixer.replaceText(importNodes[element.name], newImportDeclaration);
70
+ },
71
+ });
72
+ }
73
+ }
74
+ },
75
+ };
76
+ },
77
+ });
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEslintRule = void 0;
4
+ const utils_1 = require("@typescript-eslint/utils");
5
+ exports.createEslintRule = utils_1.ESLintUtils.RuleCreator((ruleName) => ruleName);
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const runner_1 = require("@japa/runner");
4
+ const ts_eslint_1 = require("@typescript-eslint/utils/dist/ts-eslint");
5
+ const prefer_lazy_controller_import_1 = require("../src/rules/prefer_lazy_controller_import");
6
+ const valids = [
7
+ `
8
+ import router from "@adonisjs/core/services/router"
9
+ const lazyController = () => import("./controller")
10
+
11
+ router.get("/", "HomeController.index")
12
+ router.get("/test", [lazyController, 'index'])
13
+ `,
14
+ ];
15
+ const invalids = [
16
+ [
17
+ `
18
+ import router from "@adonisjs/core/services/router"
19
+ import HomeController from "./controller"
20
+
21
+ router.group(() => {
22
+ router.get("/", [HomeController, 'index'])
23
+ })
24
+ `,
25
+ `
26
+ import router from "@adonisjs/core/services/router"
27
+ const HomeController = () => import("./controller")
28
+
29
+ router.group(() => {
30
+ router.get("/", [HomeController, 'index'])
31
+ })
32
+ `,
33
+ ],
34
+ ];
35
+ (0, runner_1.test)('Prefer lazy controller import', ({ assert }) => {
36
+ const ruleTester = new ts_eslint_1.RuleTester({
37
+ parser: require.resolve('@typescript-eslint/parser'),
38
+ });
39
+ ruleTester.run('prefer-lazy-controller-import', prefer_lazy_controller_import_1.default, {
40
+ valid: valids,
41
+ invalid: invalids.map((invalid) => ({
42
+ code: invalid[0],
43
+ output: invalid[1],
44
+ errors: [{ messageId: 'preferLazyControllerImport' }],
45
+ })),
46
+ });
47
+ });
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@adonisjs/eslint-plugin",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "dist/src/index.js",
9
+ "keywords": [],
10
+ "author": "Julien Ripouteau <julien@ripouteau.com>",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "@typescript-eslint/utils": "^5.59.11"
14
+ },
15
+ "devDependencies": {
16
+ "@japa/assert": "^1.4.1",
17
+ "@japa/runner": "^2.5.1",
18
+ "@japa/spec-reporter": "^1.3.3",
19
+ "@types/node": "^20.3.1",
20
+ "@typescript-eslint/parser": "^5.59.11",
21
+ "ts-node": "^10.9.1",
22
+ "typescript": "^5.1.3"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc --outDir dist",
26
+ "quick:test": "ts-node bin/test.ts"
27
+ }
28
+ }