@futdevpro/dynamo-eslint 1.12.1 → 1.14.3
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/.github/workflows/main.yml +1 -2
- package/README.md +286 -17
- package/build/configs/base.d.ts +9 -80
- package/build/configs/base.d.ts.map +1 -1
- package/build/configs/base.js +89 -49
- package/build/configs/base.js.map +1 -1
- package/build/configs/fsm.d.ts +1 -81
- package/build/configs/fsm.d.ts.map +1 -1
- package/build/configs/fsm.js +7 -6
- package/build/configs/fsm.js.map +1 -1
- package/build/configs/ngx-package.d.ts +1 -80
- package/build/configs/ngx-package.d.ts.map +1 -1
- package/build/configs/ngx-package.js +7 -6
- package/build/configs/ngx-package.js.map +1 -1
- package/build/configs/ngx.d.ts +4 -80
- package/build/configs/ngx.d.ts.map +1 -1
- package/build/configs/ngx.js +71 -11
- package/build/configs/ngx.js.map +1 -1
- package/build/configs/nts-package.d.ts +1 -82
- package/build/configs/nts-package.d.ts.map +1 -1
- package/build/configs/nts-package.js +16 -10
- package/build/configs/nts-package.js.map +1 -1
- package/build/configs/nts.d.ts +1 -81
- package/build/configs/nts.d.ts.map +1 -1
- package/build/configs/nts.js +30 -11
- package/build/configs/nts.js.map +1 -1
- package/build/plugin/index.d.ts +4 -0
- package/build/plugin/index.d.ts.map +1 -1
- package/build/plugin/index.js +7 -1
- package/build/plugin/index.js.map +1 -1
- package/build/plugin/rules/import/import-order.d.ts +4 -0
- package/build/plugin/rules/import/import-order.d.ts.map +1 -0
- package/build/plugin/rules/{import-order.js → import/import-order.js} +16 -17
- package/build/plugin/rules/import/import-order.js.map +1 -0
- package/build/plugin/rules/import/import-order.spec.d.ts.map +1 -0
- package/build/plugin/rules/{import-order.spec.js → import/import-order.spec.js} +40 -40
- package/build/plugin/rules/import/import-order.spec.js.map +1 -0
- package/build/plugin/rules/import/no-import-type.d.ts +4 -0
- package/build/plugin/rules/import/no-import-type.d.ts.map +1 -0
- package/build/plugin/rules/import/no-import-type.js +35 -0
- package/build/plugin/rules/import/no-import-type.js.map +1 -0
- package/build/plugin/rules/import/no-import-type.spec.d.ts +2 -0
- package/build/plugin/rules/import/no-import-type.spec.d.ts.map +1 -0
- package/build/plugin/rules/import/no-import-type.spec.js +60 -0
- package/build/plugin/rules/import/no-import-type.spec.js.map +1 -0
- package/build/plugin/rules/import/no-js-import.d.ts +4 -0
- package/build/plugin/rules/import/no-js-import.d.ts.map +1 -0
- package/build/plugin/rules/import/no-js-import.js +33 -0
- package/build/plugin/rules/import/no-js-import.js.map +1 -0
- package/build/plugin/rules/import/no-js-import.spec.d.ts +2 -0
- package/build/plugin/rules/import/no-js-import.spec.d.ts.map +1 -0
- package/build/plugin/rules/import/no-js-import.spec.js +68 -0
- package/build/plugin/rules/import/no-js-import.spec.js.map +1 -0
- package/build/plugin/rules/naming-patterns.d.ts +1 -1
- package/build/plugin/rules/naming-patterns.d.ts.map +1 -1
- package/build/plugin/rules/naming-patterns.spec.js +1 -1
- package/build/plugin/rules/naming-patterns.spec.js.map +1 -1
- package/build/scripts/dynamo-fix.d.ts +3 -0
- package/build/scripts/dynamo-fix.d.ts.map +1 -0
- package/build/scripts/dynamo-fix.js +92 -0
- package/build/scripts/dynamo-fix.js.map +1 -0
- package/build/scripts/eslintrc-audit.js.map +1 -1
- package/build/scripts/fix-return-types.d.ts +3 -0
- package/build/scripts/fix-return-types.d.ts.map +1 -0
- package/build/scripts/fix-return-types.js +109 -0
- package/build/scripts/fix-return-types.js.map +1 -0
- package/build/scripts/validate-imports.js +10 -9
- package/build/scripts/validate-imports.js.map +1 -1
- package/build/scripts/validate-naming.js +11 -26
- package/build/scripts/validate-naming.js.map +1 -1
- package/build-test/plugin/rules/import-order.d.ts +1 -1
- package/build-test/plugin/rules/naming-patterns.d.ts +1 -1
- package/eslint.config.js +55 -0
- package/futdevpro-dynamo-eslint-01.14.3.tgz +0 -0
- package/package.json +27 -18
- package/samples/.vscode/settings.json +13 -0
- package/samples/base/eslint.config.js +3 -0
- package/samples/fsm/.eslintrc.json +4 -0
- package/samples/fsm/eslint.config.js +3 -0
- package/samples/ngx/eslint.config.js +3 -0
- package/samples/ngx-package/.eslintrc.json +4 -0
- package/samples/ngx-package/eslint.config.js +3 -0
- package/samples/nts/eslint.config.js +3 -0
- package/samples/nts-package/.eslintrc.json +4 -0
- package/samples/nts-package/eslint.config.js +3 -0
- package/samples/package.json.example +26 -0
- package/samples/poc-violations.ts +32 -3
- package/src/configs/base.ts +93 -48
- package/src/configs/fsm.ts +8 -7
- package/src/configs/ngx-package.ts +8 -7
- package/src/configs/ngx.ts +71 -11
- package/src/configs/nts-package.ts +16 -10
- package/src/configs/nts.ts +30 -11
- package/src/plugin/index.ts +9 -1
- package/src/plugin/rules/{import-order.spec.ts → import/import-order.spec.ts} +46 -40
- package/src/plugin/rules/{import-order.ts → import/import-order.ts} +20 -18
- package/src/plugin/rules/import/no-import-type.spec.ts +69 -0
- package/src/plugin/rules/import/no-import-type.ts +37 -0
- package/src/plugin/rules/import/no-js-import.spec.ts +82 -0
- package/src/plugin/rules/import/no-js-import.ts +35 -0
- package/src/plugin/rules/naming-patterns.spec.ts +3 -1
- package/src/plugin/rules/naming-patterns.ts +1 -1
- package/src/scripts/dynamo-fix.ts +108 -0
- package/src/scripts/eslintrc-audit.ts +6 -2
- package/src/scripts/fix-return-types.ts +148 -0
- package/src/scripts/validate-imports.ts +95 -13
- package/src/scripts/validate-naming.ts +16 -28
- package/.eslintrc.json +0 -16
- package/INTEGRATION.md +0 -74
- package/POC-README.md +0 -147
- package/build/plugin/rules/import-order.d.ts +0 -4
- package/build/plugin/rules/import-order.d.ts.map +0 -1
- package/build/plugin/rules/import-order.js.map +0 -1
- package/build/plugin/rules/import-order.spec.d.ts.map +0 -1
- package/build/plugin/rules/import-order.spec.js.map +0 -1
- package/futdevpro-dynamo-eslint-01.12.01.tgz +0 -0
- /package/build/plugin/rules/{import-order.spec.d.ts → import/import-order.spec.d.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Rule } from 'eslint';
|
|
2
2
|
|
|
3
3
|
interface ImportGroup {
|
|
4
4
|
type: 'non-futdevpro' | 'futdevpro' | 'other-modules' | 'same-module';
|
|
@@ -13,16 +13,14 @@ const rule: Rule.RuleModule = {
|
|
|
13
13
|
messages: {
|
|
14
14
|
misordered: 'Import statements should be grouped and ordered by convention.',
|
|
15
15
|
forbiddenNpmPackages: 'Import from "*/../NPM-packages/*" is forbidden. Use "@futdevpro/*" instead.',
|
|
16
|
-
forbiddenJsExtension: 'Import with ".js" extension is forbidden. Use ".ts" or no extension.',
|
|
17
|
-
forbiddenImportType: 'Use of "import type" is forbidden.',
|
|
18
16
|
missingEmptyLine: 'Missing empty line between import groups.',
|
|
19
17
|
extraEmptyLine: 'Extra empty line detected.',
|
|
20
18
|
},
|
|
21
19
|
fixable: 'code',
|
|
22
20
|
},
|
|
23
21
|
create(context) {
|
|
24
|
-
const sourceCode = context.
|
|
25
|
-
const filename = context.
|
|
22
|
+
const sourceCode = context.sourceCode;
|
|
23
|
+
const filename = context.filename;
|
|
26
24
|
|
|
27
25
|
function getImportGroup(importNode: any): ImportGroup['type'] {
|
|
28
26
|
const source = importNode.source.value as string;
|
|
@@ -67,23 +65,16 @@ const rule: Rule.RuleModule = {
|
|
|
67
65
|
context.report({
|
|
68
66
|
node: importNode.source,
|
|
69
67
|
messageId: 'forbiddenNpmPackages',
|
|
68
|
+
fix(fixer) {
|
|
69
|
+
// Replace NPM-packages path with @futdevpro equivalent
|
|
70
|
+
const newSource = source.replace(/.*\/NPM-packages\/([^\/]+)/, '@futdevpro/$1');
|
|
71
|
+
|
|
72
|
+
return fixer.replaceText(importNode.source, `'${newSource}'`);
|
|
73
|
+
},
|
|
70
74
|
});
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
if (source.endsWith('.js')) {
|
|
74
|
-
context.report({
|
|
75
|
-
node: importNode.source,
|
|
76
|
-
messageId: 'forbiddenJsExtension',
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
77
|
|
|
80
|
-
// Check for import type usage
|
|
81
|
-
if (importNode.importKind === 'type') {
|
|
82
|
-
context.report({
|
|
83
|
-
node: importNode,
|
|
84
|
-
messageId: 'forbiddenImportType',
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
78
|
}
|
|
88
79
|
|
|
89
80
|
function validateImportOrdering(importNodes: any[]) {
|
|
@@ -100,6 +91,7 @@ const rule: Rule.RuleModule = {
|
|
|
100
91
|
importNodes.forEach(importNode => {
|
|
101
92
|
const groupType = getImportGroup(importNode);
|
|
102
93
|
const group = groups.find(g => g.type === groupType);
|
|
94
|
+
|
|
103
95
|
if (group) {
|
|
104
96
|
group.imports.push(importNode);
|
|
105
97
|
}
|
|
@@ -110,6 +102,7 @@ const rule: Rule.RuleModule = {
|
|
|
110
102
|
|
|
111
103
|
// Check if imports are in correct order
|
|
112
104
|
let currentGroupIndex = 0;
|
|
105
|
+
|
|
113
106
|
for (let i = 0; i < importNodes.length; i++) {
|
|
114
107
|
const importNode = importNodes[i];
|
|
115
108
|
const expectedGroupType = getImportGroup(importNode);
|
|
@@ -120,6 +113,11 @@ const rule: Rule.RuleModule = {
|
|
|
120
113
|
context.report({
|
|
121
114
|
node: importNode,
|
|
122
115
|
messageId: 'misordered',
|
|
116
|
+
fix(fixer) {
|
|
117
|
+
// This is complex to fix automatically, so we'll just report for now
|
|
118
|
+
// Future enhancement: reorder imports automatically
|
|
119
|
+
return null;
|
|
120
|
+
},
|
|
123
121
|
});
|
|
124
122
|
}
|
|
125
123
|
|
|
@@ -141,6 +139,10 @@ const rule: Rule.RuleModule = {
|
|
|
141
139
|
context.report({
|
|
142
140
|
node: firstImportOfNextGroup,
|
|
143
141
|
messageId: 'missingEmptyLine',
|
|
142
|
+
fix(fixer) {
|
|
143
|
+
// Add empty line before the first import of next group
|
|
144
|
+
return fixer.insertTextBefore(firstImportOfNextGroup, '\n');
|
|
145
|
+
},
|
|
144
146
|
});
|
|
145
147
|
}
|
|
146
148
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import noImportTypeRule from './no-import-type';
|
|
2
|
+
|
|
3
|
+
describe('| no-import-type', () => {
|
|
4
|
+
it('| should be a valid ESLint rule', () => {
|
|
5
|
+
expect(noImportTypeRule.meta?.type).toBe('suggestion');
|
|
6
|
+
expect(noImportTypeRule.meta?.docs?.description).toContain('import type');
|
|
7
|
+
expect(noImportTypeRule.meta?.fixable).toBe('code');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('| should have create function that returns object with ImportDeclaration method', () => {
|
|
11
|
+
const mockContext = {
|
|
12
|
+
sourceCode: {
|
|
13
|
+
getText: (node: any) => 'import type { SomeType } from \'./some-file\';',
|
|
14
|
+
},
|
|
15
|
+
report: () => {},
|
|
16
|
+
} as any;
|
|
17
|
+
|
|
18
|
+
const result = noImportTypeRule.create(mockContext);
|
|
19
|
+
|
|
20
|
+
expect(typeof result).toBe('object');
|
|
21
|
+
expect(typeof result.ImportDeclaration).toBe('function');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('| should detect forbidden import type usage', () => {
|
|
25
|
+
const mockContext = {
|
|
26
|
+
sourceCode: {
|
|
27
|
+
getText: (node: any) => 'import type { SomeType } from \'./some-file\';',
|
|
28
|
+
},
|
|
29
|
+
report: (options: any) => {
|
|
30
|
+
expect(options.messageId).toBe('forbiddenImportType');
|
|
31
|
+
},
|
|
32
|
+
} as any;
|
|
33
|
+
|
|
34
|
+
const mockNode = {
|
|
35
|
+
type: 'ImportDeclaration',
|
|
36
|
+
importKind: 'type',
|
|
37
|
+
loc: { start: { line: 1 }, end: { line: 1 } },
|
|
38
|
+
} as any;
|
|
39
|
+
|
|
40
|
+
const rule = noImportTypeRule.create(mockContext);
|
|
41
|
+
|
|
42
|
+
rule.ImportDeclaration(mockNode);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('| should not report errors for regular imports', () => {
|
|
46
|
+
const mockContext = {
|
|
47
|
+
sourceCode: {
|
|
48
|
+
getText: (node: any) => 'import { SomeType } from \'./some-file\';',
|
|
49
|
+
},
|
|
50
|
+
report: (options: any) => {
|
|
51
|
+
// Should not be called for regular imports
|
|
52
|
+
fail('Should not report errors for regular imports');
|
|
53
|
+
},
|
|
54
|
+
} as any;
|
|
55
|
+
|
|
56
|
+
const mockNode = {
|
|
57
|
+
type: 'ImportDeclaration',
|
|
58
|
+
importKind: 'value',
|
|
59
|
+
loc: { start: { line: 1 }, end: { line: 1 } },
|
|
60
|
+
} as any;
|
|
61
|
+
|
|
62
|
+
const rule = noImportTypeRule.create(mockContext);
|
|
63
|
+
|
|
64
|
+
rule.ImportDeclaration(mockNode);
|
|
65
|
+
|
|
66
|
+
// If we get here without the report function being called, the test passes
|
|
67
|
+
expect(true).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Rule } from 'eslint';
|
|
2
|
+
|
|
3
|
+
const rule: Rule.RuleModule = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
docs: { description: 'Forbid use of "import type" syntax' },
|
|
7
|
+
schema: [],
|
|
8
|
+
messages: {
|
|
9
|
+
forbiddenImportType: 'Use of "import type" is forbidden.',
|
|
10
|
+
},
|
|
11
|
+
fixable: 'code',
|
|
12
|
+
},
|
|
13
|
+
create(context) {
|
|
14
|
+
const sourceCode = context.sourceCode;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
ImportDeclaration(node: any) {
|
|
18
|
+
// Check for import type usage
|
|
19
|
+
if (node.importKind === 'type') {
|
|
20
|
+
context.report({
|
|
21
|
+
node,
|
|
22
|
+
messageId: 'forbiddenImportType',
|
|
23
|
+
fix(fixer) {
|
|
24
|
+
// Remove 'type' keyword
|
|
25
|
+
const importText = sourceCode.getText(node);
|
|
26
|
+
const newImportText = importText.replace(/import\s+type\s+/, 'import ');
|
|
27
|
+
|
|
28
|
+
return fixer.replaceText(node, newImportText);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default rule;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import noJsExtensionRule from './no-js-import';
|
|
2
|
+
|
|
3
|
+
describe('| no-js-import', () => {
|
|
4
|
+
it('| should be a valid ESLint rule', () => {
|
|
5
|
+
expect(noJsExtensionRule.meta?.type).toBe('suggestion');
|
|
6
|
+
expect(noJsExtensionRule.meta?.docs?.description).toContain('.js');
|
|
7
|
+
expect(noJsExtensionRule.meta?.fixable).toBe('code');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('| should have create function that returns object with ImportDeclaration method', () => {
|
|
11
|
+
const mockContext = {
|
|
12
|
+
report: () => {},
|
|
13
|
+
} as any;
|
|
14
|
+
|
|
15
|
+
const result = noJsExtensionRule.create(mockContext);
|
|
16
|
+
|
|
17
|
+
expect(typeof result).toBe('object');
|
|
18
|
+
expect(typeof result.ImportDeclaration).toBe('function');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('| should detect forbidden .js extension imports', () => {
|
|
22
|
+
const mockContext = {
|
|
23
|
+
report: (options: any) => {
|
|
24
|
+
expect(options.messageId).toBe('forbiddenJsExtension');
|
|
25
|
+
},
|
|
26
|
+
} as any;
|
|
27
|
+
|
|
28
|
+
const mockNode = {
|
|
29
|
+
type: 'ImportDeclaration',
|
|
30
|
+
source: { value: './some-file.js' },
|
|
31
|
+
loc: { start: { line: 1 }, end: { line: 1 } },
|
|
32
|
+
} as any;
|
|
33
|
+
|
|
34
|
+
const rule = noJsExtensionRule.create(mockContext);
|
|
35
|
+
|
|
36
|
+
rule.ImportDeclaration(mockNode);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('| should not report errors for imports without .js extension', () => {
|
|
40
|
+
const mockContext = {
|
|
41
|
+
report: (options: any) => {
|
|
42
|
+
// Should not be called for imports without .js extension
|
|
43
|
+
fail('Should not report errors for imports without .js extension');
|
|
44
|
+
},
|
|
45
|
+
} as any;
|
|
46
|
+
|
|
47
|
+
const mockNode = {
|
|
48
|
+
type: 'ImportDeclaration',
|
|
49
|
+
source: { value: './some-file' },
|
|
50
|
+
loc: { start: { line: 1 }, end: { line: 1 } },
|
|
51
|
+
} as any;
|
|
52
|
+
|
|
53
|
+
const rule = noJsExtensionRule.create(mockContext);
|
|
54
|
+
|
|
55
|
+
rule.ImportDeclaration(mockNode);
|
|
56
|
+
|
|
57
|
+
// If we get here without the report function being called, the test passes
|
|
58
|
+
expect(true).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('| should not report errors for imports with .ts extension', () => {
|
|
62
|
+
const mockContext = {
|
|
63
|
+
report: (options: any) => {
|
|
64
|
+
// Should not be called for imports with .ts extension
|
|
65
|
+
fail('Should not report errors for imports with .ts extension');
|
|
66
|
+
},
|
|
67
|
+
} as any;
|
|
68
|
+
|
|
69
|
+
const mockNode = {
|
|
70
|
+
type: 'ImportDeclaration',
|
|
71
|
+
source: { value: './some-file.ts' },
|
|
72
|
+
loc: { start: { line: 1 }, end: { line: 1 } },
|
|
73
|
+
} as any;
|
|
74
|
+
|
|
75
|
+
const rule = noJsExtensionRule.create(mockContext);
|
|
76
|
+
|
|
77
|
+
rule.ImportDeclaration(mockNode);
|
|
78
|
+
|
|
79
|
+
// If we get here without the report function being called, the test passes
|
|
80
|
+
expect(true).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Rule } from 'eslint';
|
|
2
|
+
|
|
3
|
+
const rule: Rule.RuleModule = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
docs: { description: 'Forbid imports with ".js" extension' },
|
|
7
|
+
schema: [],
|
|
8
|
+
messages: {
|
|
9
|
+
forbiddenJsExtension: 'Import with ".js" extension is forbidden. Use ".ts" or no extension.',
|
|
10
|
+
},
|
|
11
|
+
fixable: 'code',
|
|
12
|
+
},
|
|
13
|
+
create(context) {
|
|
14
|
+
return {
|
|
15
|
+
ImportDeclaration(node: any) {
|
|
16
|
+
const source = node.source.value as string;
|
|
17
|
+
|
|
18
|
+
if (source.endsWith('.js')) {
|
|
19
|
+
context.report({
|
|
20
|
+
node: node.source,
|
|
21
|
+
messageId: 'forbiddenJsExtension',
|
|
22
|
+
fix(fixer) {
|
|
23
|
+
// Remove .js extension
|
|
24
|
+
const newSource = source.replace(/\.js$/, '');
|
|
25
|
+
|
|
26
|
+
return fixer.replaceText(node.source, `'${newSource}'`);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default rule;
|
|
@@ -12,6 +12,7 @@ describe('| naming-patterns', () => {
|
|
|
12
12
|
} as any;
|
|
13
13
|
|
|
14
14
|
const result = namingPatternsRule.create(mockContext);
|
|
15
|
+
|
|
15
16
|
expect(typeof result).toBe('object');
|
|
16
17
|
expect(typeof result.Identifier).toBe('function');
|
|
17
18
|
});
|
|
@@ -27,10 +28,11 @@ describe('| naming-patterns', () => {
|
|
|
27
28
|
const mockNode = {
|
|
28
29
|
type: 'Identifier' as const,
|
|
29
30
|
name: 'validVariableName',
|
|
30
|
-
loc: { start: { line: 1 }, end: { line: 1 } }
|
|
31
|
+
loc: { start: { line: 1 }, end: { line: 1 } },
|
|
31
32
|
} as any;
|
|
32
33
|
|
|
33
34
|
const rule = namingPatternsRule.create(mockContext);
|
|
35
|
+
|
|
34
36
|
rule.Identifier(mockNode);
|
|
35
37
|
|
|
36
38
|
// If we get here without the report function being called, the test passes
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fg from 'fast-glob';
|
|
3
|
+
import { ESLint } from 'eslint';
|
|
4
|
+
import { writeFileSync } from 'fs';
|
|
5
|
+
|
|
6
|
+
interface FixResult {
|
|
7
|
+
file: string;
|
|
8
|
+
fixed: boolean;
|
|
9
|
+
errors: number;
|
|
10
|
+
warnings: number;
|
|
11
|
+
fixes: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function fixFile(filePath: string): Promise<FixResult> {
|
|
15
|
+
const eslint = new ESLint({
|
|
16
|
+
baseConfig: [
|
|
17
|
+
{ ignores: [ 'build/**', 'dist/**', 'node_modules/**' ] },
|
|
18
|
+
{
|
|
19
|
+
files: [ '**/*.{ts,tsx}' ],
|
|
20
|
+
languageOptions: {
|
|
21
|
+
parser: require('@typescript-eslint/parser'),
|
|
22
|
+
parserOptions: { ecmaVersion: 2020, sourceType: 'module' },
|
|
23
|
+
},
|
|
24
|
+
plugins: {
|
|
25
|
+
'@futdevpro/dynamo': {
|
|
26
|
+
rules: {
|
|
27
|
+
'import-order': require('../plugin/rules/import-order').default,
|
|
28
|
+
'naming-patterns': require('../plugin/rules/naming-patterns').default,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
rules: {
|
|
33
|
+
'@futdevpro/dynamo/import-order': 'error',
|
|
34
|
+
'@futdevpro/dynamo/naming-patterns': 'warn',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
fix: true, // Enable auto-fixing
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const results = await eslint.lintFiles([ filePath ]);
|
|
42
|
+
const result = results[0];
|
|
43
|
+
|
|
44
|
+
if (result && result.output) {
|
|
45
|
+
// Write the fixed content back to the file
|
|
46
|
+
writeFileSync(filePath, result.output);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
file: filePath,
|
|
51
|
+
fixed: result && result.output !== undefined,
|
|
52
|
+
errors: result.messages.filter(msg => msg.severity === 2).length,
|
|
53
|
+
warnings: result.messages.filter(msg => msg.severity === 1).length,
|
|
54
|
+
fixes: result.fixableErrorCount + result.fixableWarningCount,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function main() {
|
|
59
|
+
const files = await fg([ '**/*.{ts,tsx}' ], {
|
|
60
|
+
ignore: [ '**/node_modules/**', '**/build/**', '**/dist/**', '**/*.spec.ts', '**/*.test.ts' ],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log(`[dynamo-fix] Fixing ${files.length} files...`);
|
|
64
|
+
|
|
65
|
+
const results: FixResult[] = [];
|
|
66
|
+
let totalFixed = 0;
|
|
67
|
+
let totalErrors = 0;
|
|
68
|
+
let totalWarnings = 0;
|
|
69
|
+
let totalFixes = 0;
|
|
70
|
+
|
|
71
|
+
for (const file of files) {
|
|
72
|
+
try {
|
|
73
|
+
const result = await fixFile(file);
|
|
74
|
+
|
|
75
|
+
results.push(result);
|
|
76
|
+
|
|
77
|
+
if (result.fixed) {
|
|
78
|
+
totalFixed++;
|
|
79
|
+
console.log(`✅ Fixed: ${result.file}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
totalErrors += result.errors;
|
|
83
|
+
totalWarnings += result.warnings;
|
|
84
|
+
totalFixes += result.fixes;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error(`❌ Error processing ${file}:`, error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Report results
|
|
91
|
+
console.log(`\n📊 Fix Summary:`);
|
|
92
|
+
console.log(` Files processed: ${files.length}`);
|
|
93
|
+
console.log(` Files fixed: ${totalFixed}`);
|
|
94
|
+
console.log(` Total errors: ${totalErrors}`);
|
|
95
|
+
console.log(` Total warnings: ${totalWarnings}`);
|
|
96
|
+
console.log(` Total fixes applied: ${totalFixes}`);
|
|
97
|
+
|
|
98
|
+
if (totalFixed === 0) {
|
|
99
|
+
console.log('✅ No files needed fixing!');
|
|
100
|
+
} else {
|
|
101
|
+
console.log(`\n🎉 Successfully fixed ${totalFixed} files!`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
main().catch((err) => {
|
|
106
|
+
console.error('Fix failed:', err);
|
|
107
|
+
process.exit(1);
|
|
108
|
+
});
|
|
@@ -7,6 +7,7 @@ type AnyJson = any;
|
|
|
7
7
|
function safeParse(jsonPath: string): AnyJson | null {
|
|
8
8
|
try {
|
|
9
9
|
const content = readFileSync(jsonPath, 'utf8');
|
|
10
|
+
|
|
10
11
|
return JSON.parse(content);
|
|
11
12
|
} catch {
|
|
12
13
|
return null;
|
|
@@ -14,21 +15,24 @@ function safeParse(jsonPath: string): AnyJson | null {
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
async function main() {
|
|
17
|
-
const files = await fg(['**/.eslintrc.json'], { ignore: ['**/node_modules/**'] });
|
|
18
|
+
const files = await fg([ '**/.eslintrc.json' ], { ignore: [ '**/node_modules/**' ] });
|
|
18
19
|
const ruleCounts: Record<string, number> = {};
|
|
19
20
|
const total = files.length;
|
|
20
21
|
|
|
21
22
|
for (const p of files) {
|
|
22
23
|
const cfg = safeParse(p);
|
|
24
|
+
|
|
23
25
|
if (!cfg?.rules) continue;
|
|
26
|
+
|
|
24
27
|
for (const key of Object.keys(cfg.rules)) {
|
|
25
28
|
ruleCounts[key] = (ruleCounts[key] ?? 0) + 1;
|
|
26
29
|
}
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
const entries = Object.entries(ruleCounts).sort((a, b) => b[1] - a[1]);
|
|
33
|
+
|
|
30
34
|
console.log(`[dynamo-eslintrc-audit] analyzed ${total} files`);
|
|
31
|
-
console.table(entries.slice(0, 50).map(([rule, count]) => ({ rule, count })));
|
|
35
|
+
console.table(entries.slice(0, 50).map(([ rule, count ]) => ({ rule, count })));
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
main().catch((err) => {
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as ts from 'typescript';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import fg from 'fast-glob';
|
|
6
|
+
import { DyFM_Log } from '@futdevpro/fsm-dynamo';
|
|
7
|
+
|
|
8
|
+
interface FixResult {
|
|
9
|
+
file: string;
|
|
10
|
+
fixed: boolean;
|
|
11
|
+
changes: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function addReturnTypesToFile(filePath: string): FixResult {
|
|
15
|
+
const sourceCode = fs.readFileSync(filePath, 'utf8');
|
|
16
|
+
const sourceFile = ts.createSourceFile(filePath, sourceCode, ts.ScriptTarget.Latest, true);
|
|
17
|
+
|
|
18
|
+
const program = ts.createProgram([filePath], {
|
|
19
|
+
target: ts.ScriptTarget.Latest,
|
|
20
|
+
module: ts.ModuleKind.ESNext,
|
|
21
|
+
strict: true,
|
|
22
|
+
skipLibCheck: true,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const checker = program.getTypeChecker();
|
|
26
|
+
let changes = 0;
|
|
27
|
+
let hasChanges = false;
|
|
28
|
+
|
|
29
|
+
function visit(node: ts.Node): ts.Node {
|
|
30
|
+
// Handle function declarations
|
|
31
|
+
if (ts.isFunctionDeclaration(node) && !node.type) {
|
|
32
|
+
const signature = checker.getSignatureFromDeclaration(node);
|
|
33
|
+
|
|
34
|
+
if (signature) {
|
|
35
|
+
const returnType = checker.getReturnTypeOfSignature(signature);
|
|
36
|
+
const typeString = checker.typeToString(returnType);
|
|
37
|
+
|
|
38
|
+
if (typeString !== 'void' && typeString !== 'any') {
|
|
39
|
+
const newType = ts.factory.createTypeReferenceNode(typeString);
|
|
40
|
+
const updatedNode = ts.factory.updateFunctionDeclaration(
|
|
41
|
+
node,
|
|
42
|
+
node.modifiers,
|
|
43
|
+
node.asteriskToken,
|
|
44
|
+
node.name,
|
|
45
|
+
node.typeParameters,
|
|
46
|
+
node.parameters,
|
|
47
|
+
newType,
|
|
48
|
+
node.body
|
|
49
|
+
);
|
|
50
|
+
changes++;
|
|
51
|
+
hasChanges = true;
|
|
52
|
+
return ts.visitEachChild(updatedNode, visit, {} as ts.TransformationContext);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Handle arrow functions
|
|
58
|
+
if (ts.isArrowFunction(node) && !node.type) {
|
|
59
|
+
const signature = checker.getSignatureFromDeclaration(node);
|
|
60
|
+
|
|
61
|
+
if (signature) {
|
|
62
|
+
const returnType = checker.getReturnTypeOfSignature(signature);
|
|
63
|
+
const typeString = checker.typeToString(returnType);
|
|
64
|
+
|
|
65
|
+
if (typeString !== 'void' && typeString !== 'any') {
|
|
66
|
+
const newType = ts.factory.createTypeReferenceNode(typeString);
|
|
67
|
+
const updatedNode = ts.factory.updateArrowFunction(
|
|
68
|
+
node,
|
|
69
|
+
node.modifiers,
|
|
70
|
+
node.typeParameters,
|
|
71
|
+
node.parameters,
|
|
72
|
+
newType,
|
|
73
|
+
node.equalsGreaterThanToken,
|
|
74
|
+
node.body
|
|
75
|
+
);
|
|
76
|
+
changes++;
|
|
77
|
+
hasChanges = true;
|
|
78
|
+
return ts.visitEachChild(updatedNode, visit, {} as ts.TransformationContext);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return ts.visitEachChild(node, visit, {} as ts.TransformationContext);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const result = ts.visitEachChild(sourceFile, visit, {} as ts.TransformationContext);
|
|
87
|
+
|
|
88
|
+
if (hasChanges) {
|
|
89
|
+
const printer = ts.createPrinter();
|
|
90
|
+
const newSourceCode = printer.printFile(result);
|
|
91
|
+
fs.writeFileSync(filePath, newSourceCode);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
file: filePath,
|
|
96
|
+
fixed: hasChanges,
|
|
97
|
+
changes,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function main() {
|
|
102
|
+
const files = await fg([ '**/*.{ts,tsx}' ], {
|
|
103
|
+
ignore: [
|
|
104
|
+
'**/node_modules/**',
|
|
105
|
+
'**/build/**',
|
|
106
|
+
'**/dist/**',
|
|
107
|
+
'**/*.spec.ts',
|
|
108
|
+
'**/*.test.ts',
|
|
109
|
+
],
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
DyFM_Log.log(`[fix-return-types] Processing ${files.length} files...`);
|
|
113
|
+
|
|
114
|
+
const results: FixResult[] = [];
|
|
115
|
+
let totalFixed = 0;
|
|
116
|
+
let totalChanges = 0;
|
|
117
|
+
|
|
118
|
+
for (const file of files) {
|
|
119
|
+
try {
|
|
120
|
+
const result = addReturnTypesToFile(file);
|
|
121
|
+
results.push(result);
|
|
122
|
+
|
|
123
|
+
if (result.fixed) {
|
|
124
|
+
totalFixed++;
|
|
125
|
+
totalChanges += result.changes;
|
|
126
|
+
DyFM_Log.log(`✅ Fixed ${result.changes} return types in ${file}`);
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
DyFM_Log.error(`❌ Error processing ${file}:`, error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
DyFM_Log.log(`\n📊 Fix Summary:`);
|
|
134
|
+
DyFM_Log.log(` Files processed: ${files.length}`);
|
|
135
|
+
DyFM_Log.log(` Files fixed: ${totalFixed}`);
|
|
136
|
+
DyFM_Log.log(` Total return types added: ${totalChanges}`);
|
|
137
|
+
|
|
138
|
+
if (totalFixed > 0) {
|
|
139
|
+
DyFM_Log.log(`🎉 Successfully added return types to ${totalFixed} files!`);
|
|
140
|
+
} else {
|
|
141
|
+
DyFM_Log.log('✅ No files needed return type fixes!');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
main().catch((err) => {
|
|
146
|
+
DyFM_Log.error('Fix script failed:', err);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
});
|