Skip to content

Commit d114be0

Browse files
angrychowHoblovski
authored andcommitted
add eslint rules for ts parser
1 parent 36628b6 commit d114be0

12 files changed

Lines changed: 151 additions & 71 deletions

ts-parser/.eslintrc.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["@typescript-eslint"],
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:@typescript-eslint/recommended",
8+
"prettier"
9+
],
10+
"parserOptions": {
11+
"ecmaVersion": 2022,
12+
"sourceType": "module"
13+
},
14+
"rules": {
15+
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
16+
"@typescript-eslint/no-explicit-any": "warn",
17+
"prefer-const": "error",
18+
"no-var": "error",
19+
"object-shorthand": "error",
20+
"prefer-arrow-callback": "error",
21+
"@typescript-eslint/ban-types": "off"
22+
},
23+
"env": {
24+
"node": true,
25+
"es2022": true
26+
},
27+
"ignorePatterns": [
28+
"dist/",
29+
"node_modules/",
30+
"**/*.js",
31+
"**/*.d.ts",
32+
"coverage/"
33+
]
34+
}

ts-parser/.prettierignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Build outputs
5+
dist/
6+
build/
7+
8+
# Test coverage
9+
coverage/
10+
11+
# Logs
12+
*.log
13+
14+
# Environment files
15+
.env*
16+
17+
# Package files
18+
package-lock.json
19+
yarn.lock
20+
21+
# Generated files
22+
*.d.ts
23+
*.js.map
24+
*.ts.map
25+
26+
# Temporary files
27+
*.tmp
28+
*.temp
29+
.DS_Store
30+
31+
# IDE
32+
.vscode/
33+
.idea/

ts-parser/.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "es5",
4+
"singleQuote": true,
5+
"printWidth": 100,
6+
"tabWidth": 2,
7+
"useTabs": false,
8+
"bracketSpacing": true,
9+
"arrowParens": "avoid"
10+
}

