Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion client/src/features/packageData/model/PythonClass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ test('getByRelativePath with correct path', () => {
'Class',
[],
[],
[new PythonFunction('function', 'function', [], [pythonParameter])],
[
new PythonFunction(
'function',
'function',
'function',
'function',
[],
[pythonParameter],
),
],
);
expect(pythonClass.getByRelativePath(['function', 'param'])).toBe(
pythonParameter,
Expand Down
8 changes: 6 additions & 2 deletions client/src/features/packageData/model/PythonDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export default abstract class PythonDeclaration {

abstract children(): PythonDeclaration[];

getUniqueName(): string {
return this.name;
}

path(): string[] {
let current: Optional<PythonDeclaration> = this;
const result: string[] = [];

while (current !== null && current !== undefined) {
result.unshift(current.name);
result.unshift(current.getUniqueName());
current = current.parent();
}

Expand All @@ -32,7 +36,7 @@ export default abstract class PythonDeclaration {
const [head, ...tail] = relativePath;
return (
this.children()
.find((it) => it.name === head)
.find((it) => it.getUniqueName() === head)
?.getByRelativePath(tail) ?? null
);
}
Expand Down
32 changes: 28 additions & 4 deletions client/src/features/packageData/model/PythonFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ import PythonPackage from './PythonPackage';
import PythonParameter from './PythonParameter';

test('path without parent', () => {
const pythonFunction = new PythonFunction('function', 'function');
const pythonFunction = new PythonFunction(
'function',
'function',
'function',
'function',
);
expect(pythonFunction.path()).toEqual(['function']);
});

test('path with ancestors', () => {
const pythonFunction = new PythonFunction('function', 'function');
const pythonFunction = new PythonFunction(
'function',
'function',
'function',
'function',
);

// eslint-disable-next-line no-new
new PythonPackage('distribution', 'package', '0.0.1', [
Expand All @@ -33,6 +43,8 @@ test('path with ancestors', () => {
test('getByRelativePath with correct path', () => {
const pythonParameter = new PythonParameter('param');
const pythonFunction = new PythonFunction(
'function',
'function',
'function',
'function',
[],
Expand All @@ -42,18 +54,30 @@ test('getByRelativePath with correct path', () => {
});

test('getByRelativePath with misleading path', () => {
const pythonFunction = new PythonFunction('function', 'function');
const pythonFunction = new PythonFunction(
'function',
'function',
'function',
'function',
);
// eslint-disable-next-line testing-library/prefer-presence-queries
expect(pythonFunction.getByRelativePath(['child'])).toBeNull();
});

test('toString without decorators and parameters', () => {
const pythonFunction = new PythonFunction('function', 'function');
const pythonFunction = new PythonFunction(
'function',
'function',
'function',
'function',
);
expect(pythonFunction.toString()).toBe('def function()');
});

test('toString with decorators and parameters', () => {
const pythonFunction = new PythonFunction(
'function',
'function',
'function',
'function',
['deco1', 'deco2'],
Expand Down
24 changes: 24 additions & 0 deletions client/src/features/packageData/model/PythonFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import PythonResult from './PythonResult';

export default class PythonFunction extends PythonDeclaration {
readonly name: string;
readonly uniqueName: string;
readonly qualifiedName: string;
readonly uniqueQualifiedName: string;
readonly decorators: string[];
readonly parameters: PythonParameter[];
readonly results: PythonResult[];
Expand All @@ -19,7 +21,9 @@ export default class PythonFunction extends PythonDeclaration {

constructor(
name: string,
uniqueName: string,
qualifiedName: string,
uniqueQualifiedName: string,
decorators: string[] = [],
parameters: PythonParameter[] = [],
results: PythonResult[] = [],
Expand All @@ -30,7 +34,9 @@ export default class PythonFunction extends PythonDeclaration {
super();

this.name = name;
this.uniqueName = uniqueName;
this.qualifiedName = qualifiedName;
this.uniqueQualifiedName = uniqueQualifiedName;
this.decorators = decorators;
this.parameters = parameters;
this.results = results;
Expand All @@ -56,6 +62,22 @@ export default class PythonFunction extends PythonDeclaration {
return this.parameters;
}

getUniqueName(): string {
return this.uniqueName;
}

isGetter(): boolean {
return this.decorators.includes('property');
}

isSetter(): boolean {
return this.decorators.some((it) => /[^.]*.setter/u.test(it));
}

isDeleter(): boolean {
return this.decorators.some((it) => /[^.]*.deleter/u.test(it));
}

explicitParameters(): PythonParameter[] {
if (this.parent() instanceof PythonModule) {
return this.children();
Expand Down Expand Up @@ -109,7 +131,9 @@ export default class PythonFunction extends PythonDeclaration {

return new PythonFunction(
this.name,
this.uniqueName,
this.qualifiedName,
this.uniqueQualifiedName,
this.decorators,
parameters,
results,
Expand Down
11 changes: 10 additions & 1 deletion client/src/features/packageData/model/PythonModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ test('getByRelativePath with correct path', () => {
[],
[],
[],
[new PythonFunction('function', 'function', [], [pythonParameter])],
[
new PythonFunction(
'function',
'function',
'function',
'function',
[],
[pythonParameter],
),
],
);
expect(pythonModule.getByRelativePath(['function', 'param'])).toBe(
pythonParameter,
Expand Down
26 changes: 17 additions & 9 deletions client/src/features/packageData/model/PythonPackageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const parsePythonPackageJson = function (
const functions = new Map(
packageJson.functions
.map(parsePythonFunctionJson)
.map((it) => [it.qualifiedName, it]),
.map((it) => [it.uniqueQualifiedName, it]),
);

// Classes
Expand Down Expand Up @@ -79,12 +79,14 @@ const parsePythonModuleJson = function (
),
moduleJson.functions
.sort((a, b) => a.localeCompare(b))
.filter((functionQualifiedName) =>
functions.has(functionQualifiedName),
.filter((functionUniqueQualifiedName) =>
functions.has(functionUniqueQualifiedName),
)
.map(
(functionQualifiedName) =>
functions.get(functionQualifiedName) as PythonFunction,
(functionUniqueQualifiedName) =>
functions.get(
functionUniqueQualifiedName,
) as PythonFunction,
),
);
};
Expand Down Expand Up @@ -139,12 +141,14 @@ const parsePythonClassJson = function (
classJson.superclasses,
classJson.methods
.sort((a, b) => a.localeCompare(b))
.filter((functionQualifiedName) =>
functions.has(functionQualifiedName),
.filter((functionUniqueQualifiedName) =>
functions.has(functionUniqueQualifiedName),
)
.map(
(functionQualifiedName) =>
functions.get(functionQualifiedName) as PythonFunction,
(functionUniqueQualifiedName) =>
functions.get(
functionUniqueQualifiedName,
) as PythonFunction,
),
classJson.description ?? '',
classJson.docstring ?? '',
Expand All @@ -153,7 +157,9 @@ const parsePythonClassJson = function (

interface PythonFunctionJson {
name: string;
unique_name: string;
qname: string;
unique_qname: string;
decorators: string[];
parameters: PythonParameterJson[];
results: PythonResultJson[];
Expand All @@ -168,7 +174,9 @@ const parsePythonFunctionJson = function (
): PythonFunction {
return new PythonFunction(
functionJson.name,
functionJson.unique_name,
functionJson.qname,
functionJson.unique_qname,
functionJson.decorators,
functionJson.parameters.map(parsePythonParameterJson),
functionJson.results.map(parsePythonResultJson),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ test('path with ancestors', () => {
[],
[
new PythonFunction(
'function',
'function',
'function',
'function',
[],
Expand Down
2 changes: 2 additions & 0 deletions client/src/features/packageData/model/PythonResult.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ test('path with ancestors', () => {
[],
[
new PythonFunction(
'function',
'function',
'function',
'function',
[],
Expand Down
2 changes: 1 addition & 1 deletion client/src/features/packageData/treeView/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const TreeNode: React.FC<TreeNodeProps> = function ({
isSelected={isSelected(declaration, currentPathname)}
/>
<Icon as={icon} />
<ChakraText>{declaration.name}</ChakraText>
<ChakraText>{declaration.getUniqueName()}</ChakraText>
</HStack>
);
};
Expand Down
Loading