Skip to content

Commit 36628b6

Browse files
angrychowHoblovski
authored andcommitted
stop collecting property type
1 parent eb812cf commit 36628b6

2 files changed

Lines changed: 11 additions & 129 deletions

File tree

ts-parser/src/parser/TypeParser.ts

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ import { Type as UniType, Dependency } from '../types/uniast';
1414
import { assignSymbolName, SymbolResolver } from '../utils/symbol-resolver';
1515
import { PathUtils } from '../utils/path-utils';
1616
import { TypeUtils } from '../utils/type-utils';
17+
import { DependencyUtils } from '../utils/dependency-utils';
1718

1819
export class TypeParser {
1920
private symbolResolver: SymbolResolver;
2021
private pathUtils: PathUtils;
2122
private defaultExported: Symbol | undefined
23+
private dependencyUtils: DependencyUtils;
2224

2325
constructor(projectRoot: string) {
2426
this.symbolResolver = new SymbolResolver(null as any, projectRoot);
2527
this.pathUtils = new PathUtils(projectRoot);
28+
this.dependencyUtils = new DependencyUtils(this.symbolResolver, projectRoot);
2629
}
2730

2831
parseTypes(sourceFile: SourceFile, moduleName: string, packagePath: string): Record<string, UniType> {
@@ -132,19 +135,8 @@ export class TypeParser {
132135
}
133136
}
134137

135-
// Parse property types
136-
const propertyTypes: Dependency[] = [];
137-
const properties = cls.getProperties();
138-
for (const prop of properties) {
139-
const propType = prop.getTypeNode();
140-
if (propType) {
141-
const typeDependencies = this.extractTypeDependencies(propType, moduleName, packagePath);
142-
propertyTypes.push(...typeDependencies);
143-
}
144-
}
145-
146138
// Combine implements and extends into Implements, but filter out external symbols
147-
const allImplements = [...implementsInterfaces, ...extendsClasses, ...propertyTypes];
139+
const allImplements = [...implementsInterfaces, ...extendsClasses];
148140

149141
return {
150142
ModPath: moduleName,
@@ -200,40 +192,8 @@ export class TypeParser {
200192
}
201193
}
202194
}
203-
204-
// Parse property types
205-
const propertyTypes: Dependency[] = [];
206-
const properties = iface.getProperties();
207-
for (const prop of properties) {
208-
const propType = prop.getTypeNode();
209-
if (propType) {
210-
const typeDependencies = this.extractTypeDependencies(propType, moduleName, packagePath);
211-
propertyTypes.push(...typeDependencies);
212-
}
213-
}
214-
215-
// Parse method signatures
216-
const methodTypes: Dependency[] = [];
217-
const methodSignatures = iface.getMethods();
218-
for (const method of methodSignatures) {
219-
const returnType = method.getReturnTypeNode();
220-
if (returnType) {
221-
const typeDependencies = this.extractTypeDependencies(returnType, moduleName, packagePath);
222-
methodTypes.push(...typeDependencies);
223-
}
224-
225-
const parameters = method.getParameters();
226-
for (const param of parameters) {
227-
const paramType = param.getTypeNode();
228-
if (paramType) {
229-
const typeDependencies = this.extractTypeDependencies(paramType, moduleName, packagePath);
230-
methodTypes.push(...typeDependencies);
231-
}
232-
}
233-
}
234-
235195
// Combine extends interfaces and other dependencies into Implements, but filter out external symbols
236-
const allImplements = [...extendsInterfaces, ...propertyTypes, ...methodTypes];
196+
const allImplements = [...extendsInterfaces];
237197

