@checkdigit/eslint-plugin 7.4.1 → 7.5.0-PR.75-0252
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/README.md +1 -1
- package/dist-mjs/agent/add-assert-import.mjs +58 -0
- package/dist-mjs/agent/add-base-path-const.mjs +65 -0
- package/dist-mjs/agent/add-base-path-import.mjs +60 -0
- package/dist-mjs/agent/add-url-domain.mjs +61 -0
- package/dist-mjs/agent/agent-test-wiring.mjs +221 -0
- package/dist-mjs/agent/fetch-response-body-json.mjs +146 -0
- package/dist-mjs/agent/fetch-response-header-getter.mjs +117 -0
- package/dist-mjs/agent/fetch-response-status.mjs +66 -0
- package/dist-mjs/agent/fetch-then.mjs +269 -0
- package/dist-mjs/agent/fetch.mjs +38 -0
- package/dist-mjs/agent/file.mjs +43 -0
- package/dist-mjs/agent/fix-function-call-arguments.mjs +153 -0
- package/dist-mjs/agent/no-fixture.mjs +361 -0
- package/dist-mjs/agent/no-mapped-response.mjs +75 -0
- package/dist-mjs/agent/no-service-wrapper.mjs +185 -0
- package/dist-mjs/agent/no-status-code.mjs +59 -0
- package/dist-mjs/agent/no-unused-function-argument.mjs +79 -0
- package/dist-mjs/agent/no-unused-imports.mjs +81 -0
- package/dist-mjs/agent/no-unused-service-variable.mjs +74 -0
- package/dist-mjs/agent/response-reference.mjs +70 -0
- package/dist-mjs/agent/url.mjs +32 -0
- package/dist-mjs/index.mjs +205 -51
- package/dist-mjs/library/tree.mjs +2 -2
- package/dist-mjs/no-legacy-service-typing.mjs +39 -0
- package/dist-mjs/{agent/no-serve-runtime.mjs → no-serve-runtime.mjs} +3 -3
- package/dist-mjs/require-fixed-services-import.mjs +1 -1
- package/dist-mjs/require-resolve-full-response.mjs +30 -15
- package/dist-types/agent/add-assert-import.d.ts +4 -0
- package/dist-types/agent/add-base-path-const.d.ts +4 -0
- package/dist-types/agent/add-base-path-import.d.ts +4 -0
- package/dist-types/agent/add-url-domain.d.ts +4 -0
- package/dist-types/agent/agent-test-wiring.d.ts +4 -0
- package/dist-types/agent/fetch-response-body-json.d.ts +4 -0
- package/dist-types/agent/fetch-response-header-getter.d.ts +4 -0
- package/dist-types/agent/fetch-response-status.d.ts +4 -0
- package/dist-types/agent/fetch-then.d.ts +4 -0
- package/dist-types/agent/fetch.d.ts +5 -0
- package/dist-types/agent/file.d.ts +7 -0
- package/dist-types/agent/fix-function-call-arguments.d.ts +9 -0
- package/dist-types/agent/no-fixture.d.ts +4 -0
- package/dist-types/agent/no-mapped-response.d.ts +4 -0
- package/dist-types/agent/no-service-wrapper.d.ts +4 -0
- package/dist-types/agent/no-status-code.d.ts +4 -0
- package/dist-types/agent/no-unused-function-argument.d.ts +4 -0
- package/dist-types/agent/no-unused-imports.d.ts +4 -0
- package/dist-types/agent/no-unused-service-variable.d.ts +4 -0
- package/dist-types/agent/response-reference.d.ts +16 -0
- package/dist-types/agent/url.d.ts +4 -0
- package/dist-types/index.d.ts +4 -2
- package/dist-types/no-legacy-service-typing.d.ts +5 -0
- package/package.json +1 -96
- package/src/agent/add-assert-import.ts +74 -0
- package/src/agent/add-base-path-const.ts +81 -0
- package/src/agent/add-base-path-import.ts +69 -0
- package/src/agent/add-url-domain.ts +76 -0
- package/src/agent/agent-test-wiring.ts +273 -0
- package/src/agent/fetch-response-body-json.ts +197 -0
- package/src/agent/fetch-response-header-getter.ts +148 -0
- package/src/agent/fetch-response-status.ts +87 -0
- package/src/agent/fetch-then.ts +357 -0
- package/src/agent/fetch.ts +57 -0
- package/src/agent/file.ts +42 -0
- package/src/agent/fix-function-call-arguments.ts +200 -0
- package/src/agent/no-fixture.ts +521 -0
- package/src/agent/no-mapped-response.ts +84 -0
- package/src/agent/no-service-wrapper.ts +241 -0
- package/src/agent/no-status-code.ts +72 -0
- package/src/agent/no-unused-function-argument.ts +98 -0
- package/src/agent/no-unused-imports.ts +103 -0
- package/src/agent/no-unused-service-variable.ts +93 -0
- package/src/agent/response-reference.ts +129 -0
- package/src/agent/url.ts +32 -0
- package/src/index.ts +206 -50
- package/src/library/tree.ts +1 -0
- package/src/no-legacy-service-typing.ts +49 -0
- package/src/{agent/no-serve-runtime.ts → no-serve-runtime.ts} +2 -2
- package/src/require-fixed-services-import.ts +1 -1
- package/src/require-resolve-full-response.ts +38 -21
- package/dist-mjs/agent/no-full-response.mjs +0 -35
- package/dist-types/agent/no-full-response.d.ts +0 -4
- package/src/agent/no-full-response.ts +0 -41
- /package/dist-types/{agent/no-serve-runtime.d.ts → no-serve-runtime.d.ts} +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { MemberExpression, ObjectPattern, VariableDeclaration } from 'estree';
|
|
2
|
+
import { type Scope } from 'eslint';
|
|
3
|
+
/**
|
|
4
|
+
* analyze response related variables and their references
|
|
5
|
+
* the implementation is for fixture API, but it can be used for fetch API as well since the tree structure is similar
|
|
6
|
+
* @param variableDeclaration - variable declaration node
|
|
7
|
+
*/
|
|
8
|
+
export declare function analyzeResponseReferences(variableDeclaration: VariableDeclaration | undefined, scopeManager: Scope.ScopeManager): {
|
|
9
|
+
variable?: Scope.Variable;
|
|
10
|
+
bodyReferences: MemberExpression[];
|
|
11
|
+
headersReferences: MemberExpression[];
|
|
12
|
+
statusReferences: MemberExpression[];
|
|
13
|
+
destructuringBodyVariable?: Scope.Variable | ObjectPattern;
|
|
14
|
+
destructuringHeadersVariable?: Scope.Variable | ObjectPattern;
|
|
15
|
+
destructuringHeadersReferences?: MemberExpression[] | undefined;
|
|
16
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function isServiceApiCallUrl(url: string): boolean;
|
|
2
|
+
export declare function replaceEndpointUrlPrefixWithBasePath(url: string): string;
|
|
3
|
+
export declare function replaceEndpointUrlPrefixWithDomain(url: string): string;
|
|
4
|
+
export declare function addBasePathUrlDomain(url: string): string;
|
package/dist-types/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { TSESLint } from '@typescript-eslint/utils';
|
|
2
|
-
declare const
|
|
3
|
-
|
|
2
|
+
declare const defaultToExport: Exclude<TSESLint.FlatConfig.Plugin, 'config'> & {
|
|
3
|
+
configs: Record<string, TSESLint.FlatConfig.Config | TSESLint.FlatConfig.Config[]>;
|
|
4
|
+
};
|
|
5
|
+
export default defaultToExport;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
export declare const ruleId = "no-legacy-service-typing";
|
|
3
|
+
declare const DISALLOWED_SERVICE_TYPINGS: string[] | undefined;
|
|
4
|
+
declare const rule: ESLintUtils.RuleModule<'noLegacyServiceTyping', [typeof DISALLOWED_SERVICE_TYPINGS]>;
|
|
5
|
+
export default rule;
|
package/package.json
CHANGED
|
@@ -1,96 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@checkdigit/eslint-plugin",
|
|
3
|
-
"version": "7.4.1",
|
|
4
|
-
"description": "Check Digit eslint plugins",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"eslint",
|
|
7
|
-
"eslintplugin"
|
|
8
|
-
],
|
|
9
|
-
"homepage": "https://github.com/checkdigit/eslint-plugin#readme",
|
|
10
|
-
"bugs": {
|
|
11
|
-
"url": "https://github.com/checkdigit/eslint-plugin/issues"
|
|
12
|
-
},
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "https://github.com/checkdigit/eslint-plugin"
|
|
16
|
-
},
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"author": "Check Digit, LLC",
|
|
19
|
-
"sideEffects": false,
|
|
20
|
-
"type": "module",
|
|
21
|
-
"exports": {
|
|
22
|
-
".": {
|
|
23
|
-
"types": "./dist-types/index.d.ts",
|
|
24
|
-
"import": "./dist-mjs/index.mjs",
|
|
25
|
-
"default": "./dist-mjs/index.mjs"
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
"files": [
|
|
29
|
-
"src",
|
|
30
|
-
"dist-types",
|
|
31
|
-
"dist-mjs",
|
|
32
|
-
"!src/**/test/**",
|
|
33
|
-
"!src/**/*.test.ts",
|
|
34
|
-
"!src/**/*.spec.ts",
|
|
35
|
-
"!dist-types/**/test/**",
|
|
36
|
-
"!dist-types/**/*.test.d.ts",
|
|
37
|
-
"!dist-types/**/*.spec.d.ts",
|
|
38
|
-
"!dist-mjs/**/test/**",
|
|
39
|
-
"!dist-mjs/**/*.test.mjs",
|
|
40
|
-
"!dist-mjs/**/*.spec.mjs",
|
|
41
|
-
"SECURITY.md"
|
|
42
|
-
],
|
|
43
|
-
"scripts": {
|
|
44
|
-
"build:dist-mjs": "rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs",
|
|
45
|
-
"build:dist-types": "rimraf dist-types && npx builder --type=types --outDir=dist-types",
|
|
46
|
-
"ci:compile": "tsc --noEmit",
|
|
47
|
-
"ci:coverage": "NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true",
|
|
48
|
-
"ci:lint": "npm run lint",
|
|
49
|
-
"ci:style": "npm run prettier",
|
|
50
|
-
"ci:test": "NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false",
|
|
51
|
-
"lint": "eslint --max-warnings 0 .",
|
|
52
|
-
"lint:fix": "eslint --max-warnings 0 --fix .",
|
|
53
|
-
"prepare": "",
|
|
54
|
-
"prepublishOnly": "npm run build:dist-types && npm run build:dist-mjs",
|
|
55
|
-
"prettier": "prettier --ignore-path .gitignore --list-different .",
|
|
56
|
-
"prettier:fix": "prettier --ignore-path .gitignore --write .",
|
|
57
|
-
"test": "npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"
|
|
58
|
-
},
|
|
59
|
-
"prettier": "@checkdigit/prettier-config",
|
|
60
|
-
"jest": {
|
|
61
|
-
"preset": "@checkdigit/jest-config"
|
|
62
|
-
},
|
|
63
|
-
"dependencies": {
|
|
64
|
-
"@typescript-eslint/type-utils": "^8.15.0",
|
|
65
|
-
"@typescript-eslint/utils": "^8.15.0",
|
|
66
|
-
"ts-api-utils": "^1.4.0"
|
|
67
|
-
},
|
|
68
|
-
"devDependencies": {
|
|
69
|
-
"@checkdigit/jest-config": "^6.0.2",
|
|
70
|
-
"@checkdigit/prettier-config": "^5.5.1",
|
|
71
|
-
"@checkdigit/typescript-config": "^8.0.0",
|
|
72
|
-
"@eslint/js": "^9.15.0",
|
|
73
|
-
"@types/eslint": "^9.6.1",
|
|
74
|
-
"@types/eslint-config-prettier": "^6.11.3",
|
|
75
|
-
"@typescript-eslint/parser": "^8.15.0",
|
|
76
|
-
"@typescript-eslint/rule-tester": "^8.15.0",
|
|
77
|
-
"eslint": "^9.15.0",
|
|
78
|
-
"eslint-config-prettier": "^9.1.0",
|
|
79
|
-
"eslint-import-resolver-typescript": "^3.6.3",
|
|
80
|
-
"eslint-plugin-eslint-plugin": "^6.3.2",
|
|
81
|
-
"eslint-plugin-import": "^2.31.0",
|
|
82
|
-
"eslint-plugin-no-only-tests": "^3.3.0",
|
|
83
|
-
"eslint-plugin-no-secrets": "^1.1.2",
|
|
84
|
-
"eslint-plugin-node": "^11.1.0",
|
|
85
|
-
"eslint-plugin-sonarjs": "1.0.4",
|
|
86
|
-
"http-status-codes": "^2.3.0",
|
|
87
|
-
"rimraf": "^6.0.1",
|
|
88
|
-
"typescript-eslint": "^8.15.0"
|
|
89
|
-
},
|
|
90
|
-
"peerDependencies": {
|
|
91
|
-
"eslint": ">=9 <10"
|
|
92
|
-
},
|
|
93
|
-
"engines": {
|
|
94
|
-
"node": ">=20.17"
|
|
95
|
-
}
|
|
96
|
-
}
|
|
1
|
+
{"name":"@checkdigit/eslint-plugin","version":"7.5.0-PR.75-0252","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-mjs","!src/**/test/**","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/test/**","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-mjs/**/test/**","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 .","lint:fix":"eslint --max-warnings 0 --fix .","prepare":"","prepublishOnly":"npm run build:dist-types && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"dependencies":{"@typescript-eslint/type-utils":"8.15.0","@typescript-eslint/utils":"8.15.0","debug":"^4.3.7","ts-api-utils":"^1.4.0"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^5.5.1","@checkdigit/typescript-config":"^8.0.0","@eslint/js":"^9.15.0","@types/debug":"^4.1.12","@types/eslint":"^9.6.1","@types/eslint-config-prettier":"^6.11.3","@typescript-eslint/parser":"^8.15.0","@typescript-eslint/rule-tester":"^8.15.0","eslint":"9.15.0","eslint-config-prettier":"^9.1.0","eslint-import-resolver-typescript":"^3.6.3","eslint-plugin-eslint-plugin":"^6.3.2","eslint-plugin-import":"^2.31.0","eslint-plugin-no-only-tests":"^3.3.0","eslint-plugin-no-secrets":"^1.1.2","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"1.0.4","http-status-codes":"^2.3.0","rimraf":"^6.0.1","typescript-eslint":"^8.15.0"},"peerDependencies":{"eslint":">=9 <10"},"engines":{"node":">=20.17"}}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// agent/add-url-domain.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { strict as assert } from 'node:assert';
|
|
10
|
+
|
|
11
|
+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
12
|
+
|
|
13
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
14
|
+
|
|
15
|
+
export const ruleId = 'add-assert-import';
|
|
16
|
+
|
|
17
|
+
const ASSERT_IMPORT_STATEMENT = "import { strict as assert } from 'node:assert';";
|
|
18
|
+
|
|
19
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
20
|
+
|
|
21
|
+
const rule: ESLintUtils.RuleModule<'addAssertImport'> = createRule({
|
|
22
|
+
name: ruleId,
|
|
23
|
+
meta: {
|
|
24
|
+
type: 'suggestion',
|
|
25
|
+
docs: {
|
|
26
|
+
description: 'Add import of assert module of node.',
|
|
27
|
+
},
|
|
28
|
+
messages: {
|
|
29
|
+
addAssertImport: 'Add import of assert module of node.',
|
|
30
|
+
},
|
|
31
|
+
fixable: 'code',
|
|
32
|
+
schema: [],
|
|
33
|
+
},
|
|
34
|
+
defaultOptions: [],
|
|
35
|
+
create(context) {
|
|
36
|
+
let isAssertImported = false;
|
|
37
|
+
let isAssertUsed = false;
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
ImportDeclaration: (node) => {
|
|
41
|
+
if (node.source.value === 'assert' || node.source.value === 'node:assert') {
|
|
42
|
+
isAssertImported = true;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
CallExpression: (callExpression) => {
|
|
46
|
+
// detect if assert is used
|
|
47
|
+
if (
|
|
48
|
+
(callExpression.callee.type === AST_NODE_TYPES.Identifier && callExpression.callee.name === 'assert') ||
|
|
49
|
+
(callExpression.callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
50
|
+
callExpression.callee.object.type === AST_NODE_TYPES.Identifier &&
|
|
51
|
+
callExpression.callee.object.name === 'assert')
|
|
52
|
+
) {
|
|
53
|
+
isAssertUsed = true;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
'Program:exit': (program) => {
|
|
57
|
+
// add assert import if necessary
|
|
58
|
+
if (isAssertUsed && !isAssertImported) {
|
|
59
|
+
const firstStatement = program.body[0];
|
|
60
|
+
assert(firstStatement);
|
|
61
|
+
context.report({
|
|
62
|
+
node: program,
|
|
63
|
+
messageId: 'addAssertImport',
|
|
64
|
+
fix(fixer) {
|
|
65
|
+
return fixer.insertTextBefore(firstStatement, `${ASSERT_IMPORT_STATEMENT}\n`);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export default rule;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// agent/add-url-domain.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { strict as assert } from 'node:assert';
|
|
10
|
+
|
|
11
|
+
import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
12
|
+
|
|
13
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
14
|
+
import { getProjectRootFolder, getSwaggerPathByIndexFile, isApiIndexFile, loadPackageJson, loadSwagger } from './file';
|
|
15
|
+
|
|
16
|
+
export const ruleId = 'add-base-path-const';
|
|
17
|
+
|
|
18
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
19
|
+
|
|
20
|
+
const rule: ESLintUtils.RuleModule<'addBasePathConst'> = createRule({
|
|
21
|
+
name: ruleId,
|
|
22
|
+
meta: {
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
docs: {
|
|
25
|
+
description: 'Add BASE_PATH const variable.',
|
|
26
|
+
},
|
|
27
|
+
messages: {
|
|
28
|
+
addBasePathConst: 'Add BASE_PATH const variable.',
|
|
29
|
+
},
|
|
30
|
+
fixable: 'code',
|
|
31
|
+
schema: [],
|
|
32
|
+
},
|
|
33
|
+
defaultOptions: [],
|
|
34
|
+
create(context) {
|
|
35
|
+
const sourceCode = context.sourceCode;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
Program: (program: TSESTree.Program) => {
|
|
39
|
+
if (!isApiIndexFile(context.filename)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const scope = sourceCode.getScope(program).childScopes[0];
|
|
44
|
+
assert(scope);
|
|
45
|
+
|
|
46
|
+
const foundBasePathConst = scope.variables.find((variable) => variable.name === 'BASE_PATH');
|
|
47
|
+
if (foundBasePathConst) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const swaggerPath = getSwaggerPathByIndexFile(context.filename);
|
|
52
|
+
const swaggerFileContents = loadSwagger(swaggerPath);
|
|
53
|
+
const baseUrlLine = swaggerFileContents
|
|
54
|
+
.split('\n')
|
|
55
|
+
.find((line) => /^\s*-\s*url:\s*\/.*$/u.test(line) || /^basePath:.*/u.test(line));
|
|
56
|
+
const baseUrl = baseUrlLine?.split(':')[1]?.trim();
|
|
57
|
+
assert(baseUrl !== undefined);
|
|
58
|
+
|
|
59
|
+
const packageRoot = getProjectRootFolder(context.filename);
|
|
60
|
+
const packageJson = JSON.parse(loadPackageJson(packageRoot)) as { name: string };
|
|
61
|
+
const serviceName = packageJson.name.split('/')[1];
|
|
62
|
+
assert(serviceName !== undefined);
|
|
63
|
+
|
|
64
|
+
const domain = `https://${serviceName}.checkdigit${baseUrl}`;
|
|
65
|
+
|
|
66
|
+
const lastImportStatement = program.body.findLast((node) => node.type === AST_NODE_TYPES.ImportDeclaration);
|
|
67
|
+
assert(lastImportStatement);
|
|
68
|
+
|
|
69
|
+
context.report({
|
|
70
|
+
messageId: 'addBasePathConst',
|
|
71
|
+
node: program,
|
|
72
|
+
fix(fixer) {
|
|
73
|
+
return fixer.insertTextAfter(lastImportStatement, `\nexport const BASE_PATH = '${domain}';\n`);
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export default rule;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// agent/add-url-domain.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { strict as assert } from 'node:assert';
|
|
10
|
+
|
|
11
|
+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
12
|
+
|
|
13
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
14
|
+
import { getApiIndexPathByFilename } from './file';
|
|
15
|
+
|
|
16
|
+
export const ruleId = 'add-base-path-import';
|
|
17
|
+
|
|
18
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
19
|
+
|
|
20
|
+
const rule: ESLintUtils.RuleModule<'addBasePathImport'> = createRule({
|
|
21
|
+
name: ruleId,
|
|
22
|
+
meta: {
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
docs: {
|
|
25
|
+
description: 'Add import of BASE_PATH if it is used but not imported.',
|
|
26
|
+
},
|
|
27
|
+
messages: {
|
|
28
|
+
addBasePathImport: 'Add import of BASE_PATH.',
|
|
29
|
+
},
|
|
30
|
+
fixable: 'code',
|
|
31
|
+
schema: [],
|
|
32
|
+
},
|
|
33
|
+
defaultOptions: [],
|
|
34
|
+
create(context) {
|
|
35
|
+
const sourceCode = context.sourceCode;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
Program: (program) => {
|
|
39
|
+
const isBasePathUsed = sourceCode.text.includes(`$\{BASE_PATH}`);
|
|
40
|
+
if (isBasePathUsed) {
|
|
41
|
+
const topScope = sourceCode.getScope(program).childScopes[0];
|
|
42
|
+
assert(topScope);
|
|
43
|
+
if (topScope.variables.some((variable) => variable.name === 'BASE_PATH')) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const apiIndexPath = getApiIndexPathByFilename(context.filename);
|
|
48
|
+
if (apiIndexPath !== undefined) {
|
|
49
|
+
const lastImportStatement = program.body.findLast(
|
|
50
|
+
(statement) => statement.type === AST_NODE_TYPES.ImportDeclaration,
|
|
51
|
+
);
|
|
52
|
+
assert(lastImportStatement);
|
|
53
|
+
|
|
54
|
+
const basePathImportStatement = `\nimport { BASE_PATH } from '${apiIndexPath}';\n`;
|
|
55
|
+
context.report({
|
|
56
|
+
node: program,
|
|
57
|
+
messageId: 'addBasePathImport',
|
|
58
|
+
fix(fixer) {
|
|
59
|
+
return fixer.insertTextAfter(lastImportStatement, basePathImportStatement);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export default rule;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// agent/add-url-domain.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
+
|
|
11
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
12
|
+
import { addBasePathUrlDomain } from './url';
|
|
13
|
+
|
|
14
|
+
export const ruleId = 'add-url-domain';
|
|
15
|
+
|
|
16
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
17
|
+
|
|
18
|
+
const rule: ESLintUtils.RuleModule<'addDomain' | 'unknownError'> = createRule({
|
|
19
|
+
name: ruleId,
|
|
20
|
+
meta: {
|
|
21
|
+
type: 'suggestion',
|
|
22
|
+
docs: {
|
|
23
|
+
description: 'Add HTTP domain to the BASE_PATH like url constant variable.',
|
|
24
|
+
},
|
|
25
|
+
messages: {
|
|
26
|
+
addDomain: 'Add HTTP domain to the BASE_PATH like url constant variable.',
|
|
27
|
+
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
28
|
+
},
|
|
29
|
+
fixable: 'code',
|
|
30
|
+
schema: [],
|
|
31
|
+
},
|
|
32
|
+
defaultOptions: [],
|
|
33
|
+
create(context) {
|
|
34
|
+
const sourceCode = context.sourceCode;
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
'VariableDeclarator[id.name=/^([A-Z]+_)*BASE_PATH$/]': (basePathDeclarator: TSESTree.VariableDeclarator) => {
|
|
38
|
+
try {
|
|
39
|
+
if (
|
|
40
|
+
basePathDeclarator.init === null ||
|
|
41
|
+
(basePathDeclarator.init.type !== AST_NODE_TYPES.Literal &&
|
|
42
|
+
basePathDeclarator.init.type !== AST_NODE_TYPES.TemplateLiteral)
|
|
43
|
+
) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const urlText = sourceCode.getText(basePathDeclarator.init);
|
|
48
|
+
const replacement = addBasePathUrlDomain(urlText);
|
|
49
|
+
|
|
50
|
+
if (replacement !== urlText) {
|
|
51
|
+
context.report({
|
|
52
|
+
messageId: 'addDomain',
|
|
53
|
+
node: basePathDeclarator.init,
|
|
54
|
+
fix(fixer) {
|
|
55
|
+
return fixer.replaceText(basePathDeclarator.init as TSESTree.Node, replacement);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
// eslint-disable-next-line no-console
|
|
61
|
+
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
62
|
+
context.report({
|
|
63
|
+
node: basePathDeclarator,
|
|
64
|
+
messageId: 'unknownError',
|
|
65
|
+
data: {
|
|
66
|
+
fileName: context.filename,
|
|
67
|
+
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export default rule;
|