Skip to content

Commit 04e8caf

Browse files
committed
More work and refactoring
1 parent 0f7502c commit 04e8caf

File tree

5 files changed

+259
-79
lines changed

5 files changed

+259
-79
lines changed

src/BindingIdentifier.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import {IBindingIdentifier} from "./interface/IBindingIdentifier";
2+
import { ArbitraryValue } from "src/interface/ISimpleLanguageService";
23

34
/**
45
* A simple class that represents identifiers.
56
* @author Frederik Wessberg
67
*/
78
export class BindingIdentifier implements IBindingIdentifier {
89

9-
constructor(public name: string, public path: string|null) { }
10-
11-
10+
constructor(public name: string, public path: ArbitraryValue[]|null) { }
1211

1312
/**
1413
* The string representation of an identifier
@@ -17,7 +16,23 @@ export class BindingIdentifier implements IBindingIdentifier {
1716
*/
1817
public toString(): string {
1918
const name = this.name == null ? "" : this.name;
20-
const path = this.path == null ? "" : this.path;
21-
return `${name}${path}`;
19+
return `${name}${this.flattenPath()}`;
20+
}
21+
22+
/**
23+
* Flattens the path down to a string.
24+
* @returns {string}
25+
*/
26+
public flattenPath(): string {
27+
if (this.path == null) return "";
28+
29+
return this.path.map(part => {
30+
if (typeof part === "string") {
31+
if (part.startsWith("[") && part.endsWith("]")) return part;
32+
return `["${part}"]`;
33+
}
34+
35+
return `[${part}]`;
36+
}).join("");
2237
}
2338
}

src/SimpleLanguageService.ts

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,72 @@ export class SimpleLanguageService implements ISimpleLanguageService {
10381038
}
10391039
}
10401040