238198
return {
239199
ModPath: moduleName,
@@ -332,18 +292,14 @@ export class TypeParser {
332292
const visited = new Set<string>();
333293

334294
// Extract from identifiers and find their definitions
335-
const identifiers = typeNode.getDescendantsOfKind(SyntaxKind.Identifier);
295+
const types = this.dependencyUtils.extractAtomicTypeReferences(typeNode);
336296

337-
for (const identifier of identifiers) {
338-
const symbol = identifier.getSymbol();
339-
if (!symbol || (symbol.getFlags() & (SymbolFlags.Type | SymbolFlags.TypeAlias | SymbolFlags.TypeLiteral)) === 0) {
297+
for (const t of types) {
298+
const symbol = t.getSymbol();
299+
if (!symbol) {
340300
continue;
341301
}
342-
343-
const typeName = identifier.getText();
344-
if (this.isPrimitiveType(typeName)) continue;
345-
346-
const [resolvedSymbol, resolvedRealSymbol] = this.symbolResolver.resolveSymbol(symbol, identifier);
302+
const [resolvedSymbol, resolvedRealSymbol] = this.symbolResolver.resolveSymbol(symbol, typeNode);
347303
// if symbol is not external, add it to dependencies
348304
if (!resolvedSymbol || resolvedSymbol.isExternal) {
349305
continue;
@@ -443,16 +399,6 @@ export class TypeParser {
443399
}
444400
}
445401

446-
// Parse property types
447-
const propertyTypes: Dependency[] = [];
448-
const properties = classExpr.getProperties();
449-
for (const prop of properties) {
450-
const propType = prop.getTypeNode();
451-
if (propType) {
452-
const typeDependencies = this.extractTypeDependencies(propType, moduleName, packagePath);
453-
propertyTypes.push(...typeDependencies);
454-
}
455-
}
456402

457403
return {
458404
ModPath: moduleName,
@@ -466,7 +412,7 @@ export class TypeParser {
466412
TypeKind: 'struct',
467413
Content: content,
468414
Methods: methods,
469-
Implements: [...implementsInterfaces, ...extendsClasses, ...propertyTypes],
415+
Implements: [...implementsInterfaces, ...extendsClasses],
470416
SubStruct: [],
471417
InlineStruct: []
472418
};

ts-parser/src/parser/test/TypeParser.test.ts

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -294,70 +294,6 @@ describe('TypeParser', () => {
294294
});
295295

296296
describe('type dependencies', () => {
297-
it('should extract type dependencies from class properties', () => {
298-
const { project, sourceFile, cleanup } = createTestProject(`
299-
class CustomType {}
300-
interface CustomInterface {}
301-
302-
class PropertyTypes {
303-
customType: CustomType;
304-
customInterface: CustomInterface;
305-
primitive: string;
306-
arrayType: string[];
307-
unionType: string | number;
308-
genericType: Array<number>;
309-
}
310-
`);
311-
312-
const parser = new TypeParser(process.cwd());
313-
let pkgPathAbsFile : string = sourceFile.getFilePath()
314-
pkgPathAbsFile = pkgPathAbsFile.split('/').slice(0, -1).join('/')
315-
const pkgPath = path.relative(process.cwd(), pkgPathAbsFile)
316-
317-
const types = parser.parseTypes(sourceFile, 'parser-tests', pkgPath);
318-
319-
const propertyTypes = expectToBeDefined(types['PropertyTypes']);
320-
expect(expectToBeDefined(propertyTypes.Implements).length).toBeGreaterThan(0);
321-
322-
const typeNames = expectToBeDefined(propertyTypes.Implements).map(dep => dep.Name);
323-
expect(typeNames).toContain('CustomType');
324-
expect(typeNames).toContain('CustomInterface');
325-
326-
cleanup();
327-
});
328-
329-
it('should extract type dependencies from interface properties', () => {
330-
const { project, sourceFile, cleanup } = createTestProject(`
331-
class CustomType {}
332-
interface CustomInterface {}
333-
334-
interface PropertyTypes {
335-
customType: CustomType;
336-
customInterface: CustomInterface;
337-
primitive: string;
338-
arrayType: string[];
339-
unionType: string | number;
340-
genericType: Array<number>;
341-
}
342-
`);
343-
344-
const parser = new TypeParser(process.cwd());
345-
let pkgPathAbsFile : string = sourceFile.getFilePath()
346-
pkgPathAbsFile = pkgPathAbsFile.split('/').slice(0, -1).join('/')
347-
const pkgPath = path.relative(process.cwd(), pkgPathAbsFile)
348-
349-
const types = parser.parseTypes(sourceFile, 'parser-tests', pkgPath);
350-
351-
const propertyTypes = expectToBeDefined(types['PropertyTypes']);
352-
expect(expectToBeDefined(propertyTypes.Implements).length).toBeGreaterThan(0);
353-
354-
const typeNames = expectToBeDefined(propertyTypes.Implements).map(dep => dep.Name);
355-
expect(typeNames).toContain('CustomType');
356-
expect(typeNames).toContain('CustomInterface');
357-
358-
cleanup();
359-
});
360-
361297
it('should extract type dependencies from type aliases', () => {
362298
const { project, sourceFile, cleanup } = createTestProject(`
363299
class CustomType {}

0 commit comments

Comments
 (0)