/
template-expression-resolver.ts
386 lines (361 loc) · 14 KB
/
template-expression-resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import ts from 'typescript';
import { location2pos, pos2location } from '../string-util';
import { findNode } from './utilily-functions';
import { ComputePosition, ResolvedTemplateInfo, ResolveResult, ResolveErrorInfo } from './types';
const last: ComputePosition = (pos: number) => {
throw new Error('invalid range: ' + pos);
};
export function createResultForNoSubstitution(
node: ts.NoSubstitutionTemplateLiteral,
fileName: string,
): ResolvedTemplateInfo {
const textLength = node.text.length;
const start = node.end - textLength - (node.getText().endsWith('`') ? 1 /* , '`'.length , */ : 0);
return {
combinedText: node.text,
getInnerPosition(pos: number) {
if (pos < start) {
throw new Error('invalid range: ' + pos);
} else if (pos > start + textLength) {
throw new Error('invalid range: ' + pos);
}
return { fileName, pos: pos - start };
},
getSourcePosition(pos: number) {
if (pos < 0) {
throw new Error('invalid range: ' + pos);
} else if (pos > textLength) {
throw new Error('invalid range: ' + pos);
}
return { fileName, pos: pos + start };
},
convertInnerLocation2InnerPosition: location2pos.bind(null, node.text),
convertInnerPosition2InnerLocation: pos2location.bind(null, node.text),
};
}
function createComputePositionsForTemplateHead(node: ts.TemplateHead, fileName: string) {
const textLength = node.text.length;
const start = node.end - textLength - (node.getText().endsWith('${') ? 2 /* , '${'.length , */ : 0);
const getInnerPosition = (pos: number, next: ComputePosition) => {
if (pos < start) {
throw new Error('invalid range: ' + pos);
} else if (pos > start + textLength) {
return next(pos);
}
return { fileName, pos: pos - start };
};
const getSourcePosition = (pos: number, next: ComputePosition) => {
if (pos < 0) {
throw new Error('invalid range: ' + pos);
} else if (pos > textLength) {
return next(pos);
}
return { fileName, pos: pos + start };
};
return [getInnerPosition, getSourcePosition];
}
function createComputePositionsForTemplateSpan(node: ts.TemplateSpan, fileName: string, headLength: number = 0) {
const start = node.literal.pos + 1; /* , '}'.length , */
const textLength = node.literal.text.length;
const end = start + node.literal.text.length;
const expressionStart = node.expression.pos;
const getInnerPosition = (pos: number, next: ComputePosition) => {
if (pos < start) {
throw new Error('invalid range: ' + pos);
} else if (pos >= end) {
return next(pos);
}
return { fileName, pos: pos - start + headLength };
};
const getSourcePosition = (pos: number, next: ComputePosition) => {
if (pos < headLength) {
return { fileName, pos: expressionStart, isInOtherExpression: true };
} else if (pos > headLength + textLength - (textLength ? 1 : 0)) {
return next(pos);
}
return { fileName, pos: pos + start - headLength };
};
return [getInnerPosition, getSourcePosition];
}
class ResultCache<S> {
private _cacheMap = new Map<ts.Node, S>();
constructor(private _maxSize: number = 200) {}
set(key: ts.Node, value: S) {
this._cacheMap.set(key, value);
if (this._cacheMap.size > this._maxSize) {
const lru = this._cacheMap.keys().next();
this._cacheMap.delete(lru.value);
}
}
get(key: ts.Node) {
const result = this._cacheMap.get(key);
if (!result) return;
return result;
}
has(key: ts.Node) {
return this._cacheMap.has(key);
}
touch(key: ts.Node) {
const result = this._cacheMap.get(key);
if (!result) return;
this._cacheMap.delete(key);
this._cacheMap.set(key, result);
}
del(key: ts.Node) {
this._cacheMap.delete(key);
}
}
export class TemplateExpressionResolver {
/** @internal **/
readonly _resultCache = new ResultCache<{
result: ResolvedTemplateInfo;
dependencyVersions: { fileName: string; version: string }[];
}>();
/** @internal **/
readonly _stringValueCache = new ResultCache<{
result: string | undefined;
dependencyVersions: { fileName: string; version: string }[];
}>();
logger: (msg: string) => void = () => {};
constructor(
private readonly _langService: ts.LanguageService,
private readonly _getFileVersion: (fileName: string) => string,
private readonly _isExcluded: (fileName: string) => boolean,
) {}
resolve(
fileName: string,
node: ts.TaggedTemplateExpression | ts.NoSubstitutionTemplateLiteral | ts.TemplateExpression,
): ResolveResult {
const cacheValue = this._resultCache.get(node);
if (cacheValue) {
if (cacheValue.dependencyVersions.every(dep => this._getFileVersion(dep.fileName) === dep.version)) {
this._resultCache.touch(node);
this.logger('Use cache value: ' + cacheValue.result.combinedText);
return { resolvedInfo: cacheValue.result, resolveErrors: [] };
} else {
this._resultCache.del(node);
}
}
const setValueToCache = (resolvedInfo: ResolvedTemplateInfo, dependencies = [fileName]) => {
const versions = [...new Set(dependencies)].map(fileName => ({
fileName,
version: this._getFileVersion(fileName),
}));
this._resultCache.set(node, { result: resolvedInfo, dependencyVersions: versions });
return {
resolvedInfo,
resolveErrors: [],
};
};
if (ts.isNoSubstitutionTemplateLiteral(node)) {
return setValueToCache(createResultForNoSubstitution(node, fileName));
}
let template: ts.TemplateExpression;
if (ts.isTemplateExpression(node)) {
template = node;
} else if (ts.isTemplateExpression(node.template)) {
template = node.template;
} else if (ts.isNoSubstitutionTemplateLiteral(node.template)) {
return setValueToCache(createResultForNoSubstitution(node.template, fileName));
} else {
return {
resolveErrors: [],
};
}
const head = template.head;
const [getInnerPosForHead, getSourcePosForHead] = createComputePositionsForTemplateHead(head, fileName);
let headLength = head.text.length;
let dependencies = [fileName];
const texts = [head.text];
const getInnerPositions = [getInnerPosForHead];
const getSourcePositions = [getSourcePosForHead];
const resolveErrors: ResolveErrorInfo[] = [];
for (const spanNode of template.templateSpans) {
const { text: stringForSpan, dependencies: childDeps } = this._getValueAsString(
fileName,
spanNode.expression,
dependencies,
);
if (!stringForSpan) {
resolveErrors.push({
fileName,
start: spanNode.expression.getStart(),
end: spanNode.expression.getEnd(),
});
continue;
}
headLength += stringForSpan.length;
texts.push(stringForSpan);
const [getInnerPositionsForSpan, getSourcePositionForSpan] = createComputePositionsForTemplateSpan(
spanNode,
fileName,
headLength,
);
getInnerPositions.unshift(getInnerPositionsForSpan);
getSourcePositions.unshift(getSourcePositionForSpan);
headLength += spanNode.literal.text.length;
texts.push(spanNode.literal.text);
dependencies = [...dependencies, ...childDeps];
}
if (resolveErrors.length > 0) {
return {
resolveErrors,
};
}
const combinedText = texts.join('');
return setValueToCache(
{
combinedText,
getInnerPosition: getInnerPositions.reduce((acc: ComputePosition, fn) => (pos: number) => fn(pos, acc), last),
getSourcePosition: getSourcePositions.reduce((acc: ComputePosition, fn) => (pos: number) => fn(pos, acc), last),
convertInnerLocation2InnerPosition: location2pos.bind(null, combinedText),
convertInnerPosition2InnerLocation: pos2location.bind(null, combinedText),
} as ResolvedTemplateInfo,
dependencies,
);
}
update(
target: ResolvedTemplateInfo,
innerPositionRangeToChange: { start: number; end: number },
text: string = '',
): ResolvedTemplateInfo {
const {
combinedText: originalText,
getInnerPosition: originalGetInnerPosition,
getSourcePosition: originalGetSourcePosition,
} = target;
const headText = originalText.slice(0, innerPositionRangeToChange.start);
const tailText = originalText.slice(innerPositionRangeToChange.end, originalText.length);
const combinedText = headText + text + tailText;
const getInnerPosition: ComputePosition = (pos: number) => {
const x = originalGetInnerPosition(pos);
if (x.pos < innerPositionRangeToChange.start) {
return x;
} else if (innerPositionRangeToChange.start <= x.pos && x.pos < innerPositionRangeToChange.end) {
return {
...x,
pos: innerPositionRangeToChange.start,
};
} else {
return {
...x,
pos: x.pos - (innerPositionRangeToChange.end - innerPositionRangeToChange.start) + text.length,
};
}
};
const getSourcePosition: ComputePosition = (pos: number) => {
if (pos < innerPositionRangeToChange.start) {
return originalGetSourcePosition(pos);
} else if (innerPositionRangeToChange.start <= pos && pos < innerPositionRangeToChange.start + text.length) {
return originalGetSourcePosition(innerPositionRangeToChange.start);
} else {
return originalGetSourcePosition(
pos + (innerPositionRangeToChange.end - innerPositionRangeToChange.start) - text.length,
);
}
};
return {
combinedText,
getInnerPosition,
getSourcePosition,
convertInnerLocation2InnerPosition: location2pos.bind(null, combinedText),
convertInnerPosition2InnerLocation: pos2location.bind(null, combinedText),
};
}
private _getValueAsString(
fileName: string,
node: ts.Node,
dependencies = [fileName],
): { text?: string; dependencies: string[] } {
const cacheValue = this._stringValueCache.get(node);
if (cacheValue) {
if (cacheValue.dependencyVersions.every(dep => this._getFileVersion(dep.fileName) === dep.version)) {
this._stringValueCache.touch(node);
this.logger('Use cache value: ' + cacheValue.result);
return { text: cacheValue.result, dependencies: cacheValue.dependencyVersions.map(dep => dep.fileName) };
} else {
this._stringValueCache.del(node);
}
}
const setValueToCache = ({ text, dependencies }: { text?: string; dependencies: string[] }) => {
this._stringValueCache.set(node, {
result: text,
dependencyVersions: [...new Set(dependencies)].map(fileName => ({
fileName,
version: this._getFileVersion(fileName),
})),
});
return { text, dependencies };
};
const getValueForTemplateExpression = (node: ts.TemplateExpression, dependencies: string[]) => {
const texts = [node.head.text];
let newDependenciees = [...dependencies];
for (const span of node.templateSpans) {
const { text: stringForSpan, dependencies: childDepes } = this._getValueAsString(
fileName,
span.expression,
dependencies,
);
if (!stringForSpan) return { dependencies };
texts.push(stringForSpan);
texts.push(span.literal.text);
newDependenciees = [...newDependenciees, ...childDepes];
}
return { text: texts.join(''), dependencies: newDependenciees };
};
if (ts.isStringLiteral(node)) {
return setValueToCache({ text: node.text, dependencies });
} else if (ts.isNoSubstitutionTemplateLiteral(node)) {
return setValueToCache({ text: node.text, dependencies });
} else if (ts.isIdentifier(node)) {
let currentFileName = fileName;
let currentNode: ts.Node = node;
while (true) {
const defs = this._langService.getDefinitionAtPosition(currentFileName, currentNode.getStart());
if (!defs || !defs[0]) return { dependencies };
const def = defs[0];
if (this._isExcluded(def.fileName)) return { dependencies };
const src = this._langService.getProgram()!.getSourceFile(def.fileName);
if (!src) return { dependencies };
const found = findNode(src, def.textSpan.start);
if (!found || !found.parent) return { dependencies };
currentFileName = def.fileName;
if (ts.isVariableDeclaration(found.parent) && found.parent.initializer) {
currentNode = found.parent.initializer;
} else if (ts.isPropertyDeclaration(found.parent) && found.parent.initializer) {
currentNode = found.parent.initializer;
} else if (ts.isPropertyAssignment(found.parent)) {
currentNode = found.parent.initializer;
} else if (ts.isShorthandPropertyAssignment(found.parent)) {
currentNode = found;
} else {
return { dependencies };
}
if (ts.isIdentifier(currentNode)) {
continue;
}
return setValueToCache(
this._getValueAsString(currentFileName, currentNode, [...dependencies, currentFileName]),
);
}
} else if (ts.isPropertyAccessExpression(node)) {
return setValueToCache(this._getValueAsString(fileName, node.name, dependencies));
} else if (ts.isTaggedTemplateExpression(node)) {
if (ts.isNoSubstitutionTemplateLiteral(node.template)) {
return setValueToCache({ text: node.template.text, dependencies });
} else {
return setValueToCache(getValueForTemplateExpression(node.template, dependencies));
}
} else if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.arguments.length > 0) {
const firstArgNode = node.arguments[0];
if (ts.isNoSubstitutionTemplateLiteral(firstArgNode)) {
return setValueToCache({ text: firstArgNode.text, dependencies });
} else if (ts.isTemplateExpression(firstArgNode)) {
return setValueToCache(getValueForTemplateExpression(firstArgNode, dependencies));
}
} else if (ts.isTemplateExpression(node)) {
return setValueToCache(getValueForTemplateExpression(node, dependencies));
}
return { dependencies };
}
}