1041+
/**
1042+
* Formats a CallExpression into an ICallExpression.
1043+
* @param {CallExpression}
1044+
* @returns {ICallExpression}
1045+
*/
1046+
private formatCallExpression(statement: CallExpression): ICallExpression {
1047+
const exp = statement.expression;
1048+
const typeExpressions = statement.typeArguments == null ? null : statement.typeArguments.map(typeArg => this.getTypeExpression(typeArg));
1049+
let typeExpression: TypeExpression = [];
1050+
if (typeExpressions != null) {
1051+
typeExpressions.forEach((typeExp, index) => {
1052+
typeExp.forEach(part => typeExpression.push(part));
1053+
if (index !== typeExpressions.length - 1) typeExpression.push(", ");
1054+
});
1055+
}
1056+
const typeFlattened = typeExpression == null || typeExpression.length < 1 ? null : this.serializeTypeExpression(typeExpression);
1057+
const typeBindings = typeExpression == null || typeExpression.length < 1 ? null : this.takeTypeBindings(typeExpression);
1058+
let property: ArbitraryValue = null;
1059+
let method: ArbitraryValue = null;
1060+
1061+
if (this.isIdentifierObject(exp)) {
1062+
method = this.getNameOfMember(exp, false, true);
1063+
}
1064+
1065+
if (this.isPropertyAccessExpression(exp)) {
1066+
property = this.getNameOfMember(exp.expression);
1067+
method = this.getNameOfMember(exp.name, false, true);
1068+
}
1069+
1070+
if (method == null) {
1071+
throw new TypeError(`${this.formatCallExpression.name} could not format a CallExpression with an expression of kind ${SyntaxKind[exp.kind]}`);
1072+
}
1073+
1074+
return {
1075+
arguments: {
1076+
startsAt: statement.arguments.pos,
1077+
endsAt: statement.arguments.end,
1078+
argumentsList: this.formatArguments(statement)
1079+
},
1080+
property,
1081+
method,
1082+
type: {
1083+
expression: typeExpression.length < 1 ? null : typeExpression,
1084+
flattened: typeFlattened,
1085+
bindings: typeBindings
1086+
}
1087+
};
1088+
1089+
}
1090+
1091+
/**
1092+
*Formats the given Statement into an ICallExpression.
1093+
* @param {Statement|Expression} statement
1094+
* @returns {ICallExpression}
1095+
*/
1096+
private getCallExpression(statement: Statement|Expression): ICallExpression {
1097+
if (this.isCallExpression(statement)) {
1098+
return this.formatCallExpression(statement);
1099+
}
1100+
1101+
if (this.isExpressionStatement(statement)) {
1102+
return this.getCallExpression(statement.expression);
1103+
}
1104+
throw new TypeError(`${this.getCallExpression.name} could not format a CallExpression of kind ${SyntaxKind[statement.kind]}`);
1105+
}
1106+
10411107
/**
10421108
* Gets and formats all CallExpressions associated with the given statements.
10431109
* These hold information such as the arguments the members are invoked with, generic type
@@ -1049,46 +1115,8 @@ export class SimpleLanguageService implements ISimpleLanguageService {
10491115
const expressions: ICallExpression[] = [];
10501116

10511117
statements.forEach(statement => {
1052-
1053-
if (this.isExpressionStatement(statement)) {
1054-
const exp = statement.expression;
1055-
1056-
if (this.isCallExpression(exp)) {
1057-
const expExp = exp.expression;
1058-
1059-
if (this.isPropertyAccessExpression(expExp)) {
1060-
const property = this.getNameOfMember(expExp.expression);
1061-
const method = this.getNameOfMember(expExp.name, false, true);
1062-
const typeExpressions = exp.typeArguments == null ? null : exp.typeArguments.map(typeArg => this.getTypeExpression(typeArg));
1063-
let typeExpression: TypeExpression = [];
1064-
1065-
if (typeExpressions != null) {
1066-
typeExpressions.forEach((typeExp, index) => {
1067-
typeExp.forEach(part => typeExpression.push(part));
1068-
if (index !== typeExpressions.length - 1) typeExpression.push(", ");
1069-
});
1070-
}
1071-
1072-
const typeFlattened = typeExpression == null || typeExpression.length < 1 ? null : this.serializeTypeExpression(typeExpression);
1073-
const typeBindings = typeExpression == null || typeExpression.length < 1 ? null : this.takeTypeBindings(typeExpression);
1074-
1075-
expressions.push({
1076-
arguments: {
1077-
startsAt: exp.arguments.pos,
1078-
endsAt: exp.arguments.end,
1079-
argumentsList: this.formatArguments(exp)
1080-
},
1081-
property,
1082-
method,
1083-
type: {
1084-
expression: typeExpression.length < 1 ? null : typeExpression,
1085-
flattened: typeFlattened,
1086-
bindings: typeBindings
1087-
1088-
}
1089-
});
1090-
}
1091-
}
1118+
if (this.isCallExpression(statement) || this.isExpressionStatement(statement)) {
1119+
expressions.push(this.getCallExpression(statement));
10921120
}
10931121
});
10941122
return expressions;
@@ -1510,10 +1538,10 @@ export class SimpleLanguageService implements ISimpleLanguageService {
15101538
/**
15111539
* Takes the path identifier of an expression. For instance, if it has a path, it returns it.
15121540
* @param {ArbitraryValue} expression
1513-
* @returns {string}
1541+
* @returns {ArbitraryValue[]}
15141542
*/
1515-
private takePath (expression: ArbitraryValue): string {
1516-
return expression instanceof BindingIdentifier && expression.path != null ? expression.path : "";
1543+
private takePath (expression: ArbitraryValue): ArbitraryValue[] {
1544+
return expression instanceof BindingIdentifier && expression.path != null ? expression.path : [];
15171545
}
15181546

15191547
/**
@@ -1571,12 +1599,12 @@ export class SimpleLanguageService implements ISimpleLanguageService {
15711599
const baseNameStringified = this.takeBase(baseName);
15721600
const pathStringified = this.takePath(baseName);
15731601

1574-
const path = name.name == null ? null : [pathStringified, ...this.getPathOfExpression(name.name)].filter(part => {
1602+
const path = name.name == null ? null : [...pathStringified, ...this.getPathOfExpression(name.name)].filter(part => {
15751603
if (typeof part === "string") return part.length > 0;
15761604
return part != null;
15771605
});
15781606

1579-
return new BindingIdentifier(baseNameStringified, path == null || path.length === 0 ? null : this.flattenPath(path));
1607+
return new BindingIdentifier(baseNameStringified, path);
15801608
}
15811609

15821610
if (this.isCallExpression(name)) {
@@ -1588,12 +1616,12 @@ export class SimpleLanguageService implements ISimpleLanguageService {
15881616
const baseNameStringified = this.takeBase(baseName);
15891617
const pathStringified = this.takePath(baseName);
15901618

1591-
const path = name.argumentExpression == null ? null : [pathStringified, ...this.getPathOfExpression(name.argumentExpression)].filter(part => {
1619+
const path = name.argumentExpression == null ? null : [...pathStringified, ...this.getPathOfExpression(name.argumentExpression)].filter(part => {
15921620
if (typeof part === "string") return part.length > 0;
15931621
return part != null;
15941622
});
15951623

1596-
return new BindingIdentifier(baseNameStringified, path == null || path.length === 0 ? null : this.flattenPath(path));
1624+
return new BindingIdentifier(baseNameStringified, path);
15971625
}
15981626

15991627
throw new TypeError(`${this.getNameOfMember.name} could not compute the name of a ${SyntaxKind[name.kind]}.`);
@@ -1861,22 +1889,6 @@ export class SimpleLanguageService implements ISimpleLanguageService {
18611889
return obj;
18621890
}
18631891

1864-
/**
1865-
* Flattens the given path down to a string.
1866-
* @param {ArbitraryValue[]} path
1867-
* @returns {string}
1868-
*/
1869-
private flattenPath(path: ArbitraryValue[]): string {
1870-
return path.map(part => {
1871-
if (typeof part === "string") {
1872-
if (part.startsWith("[") && part.endsWith("]")) return part;
1873-
return `["${part}"]`;
1874-
}
1875-
1876-
return `[${part}]`;
1877-
}).join("");
1878-
}
1879-
18801892
/**
18811893
* Formats a concrete ParameterDeclaration and returns an IParameter.
18821894
* @param {ParameterDeclaration} parameter

src/interface/IBindingIdentifier.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { ArbitraryValue } from "src/interface/ISimpleLanguageService";
12

23
export interface IBindingIdentifier {
34
name: string;
4-
path: string | null;
5+
path: ArbitraryValue[] | null;
6+
flattenPath(): string;
57
}

src/interface/ISimpleLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface IModuleDependency {
1414

1515
export interface ICallExpression extends IArgumentsable {
1616
property: ArbitraryValue;
17-
method: ArbitraryValue;
17+
method: NonNullableArbitraryValue;
1818
type: ITypeable;
1919
}
2020

0 commit comments

Comments
 (0)