ts-parser/src/parser/FunctionParser.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class FunctionParser {
5858
// Parse method declarations in classes
5959
const classes = sourceFile.getClasses();
6060
for (const cls of classes) {
61-
let sym = cls.getSymbol();
61+
const sym = cls.getSymbol();
6262
let className = ""
6363
if (sym) {
6464
className = assignSymbolName(sym)
@@ -104,7 +104,7 @@ export class FunctionParser {
104104
for (const varDecl of variableDeclarations) {
105105
const initializer = varDecl.getInitializer();
106106
if (initializer && (Node.isArrowFunction(initializer) || Node.isFunctionExpression(initializer))) {
107-
let sym = varDecl.getSymbol()
107+
const sym = varDecl.getSymbol()
108108
let funcName = ""
109109
if (sym) {
110110
funcName = assignSymbolName(sym)
@@ -140,7 +140,7 @@ export class FunctionParser {
140140
}
141141

142142
private parseFunction(func: FunctionDeclaration, moduleName: string, packagePath: string, sourceFile: SourceFile): UniFunction {
143-
let symbol = func.getSymbol();
143+
const symbol = func.getSymbol();
144144
let name = 'anonymous_' + func.getStart();
145145
if (symbol) {
146146
name = assignSymbolName(symbol)
@@ -189,7 +189,7 @@ export class FunctionParser {
189189
}
190190

191191
private parseMethod(method: MethodDeclaration, moduleName: string, packagePath: string, sourceFile: SourceFile, className: string): UniFunction {
192-
let symbol = method.getSymbol();
192+
const symbol = method.getSymbol();
193193
let methodName = ""
194194
if (symbol) {
195195
methodName = assignSymbolName(symbol)
@@ -254,7 +254,7 @@ export class FunctionParser {
254254
}
255255

256256
private parseInterfaceMethod(method: MethodSignature, moduleName: string, packagePath: string, sourceFile: SourceFile): UniFunction {
257-
let symbol = method.getSymbol();
257+
const symbol = method.getSymbol();
258258
let methodName = ""
259259
if (symbol) {
260260
methodName = assignSymbolName(symbol)
@@ -290,7 +290,7 @@ export class FunctionParser {
290290
}
291291

292292
private parseConstructor(ctor: ConstructorDeclaration, moduleName: string, packagePath: string, sourceFile: SourceFile, className: string): UniFunction {
293-
let symbol = ctor.getSymbol();
293+
const symbol = ctor.getSymbol();
294294
let name = ""
295295
if (symbol) {
296296
name = assignSymbolName(symbol)
@@ -386,14 +386,14 @@ export class FunctionParser {
386386
}
387387

388388
// TODO: parse parameters
389-
private parseParameters(parameters: ParameterDeclaration[], moduleName: string, packagePath: string, sourceFile: SourceFile): Dependency[] {
389+
private parseParameters(_parameters: ParameterDeclaration[], _moduleName: string, _packagePath: string, _sourceFile: SourceFile): Dependency[] {
390390
const dependencies: Dependency[] = [];
391391

392392
return dependencies;
393393
}
394394

395395
// TODO: parse return types
396-
private parseReturnTypes(func: FunctionDeclaration | MethodSignature, moduleName: string, packagePath: string, sourceFile: SourceFile): Dependency[] {
396+
private parseReturnTypes(_func: FunctionDeclaration | MethodSignature, _moduleName: string, _packagePath: string, _sourceFile: SourceFile): Dependency[] {
397397
const results: Dependency[] = [];
398398
return results;
399399
}
@@ -402,7 +402,7 @@ export class FunctionParser {
402402
node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | ArrowFunction | FunctionExpression,
403403
moduleName: string,
404404
packagePath: string,
405-
sourceFile: SourceFile
405+
_sourceFile: SourceFile
406406
): Dependency[] {
407407
const calls: Dependency[] = [];
408408
const visited = new Set<string>();
@@ -429,7 +429,7 @@ export class FunctionParser {
429429
}
430430

431431
visited.add(key);
432-
let dep: Dependency = {
432+
const dep: Dependency = {
433433
ModPath: resolvedSymbol.moduleName || moduleName,
434434
PkgPath: this.getPkgPath(resolvedSymbol.packagePath || packagePath),
435435
Name: resolvedSymbol.name,
@@ -509,7 +509,7 @@ export class FunctionParser {
509509
newExpr: Identifier,
510510
moduleName: string,
511511
packagePath: string,
512-
sourceFile: SourceFile,
512+
_sourceFile: SourceFile,
513513
calls: Dependency[],
514514
visited: Set<string>
515515
): void {
@@ -521,7 +521,7 @@ export class FunctionParser {
521521
if (!resolvedSymbol) return;
522522

523523
// Handle method names like 'getX' -> 'getX'
524-
let nameFormat = resolvedSymbol.name;
524+
const nameFormat = resolvedSymbol.name;
525525

526526
const key = `${resolvedSymbol.moduleName}?${resolvedSymbol.packagePath}#${nameFormat}`;
527527

@@ -530,7 +530,7 @@ export class FunctionParser {
530530
}
531531
visited.add(key);
532532

533-
let dep: Dependency = {
533+
const dep: Dependency = {
534534
ModPath: resolvedSymbol.moduleName || moduleName,
535535
PkgPath: this.getPkgPath(resolvedSymbol.packagePath || packagePath),
536536
Name: nameFormat,
@@ -566,7 +566,7 @@ export class FunctionParser {
566566
propAccess: PropertyAccessExpression,
567567
moduleName: string,
568568
packagePath: string,
569-
sourceFile: SourceFile,
569+
_sourceFile: SourceFile,
570570
calls: Dependency[],
571571
visited: Set<string>
572572
): void {
@@ -578,7 +578,7 @@ export class FunctionParser {
578578
if (!resolvedSymbol) return;
579579

580580
// Handle method names like 'getX' -> 'getX'
581-
let nameFormat = resolvedSymbol.name;
581+
const nameFormat = resolvedSymbol.name;
582582

583583
const key = `${resolvedSymbol.moduleName}?${resolvedSymbol.packagePath}#${nameFormat}`;
584584

@@ -587,7 +587,7 @@ export class FunctionParser {
587587
}
588588
visited.add(key);
589589

590-
let dep: Dependency = {
590+
const dep: Dependency = {
591591
ModPath: resolvedSymbol.moduleName || moduleName,
592592
PkgPath: this.getPkgPath(resolvedSymbol.packagePath || packagePath),
593593
Name: nameFormat,
@@ -623,7 +623,7 @@ export class FunctionParser {
623623
node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | ArrowFunction | FunctionExpression,
624624
moduleName: string,
625625
packagePath: string,
626-
sourceFile: SourceFile
626+
_sourceFile: SourceFile
627627
): Dependency[] {
628628
const types: Dependency[] = [];
629629
const visited = new Set<string>();
@@ -683,7 +683,7 @@ export class FunctionParser {
683683
}
684684

685685
visited.add(key);
686-
let dep: Dependency = {
686+
const dep: Dependency = {
687687
ModPath: resolvedSymbol.moduleName || moduleName,
688688
PkgPath: this.getPkgPath(resolvedSymbol.packagePath || packagePath),
689689
Name: resolvedSymbol.name,
@@ -712,7 +712,7 @@ export class FunctionParser {
712712
node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | ArrowFunction | FunctionExpression,
713713
moduleName: string,
714714
packagePath: string,
715-
sourceFile: SourceFile
715+
_sourceFile: SourceFile
716716
): Dependency[] {
717717
const body = node.getBody();
718718
if (!body) return [];
@@ -795,7 +795,7 @@ export class FunctionParser {
795795

796796
visited.add(key);
797797

798-
let dep: Dependency = {
798+
const dep: Dependency = {
799799
ModPath: resolvedSymbol.moduleName || moduleName,
800800
PkgPath: this.getPkgPath(resolvedSymbol.packagePath || packagePath),
801801
Name: resolvedSymbol.name,

ts-parser/src/parser/ModuleParser.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
3-
import { Project, SourceFile, SyntaxKind } from 'ts-morph';
4-
import { Module, Package, Import } from '../types/uniast';
3+
import { Project, SourceFile } from 'ts-morph';
4+
import { Module, Package } from '../types/uniast';
55
import { PackageParser } from './PackageParser';
66
import { TypeScriptStructureAnalyzer } from '../utils/typescript-structure';
77
import { TsConfigCache } from '../utils/tsconfig-cache';
@@ -72,7 +72,7 @@ export class ModuleParser {
7272
Object.assign(dependencies, packageJson.peerDependencies);
7373
}
7474

75-
let pathUitl = new PathUtils(this.projectRoot)
75+
const pathUitl = new PathUtils(this.projectRoot)
7676

7777
// Build files map with detailed import analysis
7878
const files: Record<string, any> = {};
@@ -99,12 +99,8 @@ export class ModuleParser {
9999
};
100100
}
101101

102-
private extractImports(sourceFile: SourceFile, modulePath: string): Array<{ Path: string }> {
102+
private extractImports(sourceFile: SourceFile, _modulePath: string): Array<{ Path: string }> {
103103
const imports: Array<{ Path: string }> = [];
104-
const sourceFilePath = sourceFile.getFilePath();
105-
106-
// Cache path aliases for this module
107-
const pathAliases = this.tsConfigCache.getPathAliases(modulePath);
108104

109105
// Track unique import paths to avoid duplicates
110106
const uniquePaths = new Set<string>();
@@ -115,7 +111,12 @@ export class ModuleParser {
115111
const originalPath = importDecl.getModuleSpecifierValue();
116112
const resolvedPath = importDecl.getModuleSpecifierSourceFile()?.getFilePath();
117113
if (resolvedPath) {
118-
let relativePath = path.relative(this.projectRoot, resolvedPath);
114+
const relativePath = path.relative(this.projectRoot, resolvedPath);
115+
116+
if(uniquePaths.has(relativePath)) {
117+
continue;
118+
}
119+
uniquePaths.add(relativePath);
119120
imports.push({ Path: relativePath });
120121
} else {
121122
imports.push({ Path: "external:" + originalPath });
@@ -129,7 +130,7 @@ export class ModuleParser {
129130
if (originalPath) {
130131
const resolvedPath = exportDecl.getModuleSpecifierSourceFile()?.getFilePath();
131132
if (resolvedPath) {
132-
let relativePath = path.relative(this.projectRoot, resolvedPath);
133+
const relativePath = path.relative(this.projectRoot, resolvedPath);
133134
imports.push({ Path: relativePath });
134135
} else {
135136
imports.push({ Path: "external:" + originalPath });

0 commit comments

Comments
 (0)