Skip to content

Commit

Permalink
#94, getOverloadIndex() with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jul 6, 2024
1 parent 28cc494 commit a5a1564
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 125 deletions.
21 changes: 0 additions & 21 deletions dist/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -1197,27 +1197,6 @@ function prependOverload(parentStructure, overloadStructure, kind) {
parentStructure.overloads.unshift(overload);
return overload;
}
/*
export function isOverload(
node: OverloadableNode & Node
): boolean
{
if (Node.isAmbientable(node) && node.hasDeclareKeyword())
return false;
if (Node.isMethodDeclaration(node) || Node.isConstructorDeclaration(node)) {
const parent: ClassDeclaration | ClassExpression | ObjectLiteralExpression = node.getParentOrThrow();
if (Node.isAmbientable(parent) && parent.hasDeclareKeyword())
return false;
}
const nodes: OverloadableNode[] = node.getOverloads();
const implNode = node.getImplementation();
if (implNode)
nodes.push(implNode);
return nodes[nodes.length - 1] !== node;
}
*/

var _a$6;
// #endregion preamble
Expand Down
59 changes: 34 additions & 25 deletions stage_2_integration/source/bootstrap/adjustForOverloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import {
type ClassDeclaration,
type ClassExpression,
*/
type ConstructorDeclaration,
type ConstructorDeclarationStructure,
type ConstructorDeclarationOverloadStructure,
type FunctionDeclaration,
type FunctionDeclarationStructure,
type FunctionDeclarationOverloadStructure,
type MethodDeclaration,
type MethodDeclarationStructure,
type MethodDeclarationOverloadStructure,
Node,
/*
type ObjectLiteralExpression,
*/
type OverloadableNode,
type StatementedNodeStructure,
Structure,
type Structures,
StructureKind,
SyntaxKind,
type WriterFunction,
forEachStructureChild,
} from "ts-morph";
Expand Down Expand Up @@ -256,32 +258,39 @@ function prependOverload<

//#endregion move structure overloads inside their parent structures

export function getOverloadIndex(
node: OverloadableNode & Node
export function getOverloadIndex<
NodeType extends ConstructorDeclaration | FunctionDeclaration | MethodDeclaration
>
(
node: NodeType
): number
{
const nodes: OverloadableNode[] = node.getOverloads();
return nodes.indexOf(node);
}

/*
export function isOverload(
node: OverloadableNode & Node
): boolean
{
if (Node.isAmbientable(node) && node.hasDeclareKeyword())
return false;
const kind = node.getKind() as SyntaxKind.Constructor | SyntaxKind.FunctionDeclaration | SyntaxKind.MethodDeclaration;
let matchingNodes = node.getParentOrThrow().getChildrenOfKind(kind) as NodeType[];

switch (kind) {
case SyntaxKind.Constructor:
matchingNodes = matchingNodes.slice();
break;

case SyntaxKind.FunctionDeclaration: {
const name = (node as FunctionDeclaration).getName();
if (name === undefined)
return -1;
matchingNodes = (matchingNodes as FunctionDeclaration[]).filter(n => n.getName() === name) as NodeType[];
break;
}

if (Node.isMethodDeclaration(node) || Node.isConstructorDeclaration(node)) {
const parent: ClassDeclaration | ClassExpression | ObjectLiteralExpression = node.getParentOrThrow();
if (Node.isAmbientable(parent) && parent.hasDeclareKeyword())
return false;
case SyntaxKind.MethodDeclaration: {
const name = (node as MethodDeclaration).getName();
const isStatic = (node as MethodDeclaration).isStatic();
matchingNodes = (matchingNodes as MethodDeclaration[]).filter(
n => n.isStatic() === isStatic && n.getName() === name
) as NodeType[];
break;
}
}

const nodes: OverloadableNode[] = node.getOverloads();
const implNode = node.getImplementation();
if (implNode)
nodes.push(implNode);
return nodes[nodes.length - 1] !== node;
matchingNodes.pop();
return matchingNodes.indexOf(node);
}
*/
65 changes: 41 additions & 24 deletions stage_2_snapshot/snapshot/source/bootstrap/adjustForOverloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import {
type ClassDeclaration,
type ClassExpression,
*/
type ConstructorDeclaration,
type ConstructorDeclarationStructure,
type ConstructorDeclarationOverloadStructure,
type FunctionDeclaration,
type FunctionDeclarationStructure,
type FunctionDeclarationOverloadStructure,
type MethodDeclaration,
type MethodDeclarationStructure,
type MethodDeclarationOverloadStructure,
Node,
/*
type ObjectLiteralExpression,
*/
type OverloadableNode,
type StatementedNodeStructure,
Structure,
type Structures,
StructureKind,
SyntaxKind,
type WriterFunction,
forEachStructureChild,
} from "ts-morph";
Expand Down Expand Up @@ -283,29 +285,44 @@ function prependOverload<

//#endregion move structure overloads inside their parent structures

export function getOverloadIndex(node: OverloadableNode & Node): number {
const nodes: OverloadableNode[] = node.getOverloads();
return nodes.indexOf(node);
}
export function getOverloadIndex<
NodeType extends
| ConstructorDeclaration
| FunctionDeclaration
| MethodDeclaration,
>(node: NodeType): number {
const kind = node.getKind() as
| SyntaxKind.Constructor
| SyntaxKind.FunctionDeclaration
| SyntaxKind.MethodDeclaration;
let matchingNodes = node
.getParentOrThrow()
.getChildrenOfKind(kind) as NodeType[];

switch (kind) {
case SyntaxKind.Constructor:
matchingNodes = matchingNodes.slice();
break;

case SyntaxKind.FunctionDeclaration: {
const name = (node as FunctionDeclaration).getName();
if (name === undefined) return -1;
matchingNodes = (matchingNodes as FunctionDeclaration[]).filter(
(n) => n.getName() === name,
) as NodeType[];
break;
}

/*
export function isOverload(
node: OverloadableNode & Node
): boolean
{
if (Node.isAmbientable(node) && node.hasDeclareKeyword())
return false;
if (Node.isMethodDeclaration(node) || Node.isConstructorDeclaration(node)) {
const parent: ClassDeclaration | ClassExpression | ObjectLiteralExpression = node.getParentOrThrow();
if (Node.isAmbientable(parent) && parent.hasDeclareKeyword())
return false;
case SyntaxKind.MethodDeclaration: {
const name = (node as MethodDeclaration).getName();
const isStatic = (node as MethodDeclaration).isStatic();
matchingNodes = (matchingNodes as MethodDeclaration[]).filter(
(n) => n.isStatic() === isStatic && n.getName() === name,
) as NodeType[];
break;
}
}

const nodes: OverloadableNode[] = node.getOverloads();
const implNode = node.getImplementation();
if (implNode)
nodes.push(implNode);
return nodes[nodes.length - 1] !== node;
matchingNodes.pop();
return matchingNodes.indexOf(node);
}
*/
Loading

0 comments on commit a5a1564

Please sign in to comment.