Skip to content

Commit c8db56a

Browse files
committed
Major overhaul in regards to serialization and value resolving
1 parent 2eb6000 commit c8db56a

35 files changed

+453
-861
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"build:es2015": "tsc --module es2015 --outDir dist/es2015 -p tsconfig.dist.json",
1818
"build": "npm run build:pre && npm run build:cjs && npm run build:es2015",
1919
"test:pre": "npm run clean:compiled && tsc --module commonjs --target es2017 --sourceMap",
20-
"test": "NODE_ENV=TEST npm run test:pre && ava"
20+
"test": "NODE_ENV=TEST npm run test:pre && ava --fail-fast"
2121
},
2222
"keywords": [
2323
"ast",

src/cache/Cache.ts

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
1-
import {ClassIndexer, EnumIndexer, FunctionIndexer, ICachedContent, IClassDeclaration, ICodeAnalyzer, IEnumDeclaration, IExportDeclaration, IFunctionDeclaration, IIdentifierMap, IImportDeclaration, IParameter, IPropDeclaration, IVariableAssignment, ResolvedIIdentifierValueMap, ResolvedSerializedIIdentifierValueMap, VariableIndexer} from "../service/interface/ICodeAnalyzer";
1+
import {ClassIndexer, EnumIndexer, FunctionIndexer, ICachedContent, IClassDeclaration, ICodeAnalyzer, IEnumDeclaration, IExportDeclaration, IFunctionDeclaration, IIdentifierMap, IImportDeclaration, IPropDeclaration, IVariableAssignment, ResolvedIIdentifierValueMap, ResolvedSerializedIIdentifierValueMap, VariableIndexer} from "../service/interface/ICodeAnalyzer";
22
import {ICache} from "./interface/ICache";
3-
import {SerializedVersions} from "../serializer/interface/IIdentifierSerializer";
43

54
export class Cache implements ICache {
65
private cache: Map<string, ICachedContent<{}>> = new Map();
76

87
constructor (private languageService: ICodeAnalyzer) {
98
}
109

11-
public getCachedSerializedVariableName (fileName: string, position: number, variableName: string): string {
12-
return `serializedVariable.${fileName}.${position}.${variableName}`;
13-
}
14-
15-
public getCachedSerializedParameterName (fileName: string, position: number, parameterName: (string|undefined)[]): string {
16-
return `serializedParameter.${fileName}.${position}.${parameterName.join(".")}`;
17-
}
18-
19-
public getCachedSerializedClassName (fileName: string, position: number, className: string): string {
20-
return `serializedClass.${fileName}.${position}.${className}`;
21-
}
22-
23-
public getCachedSerializedEnumName (fileName: string, position: number, enumName: string): string {
24-
return `serializedEnum.${fileName}.${position}.${enumName}`;
25-
}
26-
27-
public getCachedSerializedFunctionName (fileName: string, position: number, functionName: string): string {
28-
return `serializedFunction.${fileName}.${position}.${functionName}`;
29-
}
30-
3110
public getCachedPropName (fileName: string, className: string, propName: string): string {
3211
return `prop.${fileName}.${className}.${propName}`;
3312
}
@@ -89,26 +68,6 @@ export class Cache implements ICache {
8968
return record == null ? null : <ICachedContent<T>>record;
9069
}
9170

92-
public getCachedSerializedVariable (variable: IVariableAssignment): ICachedContent<SerializedVersions>|null {
93-
return this.getFromCache<SerializedVersions>(this.getCachedSerializedVariableName(variable.filePath, variable.startsAt, variable.name));
94-
}
95-
96-
public getCachedSerializedParameter (parameter: IParameter): ICachedContent<SerializedVersions>|null {
97-
return this.getFromCache<SerializedVersions>(this.getCachedSerializedParameterName(parameter.filePath, parameter.startsAt, parameter.name));
98-
}
99-
100-
public getCachedSerializedClass (classDeclaration: IClassDeclaration): ICachedContent<SerializedVersions>|null {
101-
return this.getFromCache<SerializedVersions>(this.getCachedSerializedClassName(classDeclaration.filePath, classDeclaration.startsAt, classDeclaration.name));
102-
}
103-
104-
public getCachedSerializedEnum (enumDeclaration: IEnumDeclaration): ICachedContent<SerializedVersions>|null {
105-
return this.getFromCache<SerializedVersions>(this.getCachedSerializedEnumName(enumDeclaration.filePath, enumDeclaration.startsAt, enumDeclaration.name));
106-
}
107-
108-
public getCachedSerializedFunction (functionDeclaration: IFunctionDeclaration): ICachedContent<SerializedVersions>|null {
109-
return this.getFromCache<SerializedVersions>(this.getCachedSerializedFunctionName(functionDeclaration.filePath, functionDeclaration.startsAt, functionDeclaration.name));
110-
}
111-
11271
public getCachedVariable (fileName: string, variableName: string): ICachedContent<IVariableAssignment>|null {
11372
return this.getFromCache<IVariableAssignment>(this.getCachedVariableName(fileName, variableName));
11473
}
@@ -165,31 +124,6 @@ export class Cache implements ICache {
165124
return this.getFromCache<ResolvedSerializedIIdentifierValueMap>(this.getCachedResolvedSerializedIdentifierValueMapName(fileName));
166125
}
167126

168-
public setCachedSerializedVariable (variable: IVariableAssignment, content: SerializedVersions): void {
169-
const version = this.languageService.getFileVersion(variable.filePath);
170-
this.cache.set(this.getCachedSerializedVariableName(variable.filePath, variable.startsAt, variable.name), {content, version});
171-
}
172-
173-
public setCachedSerializedParameter (parameter: IParameter, content: SerializedVersions): void {
174-
const version = this.languageService.getFileVersion(parameter.filePath);
175-
this.cache.set(this.getCachedSerializedParameterName(parameter.filePath, parameter.startsAt, parameter.name), {content, version});
176-
}
177-
178-
public setCachedSerializedClass (classDeclaration: IClassDeclaration, content: SerializedVersions): void {
179-
const version = this.languageService.getFileVersion(classDeclaration.filePath);
180-
this.cache.set(this.getCachedSerializedClassName(classDeclaration.filePath, classDeclaration.startsAt, classDeclaration.name), {content, version});
181-
}
182-
183-
public setCachedSerializedEnum (enumDeclaration: IEnumDeclaration, content: SerializedVersions): void {
184-
const version = this.languageService.getFileVersion(enumDeclaration.filePath);
185-
this.cache.set(this.getCachedSerializedEnumName(enumDeclaration.filePath, enumDeclaration.startsAt, enumDeclaration.name), {content, version});
186-
}
187-
188-
public setCachedSerializedFunction (functionDeclaration: IFunctionDeclaration, content: SerializedVersions): void {
189-
const version = this.languageService.getFileVersion(functionDeclaration.filePath);
190-
this.cache.set(this.getCachedSerializedFunctionName(functionDeclaration.filePath, functionDeclaration.startsAt, functionDeclaration.name), {content, version});
191-
}
192-
193127
public setCachedVariable (fileName: string, content: IVariableAssignment): void {
194128
const version = this.languageService.getFileVersion(fileName);
195129
this.cache.set(this.getCachedVariableName(fileName, content.name), {content, version});
@@ -260,46 +194,6 @@ export class Cache implements ICache {
260194
this.cache.set(this.getCachedVariableIndexerName(fileName), {version, content});
261195
}
262196

263-
public cachedSerializedVariableNeedsUpdate (variable: IVariableAssignment): boolean {
264-
const cache = this.getCachedSerializedVariable(variable);
265-
if (cache == null) return true;
266-
267-
const version = this.languageService.getFileVersion(variable.filePath);
268-
return version > cache.version;
269-
}
270-
271-
public cachedSerializedParameterNeedsUpdate (parameter: IParameter): boolean {
272-
const cache = this.getCachedSerializedParameter(parameter);
273-
if (cache == null) return true;
274-
275-
const version = this.languageService.getFileVersion(parameter.filePath);
276-
return version > cache.version;
277-
}
278-
279-
public cachedSerializedClassNeedsUpdate (classDeclaration: IClassDeclaration): boolean {
280-
const cache = this.getCachedSerializedClass(classDeclaration);
281-
if (cache == null) return true;
282-
283-
const version = this.languageService.getFileVersion(classDeclaration.filePath);
284-
return version > cache.version;
285-
}
286-
287-
public cachedSerializedEnumNeedsUpdate (enumDeclaration: IEnumDeclaration): boolean {
288-
const cache = this.getCachedSerializedEnum(enumDeclaration);
289-
if (cache == null) return true;
290-
291-
const version = this.languageService.getFileVersion(enumDeclaration.filePath);
292-
return version > cache.version;
293-
}
294-
295-
public cachedSerializedFunctionNeedsUpdate (functionDeclaration: IFunctionDeclaration): boolean {
296-
const cache = this.getCachedSerializedFunction(functionDeclaration);
297-
if (cache == null) return true;
298-
299-
const version = this.languageService.getFileVersion(functionDeclaration.filePath);
300-
return version > cache.version;
301-
}
302-
303197
public cachedVariableNeedsUpdate (variable: IVariableAssignment): boolean {
304198
const cache = this.getCachedVariable(variable.filePath, variable.name);
305199
if (cache == null) return true;

src/cache/interface/ICache.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import {ClassIndexer, EnumIndexer, FunctionIndexer, ICachedContent, IClassDeclaration, IEnumDeclaration, IExportDeclaration, IFunctionDeclaration, IIdentifierMap, IImportDeclaration, IParameter, IPropDeclaration, IVariableAssignment, ResolvedIIdentifierValueMap, ResolvedSerializedIIdentifierValueMap, VariableIndexer} from "../../service/interface/ICodeAnalyzer";
2-
import {SerializedVersions} from "../../serializer/interface/IIdentifierSerializer";
1+
import {ClassIndexer, EnumIndexer, FunctionIndexer, ICachedContent, IClassDeclaration, IEnumDeclaration, IExportDeclaration, IFunctionDeclaration, IIdentifierMap, IImportDeclaration, IPropDeclaration, IVariableAssignment, ResolvedIIdentifierValueMap, ResolvedSerializedIIdentifierValueMap, VariableIndexer} from "../../service/interface/ICodeAnalyzer";
32

43
export interface ICache {
5-
getCachedSerializedVariableName (fileName: string, position: number, variableName: string): string;
6-
getCachedSerializedParameterName (fileName: string, position: number, parameterName: (string|undefined)[]): string;
7-
getCachedSerializedClassName (fileName: string, position: number, className: string): string;
8-
getCachedSerializedEnumName (fileName: string, position: number, enumName: string): string;
9-
getCachedSerializedFunctionName (fileName: string, position: number, functionName: string): string;
104
getCachedPropName (fileName: string, className: string, propName: string): string;
115
getCachedVariableName (fileName: string, variableName: string): string;
126
getCachedEnumName (fileName: string, enumName: string): string;
@@ -22,11 +16,6 @@ export interface ICache {
2216
getCachedVariableIndexerName (fileName: string): string;
2317
getCachedEnumIndexerName (fileName: string): string;
2418
getFromCache<T> (key: string): ICachedContent<T>|null;
25-
getCachedSerializedVariable (variable: IVariableAssignment): ICachedContent<SerializedVersions>|null;
26-
getCachedSerializedParameter (parameter: IParameter): ICachedContent<SerializedVersions>|null;
27-
getCachedSerializedClass (classDeclaration: IClassDeclaration): ICachedContent<SerializedVersions>|null;
28-
getCachedSerializedEnum (enumDeclaration: IEnumDeclaration): ICachedContent<SerializedVersions>|null;
29-
getCachedSerializedFunction (functionDeclaration: IFunctionDeclaration): ICachedContent<SerializedVersions>|null;
3019
getCachedVariable (fileName: string, variableName: string): ICachedContent<IVariableAssignment>|null;
3120
getCachedFunction (fileName: string, functionName: string): ICachedContent<IFunctionDeclaration>|null;
3221
getCachedEnum (fileName: string, enumName: string): ICachedContent<IEnumDeclaration>|null;
@@ -41,11 +30,6 @@ export interface ICache {
4130
getCachedClassIndexer (fileName: string): ICachedContent<ClassIndexer>|null;
4231
getCachedEnumIndexer (fileName: string): ICachedContent<EnumIndexer>|null;
4332
getCachedVariableIndexer (fileName: string): ICachedContent<VariableIndexer>|null;
44-
setCachedSerializedVariable (variable: IVariableAssignment, content: SerializedVersions): void;
45-
setCachedSerializedParameter (parameter: IParameter, content: SerializedVersions): void;
46-
setCachedSerializedClass (variable: IClassDeclaration, content: SerializedVersions): void;
47-
setCachedSerializedEnum (enumDeclaration: IEnumDeclaration, content: SerializedVersions): void;
48-
setCachedSerializedFunction (functionDeclaration: IFunctionDeclaration, content: SerializedVersions): void;
4933
setCachedProp (fileName: string, content: IPropDeclaration): void;
5034
setCachedVariable (fileName: string, content: IVariableAssignment): void;
5135
setCachedEnum (fileName: string, content: IEnumDeclaration): void;
@@ -60,11 +44,6 @@ export interface ICache {
6044
setCachedExportDeclarations (fileName: string, content: IExportDeclaration[]): void;
6145
setCachedEnumIndexer (fileName: string, content: EnumIndexer): void;
6246
setCachedVariableIndexer (fileName: string, content: VariableIndexer): void;
63-
cachedSerializedVariableNeedsUpdate (variable: IVariableAssignment): boolean;
64-
cachedSerializedParameterNeedsUpdate (parameter: IParameter): boolean;
65-
cachedSerializedClassNeedsUpdate (classDeclaration: IClassDeclaration): boolean;
66-
cachedSerializedEnumNeedsUpdate (enumDeclaration: IEnumDeclaration): boolean;
67-
cachedSerializedFunctionNeedsUpdate (functionDeclaration: IFunctionDeclaration): boolean;
6847
cachedVariableNeedsUpdate (variable: IVariableAssignment): boolean;
6948
cachedEnumNeedsUpdate (enumDeclaration: IEnumDeclaration): boolean;
7049
cachedFunctionNeedsUpdate (functionDeclaration: IFunctionDeclaration): boolean;

src/formatter/ArgumentsFormatter.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import {CallExpression, Expression, NewExpression} from "typescript";
2-
import {IValueExpressionGetter} from "../getter/interface/IValueExpressionGetter";
3-
import {IValueResolvedGetter} from "../getter/interface/IValueResolvedGetter";
42
import {IMapper} from "../mapper/interface/IMapper";
5-
import {IArgument, IdentifierMapKind, INonNullableValueable} from "../service/interface/ICodeAnalyzer";
6-
import {ITracer} from "../tracer/interface/ITracer";
3+
import {IArgument, IdentifierMapKind} from "../service/interface/ICodeAnalyzer";
74
import {IArgumentsFormatter} from "./interface/IArgumentsFormatter";
5+
import {IValueableFormatter} from "./interface/IValueableFormatter";
86

97
export class ArgumentsFormatter implements IArgumentsFormatter {
108

119
constructor (private mapper: IMapper,
12-
private tracer: ITracer,
13-
private valueResolvedGetter: IValueResolvedGetter,
14-
private valueExpressionGetter: IValueExpressionGetter) {
10+
private valueableFormatter: IValueableFormatter) {
1511
}
1612

1713
/**
@@ -31,33 +27,12 @@ export class ArgumentsFormatter implements IArgumentsFormatter {
3127
private formatArgument (argument: Expression): IArgument {
3228
const startsAt = argument.pos;
3329
const endsAt = argument.end;
34-
const valueExpression = this.valueExpressionGetter.getValueExpression(argument);
35-
const that = this;
36-
const scope = this.tracer.traceThis(argument);
3730

3831
const map: IArgument = {
3932
___kind: IdentifierMapKind.ARGUMENT,
4033
startsAt,
4134
endsAt,
42-
value: {
43-
expression: valueExpression,
44-
resolving: false,
45-
resolved: undefined,
46-
resolvedPrecompute: undefined,
47-
hasDoneFirstResolve () {
48-
return map.value.resolved !== undefined;
49-
},
50-
resolve () {
51-
if (map.value.expression == null) {
52-
map.value.resolved = map.value.resolvedPrecompute = null;
53-
} else {
54-
const [computed, flattened] = that.valueResolvedGetter.getValueResolved(<INonNullableValueable>map.value, argument, scope);
55-
map.value.resolved = computed;
56-
map.value.resolvedPrecompute = flattened;
57-
}
58-
return map.value.resolved;
59-
}
60-
}
35+
value: this.valueableFormatter.format(argument)
6136
};
6237
// Make the kind non-enumerable.
6338
Object.defineProperty(map, "___kind", {

src/formatter/ExportFormatter.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ export class ExportFormatter extends ModuleFormatter implements IExportFormatter
112112

113113
if (isLiteralExpression(statement.expression)) {
114114
const value = this.valueableFormatter.format(statement.expression);
115-
return {
115+
const obj = {
116116
___kind: IdentifierMapKind.LITERAL,
117117
startsAt: statement.expression.pos,
118118
endsAt: statement.expression.end,
119-
value: () => value.hasDoneFirstResolve() ? value.resolved : value.resolve()
119+
value: () => value.expression == null ? [] : value.expression
120120
};
121+
this.mapper.set(obj, statement.expression);
122+
return obj;
123+
121124
} else {
122125
const identifier = this.nameGetter.getName(statement.expression);
123126
const scope = this.tracer.traceThis(statement);
@@ -168,12 +171,14 @@ export class ExportFormatter extends ModuleFormatter implements IExportFormatter
168171
if (!isCandidate) return null;
169172

170173
const payload = () => {
171-
return {
174+
const obj = {
172175
___kind: IdentifierMapKind.LITERAL,
173176
startsAt: statement.pos,
174177
endsAt: statement.end,
175-
value: () => formatted.value.hasDoneFirstResolve() ? formatted.value.resolved : formatted.value.resolve()
178+
value: () => formatted.value.expression == null ? [] : formatted.value.expression
176179
};
180+
this.mapper.set(obj, statement);
181+
return obj;
177182
};
178183
const sourceFileProperties = this.sourceFilePropertiesGetter.getSourceFileProperties(statement);
179184
const filePath = sourceFileProperties.filePath;
@@ -397,12 +402,14 @@ export class ExportFormatter extends ModuleFormatter implements IExportFormatter
397402
if (clause == null) {
398403
const payload = () => {
399404
const path = modulePath();
400-
return {
405+
const obj = {
401406
___kind: IdentifierMapKind.LITERAL,
402407
startsAt: statement.pos,
403408
endsAt: statement.end,
404-
value: () => this.moduleToNamespacedObjectLiteral(this.languageService.getExportDeclarationsForFile(path, true))
409+
value: () => [this.moduleToNamespacedObjectLiteral(this.languageService.getExportDeclarationsForFile(path, true))]
405410
};
411+
this.mapper.set(obj, statement);
412+
return obj;
406413
};
407414

408415
indexer[NAMESPACE_NAME] = {
@@ -421,12 +428,14 @@ export class ExportFormatter extends ModuleFormatter implements IExportFormatter
421428
const path = modulePath();
422429
const block = this.tracer.traceBlockScopeName(clause);
423430
const clojure = this.tracer.traceClojure(path);
424-
return clojure == null ? {
431+
const obj = clojure == null ? {
425432
___kind: IdentifierMapKind.LITERAL,
426433
startsAt: element.pos,
427434
endsAt: element.end,
428-
value: () => path
435+
value: () => [path]
429436
} : this.tracer.findNearestMatchingIdentifier(element, block, element.name.text, clojure);
437+
this.mapper.set(obj, element);
438+
return obj;
430439
};
431440

432441
indexer[element.name.text] = {

src/formatter/FunctionLikeFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export abstract class FunctionLikeFormatter implements IFunctionLikeFormatter {
7272
returnStatementStartsAt = bodyStatement.expression.pos;
7373
returnStatementEndsAt = bodyStatement.expression.end;
7474
returnStatementContents = fileContents.slice(returnStatementStartsAt, returnStatementEndsAt);
75-
value = this.valueableFormatter.format(bodyStatement.expression);
75+
value = this.valueableFormatter.format(bodyStatement.expression, undefined, undefined);
7676
break;
7777
}
7878
}

0 commit comments

Comments
 (0)