Skip to content

Commit

Permalink
Refactor and fix handling positional and named constructor params
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Nov 15, 2024
1 parent 38f6d91 commit b5a78a1
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 144 deletions.
16 changes: 13 additions & 3 deletions src/data/dart_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ class DartClassConstructorField extends DartClassField {
constructor(
public name: string,
public type: string,
public nullable = false,
public named = true,
public positionType = DartClassConstructorFieldPositionType.named,
public nullable = false
) {
super(name, type, nullable);
}
}

export { DartClass, DartClassConstructor, DartClassConstructorField, DartClassField };
enum DartClassConstructorFieldPositionType {
named,
positional,
}

export {
DartClass,
DartClassConstructor,
DartClassConstructorField,
DartClassConstructorFieldPositionType,
DartClassField,
};
7 changes: 5 additions & 2 deletions src/generators/file_content/impl/3_0_0_generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { camelCase } from "change-case";
import { DartClassConstructor } from "../../../data/dart_class";
import {
DartClassConstructor,
DartClassConstructorFieldPositionType,
} from "../../../data/dart_class";
import { BaseFileContentGenerator } from "../base_generator";

class FileContentGenerator3_0_0 extends BaseFileContentGenerator {
Expand Down Expand Up @@ -49,7 +52,7 @@ class FileContentGenerator3_0_0 extends BaseFileContentGenerator {
`;

for (const field of constructor.fields) {
if (field.named) {
if (field.positionType == DartClassConstructorFieldPositionType.named) {
output += `${field.name}: ${this.knobForField(field)},\n`;
} else {
output += `${this.knobForField(field)},\n`;
Expand Down
102 changes: 64 additions & 38 deletions src/test/dart_class_parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DartClassConstructor, DartClassConstructorField, DartClassField } from "../data/dart_class";
import {
DartClassConstructor,
DartClassConstructorField,
DartClassConstructorFieldPositionType,
DartClassField,
} from "../data/dart_class";
import {
doesLookingFurtherMakeSense,
isConstructorLine,
Expand Down Expand Up @@ -435,15 +440,22 @@ describe("parseLinesToConstructor", () => {
" })",
];
const classFields = [
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassField("active", "bool"),
new DartClassField("semanticsLabel", "String"),
// class can have more fields which are not in the main constructor part because they are in its initializer list
new DartClassConstructorField("text", "String"),
new DartClassConstructorField("icon", "IconData"),
new DartClassField("text", "String"),
new DartClassField("icon", "IconData"),
];

expect(parseLinesToConstructor(lines, "Loader", classFields)).toEqual(
new DartClassConstructor(false, [classFields[0], classFields[1]], null)
new DartClassConstructor(
false,
[
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
],
null
)
);
});

Expand Down Expand Up @@ -473,18 +485,18 @@ describe("parseLinesToConstructor", () => {
" })",
];
const classFields = [
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("text", "String"),
new DartClassConstructorField("icon", "IconData"),
new DartClassField("active", "bool"),
new DartClassField("semanticsLabel", "String"),
new DartClassField("text", "String"),
new DartClassField("icon", "IconData"),
];

expect(parseLinesToConstructor(lines, "Loader", classFields)).toEqual(
new DartClassConstructor(
false,
[
classFields[0],
classFields[1],
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("big", "bool"),
new DartClassConstructorField("animated", "bool"),
],
Expand All @@ -504,18 +516,22 @@ describe("parseLinesToConstructor", () => {
" })",
];
const classFields = [
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("text", "String"),
new DartClassConstructorField("icon", "IconData"),
new DartClassField("active", "bool"),
new DartClassField("semanticsLabel", "String"),
new DartClassField("text", "String"),
new DartClassField("icon", "IconData"),
];

expect(parseLinesToConstructor(lines, "Loader", classFields)).toEqual(
new DartClassConstructor(
false,
[
classFields[0],
classFields[1],
new DartClassConstructorField(
"active",
"bool",
DartClassConstructorFieldPositionType.positional
),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("big", "bool"),
new DartClassConstructorField("animated", "bool"),
],
Expand Down Expand Up @@ -549,17 +565,20 @@ describe("parseLinesToConstructor", () => {
" }) : big = false,",
];
const classFields = [
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassField("active", "bool"),
new DartClassField("semanticsLabel", "String"),
// class can have more fields which are not in the main constructor part because they are in its initializer list
new DartClassConstructorField("text", "String"),
new DartClassConstructorField("icon", "IconData"),
new DartClassField("text", "String"),
new DartClassField("icon", "IconData"),
];

expect(parseLinesToConstructor(lines, "Loader", classFields)).toEqual(
new DartClassConstructor(
true,
[classFields[0], classFields[1]],
[
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
],
"small"
)
);
Expand All @@ -576,18 +595,18 @@ describe("parseLinesToConstructor", () => {
" })",
];
const classFields = [
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("text", "String"),
new DartClassConstructorField("icon", "IconData"),
new DartClassField("active", "bool"),
new DartClassField("semanticsLabel", "String"),
new DartClassField("text", "String"),
new DartClassField("icon", "IconData"),
];

expect(parseLinesToConstructor(lines, "Loader", classFields)).toEqual(
new DartClassConstructor(
true,
[
classFields[0],
classFields[1],
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("big", "bool"),
new DartClassConstructorField("animated", "bool"),
],
Expand All @@ -596,30 +615,37 @@ describe("parseLinesToConstructor", () => {
);
});


test("with positional fields and named fields", () => {
const lines = [
" const Loader.small(",
" this.active, {",
" this.active,",
" this.semanticsLabel, {",
" super.key,",
" this.semanticsLabel,",
" bool big = false,",
" required bool animated,",
" })",
];
const classFields = [
new DartClassConstructorField("active", "bool"),
new DartClassConstructorField("semanticsLabel", "String"),
new DartClassConstructorField("text", "String"),
new DartClassConstructorField("icon", "IconData"),
new DartClassField("active", "bool"),
new DartClassField("semanticsLabel", "String"),
new DartClassField("text", "String"),
new DartClassField("icon", "IconData"),
];

expect(parseLinesToConstructor(lines, "Loader", classFields)).toEqual(
new DartClassConstructor(
true,
[
classFields[0],
classFields[1],
new DartClassConstructorField(
"active",
"bool",
DartClassConstructorFieldPositionType.positional
),
new DartClassConstructorField(
"semanticsLabel",
"String",
DartClassConstructorFieldPositionType.positional
),
new DartClassConstructorField("big", "bool"),
new DartClassConstructorField("animated", "bool"),
],
Expand Down
Loading

0 comments on commit b5a78a1

Please sign in to comment.