Skip to content
This repository has been archived by the owner on Jan 19, 2025. It is now read-only.

Commit

Permalink
Merge pull request #58 from lars-reimann/track_parent_of_declaration
Browse files Browse the repository at this point in the history
Track the parent of Python declarations
  • Loading branch information
lars-reimann authored Jun 18, 2021
2 parents 9815f71 + 8a265b6 commit 7652a04
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 16 deletions.
24 changes: 24 additions & 0 deletions client/src/model/PythonClass.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import PythonClass from "./PythonClass";
import PythonPackage from "./PythonPackage";
import PythonModule from "./PythonModule";

test("path without parent", () => {
const pythonClass = new PythonClass("Class")
expect(pythonClass.path()).toEqual(["Class"])
})

test("path with ancestors", () => {
const pythonClass = new PythonClass("Class")
new PythonPackage(
"package",
[
new PythonModule(
"module",
[],
[],
[pythonClass]
)
]
)

expect(pythonClass.path()).toEqual(["package", "module", "Class"])
})

test("toString without decorators and superclasses", () => {
const pythonClass = new PythonClass("Class")
Expand Down
28 changes: 21 additions & 7 deletions client/src/model/PythonClass.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PythonFunction from "./PythonFunction";
import PythonModule from "./PythonModule";
import PythonDeclaration from "./PythonDeclaration";

export default class PythonClass {
export default class PythonClass extends PythonDeclaration {

readonly name: string;
readonly decorators: string[];
Expand All @@ -9,6 +11,7 @@ export default class PythonClass {
readonly summary: string;
readonly description: string;
readonly fullDocstring: string;
containingModule: Nullable<PythonModule>;

constructor(
name: string,
Expand All @@ -19,29 +22,40 @@ export default class PythonClass {
description: string = "",
fullDocstring: string = "",
) {
super();

this.name = name;
this.decorators = decorators;
this.superclasses = superclasses;
this.methods = methods;
this.summary = summary;
this.description = description;
this.fullDocstring = fullDocstring;
this.containingModule = null;

this.methods.forEach(it => {
it.containingModuleOrClass = this;
});
}

parent(): Nullable<PythonDeclaration> {
return this.containingModule;
}

toString() {
let result = ""
let result = "";

if (this.decorators.length > 0) {
result += this.decorators.map(it => `@${it}`).join(" ")
result += " "
result += this.decorators.map(it => `@${it}`).join(" ");
result += " ";
}

result += `class ${this.name}`
result += `class ${this.name}`;

if (this.superclasses.length > 0) {
result += `(${this.superclasses.join(", ")})`
result += `(${this.superclasses.join(", ")})`;
}

return result
return result;
}
}
18 changes: 18 additions & 0 deletions client/src/model/PythonDeclaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default abstract class PythonDeclaration {
abstract readonly name: string

abstract parent(): Nullable<PythonDeclaration>

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

const result: string[] = [];

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

return result;
}
}
32 changes: 32 additions & 0 deletions client/src/model/PythonFunction.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import PythonFunction from "./PythonFunction";
import PythonParameter from "./PythonParameter";
import PythonClass from "./PythonClass";
import PythonPackage from "./PythonPackage";
import PythonModule from "./PythonModule";

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

test("path with ancestors", () => {
const pythonFunction = new PythonFunction("function")
new PythonPackage(
"package",
[
new PythonModule(
"module",
[],
[],
[
new PythonClass(
"Class",
[],
[],
[pythonFunction]
)
]
)
]
)

expect(pythonFunction.path()).toEqual(["package", "module", "Class", "function"])
})

test("toString without decorators and parameters", () => {
const pythonFunction = new PythonFunction("function")
Expand Down
21 changes: 20 additions & 1 deletion client/src/model/PythonFunction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import PythonParameter from "./PythonParameter";
import PythonResult from "./PythonResult";
import PythonClass from "./PythonClass";
import PythonModule from "./PythonModule";
import PythonDeclaration from "./PythonDeclaration";

export default class PythonFunction {
export default class PythonFunction extends PythonDeclaration {

readonly name: string;
readonly decorators: string[];
Expand All @@ -11,6 +14,7 @@ export default class PythonFunction {
readonly summary: string;
readonly description: string;
readonly fullDocstring: string;
containingModuleOrClass: Nullable<PythonModule | PythonClass>;

constructor(
name: string,
Expand All @@ -22,6 +26,8 @@ export default class PythonFunction {
description: string = "",
fullDocstring: string = "",
) {
super();

this.name = name;
this.decorators = decorators;
this.parameters = parameters;
Expand All @@ -30,6 +36,19 @@ export default class PythonFunction {
this.summary = summary;
this.description = description;
this.fullDocstring = fullDocstring;
this.containingModuleOrClass = null;

this.parameters.forEach(it => {
it.containingFunction = this
})

this.results.forEach(it => {
it.containingFunction = this
})
}

parent(): Nullable<PythonModule | PythonClass> {
return this.containingModuleOrClass;
}

toString() {
Expand Down
16 changes: 16 additions & 0 deletions client/src/model/PythonModule.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import PythonModule from "./PythonModule";
import PythonPackage from "./PythonPackage";

test("path without parent", () => {
const pythonModule = new PythonModule("module")
expect(pythonModule.path()).toEqual(["module"])
})

test("path with parent", () => {
const pythonModule = new PythonModule("module")
new PythonPackage(
"package",
[pythonModule]
)

expect(pythonModule.path()).toEqual(["package", "module"])
})

test("toString", () => {
const pythonModule = new PythonModule("module")
Expand Down
22 changes: 20 additions & 2 deletions client/src/model/PythonModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import PythonFunction from "./PythonFunction";
import PythonClass from "./PythonClass";
import PythonFromImport from "./PythonFromImport";
import PythonImport from "./PythonImport";
import PythonPackage from "./PythonPackage";
import PythonDeclaration from "./PythonDeclaration";

export default class PythonModule {
export default class PythonModule extends PythonDeclaration {

readonly name: string;
readonly imports: PythonImport[];
readonly fromImports: PythonFromImport[];
readonly classes: PythonClass[];
readonly functions: PythonFunction[];
containingPackage: Nullable<PythonPackage>;

constructor(
name: string,
Expand All @@ -18,14 +21,29 @@ export default class PythonModule {
classes: PythonClass[] = [],
functions: PythonFunction[] = []
) {
super();

this.name = name;
this.imports = imports;
this.fromImports = fromImports;
this.classes = classes;
this.functions = functions;
this.containingPackage = null;

this.classes.forEach(it => {
it.containingModule = this
});

this.functions.forEach(it => {
it.containingModuleOrClass = this
});
}

parent(): Nullable<PythonPackage> {
return this.containingPackage;
}

toString() {
return `Module "${this.name}"`
return `Module "${this.name}"`;
}
}
5 changes: 5 additions & 0 deletions client/src/model/PythonPackage.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import PythonPackage from "./PythonPackage";

test("path", () => {
const pythonPackage = new PythonPackage("package")
expect(pythonPackage.path()).toEqual(["package"])
})

test("toString", () => {
const pythonPackage = new PythonPackage("package")
expect(pythonPackage.toString()).toBe(`Package "package"`)
Expand Down
15 changes: 13 additions & 2 deletions client/src/model/PythonPackage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import PythonModule from "./PythonModule";
import PythonDeclaration from "./PythonDeclaration";

export default class PythonPackage {
export default class PythonPackage extends PythonDeclaration {

readonly name: string;
readonly modules: PythonModule[];

constructor(name: string, modules: PythonModule[] = []) {
super();

this.name = name;
this.modules = modules;

this.modules.forEach(it => {
it.containingPackage = this;
});
}

parent(): null {
return null;
}

toString() {
return `Package "${this.name}"`
return `Package "${this.name}"`;
}
}
39 changes: 39 additions & 0 deletions client/src/model/PythonParameter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
import PythonParameter from "./PythonParameter";
import PythonFunction from "./PythonFunction";
import PythonPackage from "./PythonPackage";
import PythonModule from "./PythonModule";
import PythonClass from "./PythonClass";

test("path without parent", () => {
const pythonParameter = new PythonParameter("param")
expect(pythonParameter.path()).toEqual(["param"])
})

test("path with ancestors", () => {
const pythonParameter = new PythonParameter("param")
new PythonPackage(
"package",
[
new PythonModule(
"module",
[],
[],
[
new PythonClass(
"Class",
[],
[],
[
new PythonFunction(
"function",
[],
[pythonParameter]
)
]
)
]
)
]
)

expect(pythonParameter.path()).toEqual(["package", "module", "Class", "function", "param"])
})

test("toString", () => {
const pythonParameter = new PythonParameter("param")
Expand Down
Loading

0 comments on commit 7652a04

Please sign in to comment.