Skip to content

Commit

Permalink
fix: fix type errors in TS 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
uhyo committed Nov 21, 2020
1 parent e383425 commit cabfeb8
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 84 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"react": "^16.13.1",
"react-dom": "^16.13.1",
"standard-version": "^9.0.0",
"typescript": "^4.0.1-rc"
"typescript": "^4.1.2"
},
"husky": {
"hooks": {
Expand Down
15 changes: 10 additions & 5 deletions src/builder/PathRouteBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class PathRouteBuilder<
AnyFlag extends WildcardFlagType,
ExactFlag extends WildcardFlagType,
Match
> implements AttachableRouteBuilder<ActionResult, string> {
> implements AttachableRouteBuilder<ActionResult, string | undefined> {
static init<ActionResult>(): PathRouteBuilder<
ActionResult,
{},
Expand All @@ -79,10 +79,15 @@ export class PathRouteBuilder<
);
}

readonly #link: RouteBuilderLink<ActionResult, string>;
readonly #link: RouteBuilderLink<ActionResult, string | undefined>;
#routes: RouteRecordsBase<ActionResult> = Object.create(null);
#wildcardRoute:
| MatchingRouteRecordObject<ActionResult, string, Match, boolean>
| MatchingRouteRecordObject<
ActionResult,
string | undefined,
Match,
boolean
>
| undefined = undefined;
#exactRoute:
| PathRouteRecord<ActionResult, Match, boolean>
Expand Down Expand Up @@ -214,7 +219,7 @@ export class PathRouteBuilder<
action = a;
return this;
},
attach(builder: AttachableRouteBuilder<ActionResult, Match>) {
attach(builder: AttachableRouteBuilder<ActionResult, unknown>) {
attachedBuilder = builder;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return builder as any;
Expand Down Expand Up @@ -352,7 +357,7 @@ export class PathRouteBuilder<
return this.#exactRoute as ExactRouteType<ActionResult, ExactFlag, Match>;
}

getBuilderLink(): RouteBuilderLink<ActionResult, string> {
getBuilderLink(): RouteBuilderLink<ActionResult, string | undefined> {
return this.#link;
}
}
2 changes: 1 addition & 1 deletion src/builder/RouteRecord/PathRouteRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class PathRouteRecord<ActionResult, Match, HasAction extends boolean>
readonly key: string | undefined;

constructor(
parent: AttachableRouteBuilder<ActionResult, string>,
parent: AttachableRouteBuilder<ActionResult, string | undefined>,
key: string | undefined,
action: ActionTypeOfRouteRecord<ActionResult, Match, HasAction>
) {
Expand Down
6 changes: 3 additions & 3 deletions src/builder/SearchRouteBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SearchRouteBuilder<
Match,
WildcardFlagToHasAction<WildcardFlag>
>
implements AttachableRouteBuilder<ActionResult, string> {
implements AttachableRouteBuilder<ActionResult, string | undefined> {
static init<
ActionResult,
Key extends string,
Expand Down Expand Up @@ -76,7 +76,7 @@ export class SearchRouteBuilder<
readonly matchKey: Extract<keyof Match, string>;
readonly optional: boolean;

#link: RouteBuilderLink<ActionResult, string>;
#link: RouteBuilderLink<ActionResult, string | undefined>;
#route: RouteRecordType<ActionResult, Match, boolean>;

private constructor(
Expand Down Expand Up @@ -138,7 +138,7 @@ export class SearchRouteBuilder<
return this.#route;
}

getBuilderLink(): RouteBuilderLink<ActionResult, string> {
getBuilderLink(): RouteBuilderLink<ActionResult, string | undefined> {
return this.#link;
}
}
6 changes: 3 additions & 3 deletions src/builder/SingleHashRouteBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class SingleHashRouteBuilder<
Match,
WildcardFlagToHasAction<WildcardFlag>
>
implements AttachableRouteBuilder<ActionResult, string> {
implements AttachableRouteBuilder<ActionResult, string | undefined> {
static init<
ActionResult,
Key extends string,
Expand Down Expand Up @@ -74,7 +74,7 @@ export class SingleHashRouteBuilder<
readonly matchKey: Extract<keyof Match, string>;
readonly optional: boolean;

#link: RouteBuilderLink<ActionResult, string>;
#link: RouteBuilderLink<ActionResult, string | undefined>;
#route: RouteRecordType<ActionResult, Match, boolean>;

private constructor(
Expand Down Expand Up @@ -136,7 +136,7 @@ export class SingleHashRouteBuilder<
return this.#route;
}

getBuilderLink(): RouteBuilderLink<ActionResult, string> {
getBuilderLink(): RouteBuilderLink<ActionResult, string | undefined> {
return this.#link;
}
}
6 changes: 3 additions & 3 deletions src/builder/StateRouteBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class StateRouteBuilder<
Match,
WildcardFlagToHasAction<WildcardFlag>
>
implements AttachableRouteBuilder<ActionResult, StateValue> {
implements AttachableRouteBuilder<ActionResult, StateValue | undefined> {
static init<
ActionResult,
StateValue,
Expand Down Expand Up @@ -85,7 +85,7 @@ export class StateRouteBuilder<

readonly matchKey: Extract<keyof Match, string>;

#link: RouteBuilderLink<ActionResult, StateValue>;
#link: RouteBuilderLink<ActionResult, StateValue | undefined>;
#validator: Validator<StateValue>;
#route: RouteRecordType<ActionResult, Match, boolean>;

Expand Down Expand Up @@ -143,7 +143,7 @@ export class StateRouteBuilder<
return this.#route;
}

getBuilderLink(): RouteBuilderLink<ActionResult, StateValue> {
getBuilderLink(): RouteBuilderLink<ActionResult, StateValue | undefined> {
return this.#link;
}
}
4 changes: 2 additions & 2 deletions src/core/BuilderLink/SegmentResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export type ResolvedSegmentType<ActionResult, Segment, Value> =
// value: RouteRecordType<ActionResult, never, boolean>;
value: Value;
// TODO: this `| undefined` could be removed?
link: BuilderLink<ActionResult, Segment, Value> | undefined;
link: BuilderLink<ActionResult, unknown, Value> | undefined;
}
| {
type: "matching";
value: Value;
link: BuilderLink<ActionResult, Segment, Value> | undefined;
link: BuilderLink<ActionResult, unknown, Value> | undefined;
matchKey: string;
matchValue: Segment;
};
Expand Down
10 changes: 7 additions & 3 deletions src/core/BuilderLink/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Location } from "../Location";
import type { LocationComposer } from "../LocationComposer";
import { RouteResolver } from "../RouteResolver";
import {
createSegmentDecomposer,
SegmentDecomposer,
} from "../RouteResolver/resolveChain";
import type { BuilderLinkOptions } from "./BuilderLinkOptions";
import { BuilderLinkState } from "./BuilderLinkState";
import { HasBuilderLink } from "./HasBuilderLink";
Expand All @@ -21,7 +25,7 @@ export class BuilderLink<ActionResult, Segment, Value>
* Registered current builder.
*/
currentBuilder?: HasBuilderLink<ActionResult, Segment, Value> = undefined;
resolveSegment?: SegmentResolver<ActionResult, Segment, Value>;
segmentDecomposer?: SegmentDecomposer<ActionResult, Segment, Value>;

constructor(options: BuilderLinkOptions<Segment>) {
this.composer = options.composer;
Expand All @@ -32,7 +36,7 @@ export class BuilderLink<ActionResult, Segment, Value>
* Attach this link to a parent.
*/
attachToParent(
parentLink: BuilderLink<ActionResult, unknown, Value>,
parentLink: BuilderLink<ActionResult, Segment, Value>,
segmentGetter: (match: unknown) => Segment
) {
if (this.#state.state !== "unattached") {
Expand Down Expand Up @@ -109,7 +113,7 @@ export class BuilderLink<ActionResult, Segment, Value>
resolveSegment: SegmentResolver<ActionResult, Segment, Value>
): void {
this.currentBuilder = builder;
this.resolveSegment = resolveSegment;
this.segmentDecomposer = createSegmentDecomposer(this, resolveSegment);
}

/**
Expand Down
153 changes: 93 additions & 60 deletions src/core/RouteResolver/resolveChain.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,112 @@
import type { BuilderLink } from "../BuilderLink";
import {
ResolvedSegmentType,
SegmentResolver,
} from "../BuilderLink/SegmentResolver";
import type { Location } from "../Location";
import type { ResolvedRoute } from "./ResolvedRoute";

type DecomposedSegmentType<ActionResult, Segment, Value> = ResolvedSegmentType<
ActionResult,
Segment,
Value
> & {
segment: Segment;
nextLocation: Location;
};

export type SegmentDecomposer<ActionResult, Segment, Value> = (
location: Location
) => DecomposedSegmentType<ActionResult, Segment, Value>[];
/**
* Create a segment decomposer function for given BuilderLink and SegmentResolver.
*/
export function createSegmentDecomposer<ActionResult, Segment, Value>(
link: BuilderLink<ActionResult, Segment, Value>,
resolveSegment: SegmentResolver<ActionResult, Segment, Value>
): SegmentDecomposer<ActionResult, Segment, Value> {
const { composer } = link;
return (location) => {
const decomposed = composer.decompose(location);
return decomposed.flatMap(({ segment, nextLocation }) => {
const resolved = resolveSegment(segment, nextLocation);
if (resolved === undefined) {
return [];
}
return [{ ...resolved, segment, nextLocation }];
});
};
}

/**
* Resolve location from given link and location
* @package
*/
export function resolveChain<ActionResult, Value>(
link: BuilderLink<ActionResult, unknown, Value>,
export function resolveChain<ActionResult, Segment, Value>(
link: BuilderLink<ActionResult, Segment, Value>,
location: Location,
currentLocation: Location
): Array<ResolvedRoute<Value>> {
const decomposed = link.composer.decompose(location);
return decomposed.flatMap(
({ segment, nextLocation: nextRemainingLocation }) => {
const resolved = link.resolveSegment?.(segment, nextRemainingLocation);
if (resolved === undefined) {
return [];
}
const nextCurrentLocation = link.composer.compose(
currentLocation,
segment
);
const match = (resolved.type === "normal"
? {}
: {
[resolved.matchKey]: segment,
}) as never;
const resolvedSegments = link.segmentDecomposer?.(location);
if (resolvedSegments === undefined) {
return [];
}
return resolvedSegments.flatMap((resolved) => {
const nextCurrentLocation = link.composer.compose(
currentLocation,
resolved.segment
);
const match = (resolved.type === "normal"
? {}
: {
[resolved.matchKey]: resolved.matchValue,
}) as never;

const childLink = resolved.link;
const childLink = resolved.link;

if (childLink === undefined) {
return [
{
route: resolved.value,
match,
remainingLocation: nextRemainingLocation,
currentLocation: nextCurrentLocation,
},
];
}
if (childLink === undefined) {
return [
{
route: resolved.value,
match,
remainingLocation: resolved.nextLocation,
currentLocation: nextCurrentLocation,
},
];
}

const result = resolveChain<ActionResult, Value>(
childLink,
nextRemainingLocation,
nextCurrentLocation
);
const result = resolveChain<ActionResult, unknown, Value>(
childLink,
resolved.nextLocation,
nextCurrentLocation
);

if (
result.length === 0 &&
childLink.composer.isLeaf(nextRemainingLocation)
) {
return [
{
route: resolved.value,
match,
remainingLocation: nextRemainingLocation,
currentLocation: nextCurrentLocation,
},
];
if (
result.length === 0 &&
childLink.composer.isLeaf(resolved.nextLocation)
) {
return [
{
route: resolved.value,
match,
remainingLocation: resolved.nextLocation,
currentLocation: nextCurrentLocation,
},
];
}
switch (resolved.type) {
case "normal": {
return result;
}
switch (resolved.type) {
case "normal": {
return result;
}
case "matching": {
const key = resolved.matchKey;
const matchedValue = resolved.matchValue;
return result.map((res) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(res.match as any)[key] = matchedValue;
return res;
});
}
case "matching": {
const key = resolved.matchKey;
const matchedValue = resolved.matchValue;
return result.map((res) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(res.match as any)[key] = matchedValue;
return res;
});
}
}
);
});
}

0 comments on commit cabfeb8

Please sign in to comment.