-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathnls-analysis.ts
More file actions
317 lines (263 loc) · 10.2 KB
/
nls-analysis.ts
File metadata and controls
317 lines (263 loc) · 10.2 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as ts from 'typescript';
// ============================================================================
// Types
// ============================================================================
export interface ISpan {
start: ts.LineAndCharacter;
end: ts.LineAndCharacter;
}
export interface ILocalizeCall {
keySpan: ISpan;
key: string;
valueSpan: ISpan;
value: string;
}
// ============================================================================
// AST Collection
// ============================================================================
export const CollectStepResult = Object.freeze({
Yes: 'Yes',
YesAndRecurse: 'YesAndRecurse',
No: 'No',
NoAndRecurse: 'NoAndRecurse'
});
export type CollectStepResult = typeof CollectStepResult[keyof typeof CollectStepResult];
export function collect(node: ts.Node, fn: (node: ts.Node) => CollectStepResult): ts.Node[] {
const result: ts.Node[] = [];
function loop(node: ts.Node) {
const stepResult = fn(node);
if (stepResult === CollectStepResult.Yes || stepResult === CollectStepResult.YesAndRecurse) {
result.push(node);
}
if (stepResult === CollectStepResult.YesAndRecurse || stepResult === CollectStepResult.NoAndRecurse) {
ts.forEachChild(node, loop);
}
}
loop(node);
return result;
}
export function isImportNode(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.ImportDeclaration || node.kind === ts.SyntaxKind.ImportEqualsDeclaration;
}
export function isCallExpressionWithinTextSpanCollectStep(textSpan: ts.TextSpan, node: ts.Node): CollectStepResult {
if (!ts.textSpanContainsTextSpan({ start: node.pos, length: node.end - node.pos }, textSpan)) {
return CollectStepResult.No;
}
return node.kind === ts.SyntaxKind.CallExpression ? CollectStepResult.YesAndRecurse : CollectStepResult.NoAndRecurse;
}
// ============================================================================
// Language Service Host
// ============================================================================
export class SingleFileServiceHost implements ts.LanguageServiceHost {
private file: ts.IScriptSnapshot;
private lib: ts.IScriptSnapshot;
private options: ts.CompilerOptions;
private filename: string;
constructor(options: ts.CompilerOptions, filename: string, contents: string) {
this.options = options;
this.filename = filename;
this.file = ts.ScriptSnapshot.fromString(contents);
this.lib = ts.ScriptSnapshot.fromString('');
}
getCompilationSettings = () => this.options;
getScriptFileNames = () => [this.filename];
getScriptVersion = () => '1';
getScriptSnapshot = (name: string) => name === this.filename ? this.file : this.lib;
getCurrentDirectory = () => '';
getDefaultLibFileName = () => 'lib.d.ts';
readFile(path: string): string | undefined {
if (path === this.filename) {
return this.file.getText(0, this.file.getLength());
}
return undefined;
}
fileExists(path: string): boolean {
return path === this.filename;
}
}
// ============================================================================
// Analysis
// ============================================================================
/**
* Analyzes TypeScript source code to find localize() or localize2() calls.
*/
export function analyzeLocalizeCalls(
contents: string,
functionName: 'localize' | 'localize2'
): ILocalizeCall[] {
const filename = 'file.ts';
const options: ts.CompilerOptions = { noResolve: true };
const serviceHost = new SingleFileServiceHost(options, filename, contents);
const service = ts.createLanguageService(serviceHost);
const sourceFile = ts.createSourceFile(filename, contents, ts.ScriptTarget.ES5, true);
// Find all imports
const imports = collect(sourceFile, n => isImportNode(n) ? CollectStepResult.YesAndRecurse : CollectStepResult.NoAndRecurse);
// import nls = require('vs/nls');
const importEqualsDeclarations = imports
.filter(n => n.kind === ts.SyntaxKind.ImportEqualsDeclaration)
.map(n => n as ts.ImportEqualsDeclaration)
.filter(d => d.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference)
.filter(d => {
const text = (d.moduleReference as ts.ExternalModuleReference).expression.getText();
return text.endsWith(`/nls'`) || text.endsWith(`/nls"`) || text.endsWith(`/nls.js'`) || text.endsWith(`/nls.js"`);
});
// import ... from 'vs/nls';
const importDeclarations = imports
.filter(n => n.kind === ts.SyntaxKind.ImportDeclaration)
.map(n => n as ts.ImportDeclaration)
.filter(d => d.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral)
.filter(d => {
const text = d.moduleSpecifier.getText();
return text.endsWith(`/nls'`) || text.endsWith(`/nls"`) || text.endsWith(`/nls.js'`) || text.endsWith(`/nls.js"`);
})
.filter(d => !!d.importClause && !!d.importClause.namedBindings);
// `nls.localize(...)` calls via namespace import
const nlsLocalizeCallExpressions: ts.CallExpression[] = [];
const namespaceImports = importDeclarations
.filter(d => d.importClause?.namedBindings?.kind === ts.SyntaxKind.NamespaceImport)
.map(d => (d.importClause!.namedBindings as ts.NamespaceImport).name);
const importEqualsNames = importEqualsDeclarations.map(d => d.name);
for (const name of [...namespaceImports, ...importEqualsNames]) {
const refs = service.getReferencesAtPosition(filename, name.pos + 1) ?? [];
for (const ref of refs) {
if (ref.isWriteAccess) {
continue;
}
const calls = collect(sourceFile, n => isCallExpressionWithinTextSpanCollectStep(ref.textSpan, n));
const lastCall = calls[calls.length - 1] as ts.CallExpression | undefined;
if (lastCall &&
lastCall.expression.kind === ts.SyntaxKind.PropertyAccessExpression &&
(lastCall.expression as ts.PropertyAccessExpression).name.getText() === functionName) {
nlsLocalizeCallExpressions.push(lastCall);
}
}
}
// `localize` named imports
const namedImports = importDeclarations
.filter(d => d.importClause?.namedBindings?.kind === ts.SyntaxKind.NamedImports)
.flatMap(d => Array.from((d.importClause!.namedBindings! as ts.NamedImports).elements));
const localizeCallExpressions: ts.CallExpression[] = [];
// Direct named import: import { localize } from 'vs/nls'
for (const namedImport of namedImports) {
const isTarget = namedImport.name.getText() === functionName ||
(namedImport.propertyName && namedImport.propertyName.getText() === functionName);
if (!isTarget) {
continue;
}
const searchName = namedImport.propertyName ? namedImport.name : namedImport.name;
const refs = service.getReferencesAtPosition(filename, searchName.pos + 1) ?? [];
for (const ref of refs) {
if (ref.isWriteAccess) {
continue;
}
const calls = collect(sourceFile, n => isCallExpressionWithinTextSpanCollectStep(ref.textSpan, n));
const lastCall = calls[calls.length - 1] as ts.CallExpression | undefined;
if (lastCall) {
localizeCallExpressions.push(lastCall);
}
}
}
// Combine and deduplicate
const allCalls = [...nlsLocalizeCallExpressions, ...localizeCallExpressions];
const seen = new Set<number>();
const uniqueCalls = allCalls.filter(call => {
const start = call.getStart();
if (seen.has(start)) {
return false;
}
seen.add(start);
return true;
});
// Convert to ILocalizeCall
return uniqueCalls
.filter(e => e.arguments.length > 1)
.sort((a, b) => a.arguments[0].getStart() - b.arguments[0].getStart())
.map(e => {
const args = e.arguments;
return {
keySpan: {
start: ts.getLineAndCharacterOfPosition(sourceFile, args[0].getStart()),
end: ts.getLineAndCharacterOfPosition(sourceFile, args[0].getEnd())
},
key: args[0].getText(),
valueSpan: {
start: ts.getLineAndCharacterOfPosition(sourceFile, args[1].getStart()),
end: ts.getLineAndCharacterOfPosition(sourceFile, args[1].getEnd())
},
value: args[1].getText()
};
});
}
// ============================================================================
// Text Model for patching
// ============================================================================
export class TextModel {
private lines: string[];
private lineEndings: string[];
constructor(contents: string) {
const regex = /\r\n|\r|\n/g;
let index = 0;
let match: RegExpExecArray | null;
this.lines = [];
this.lineEndings = [];
while (match = regex.exec(contents)) {
this.lines.push(contents.substring(index, match.index));
this.lineEndings.push(match[0]);
index = regex.lastIndex;
}
if (contents.length > 0) {
this.lines.push(contents.substring(index, contents.length));
this.lineEndings.push('');
}
}
get(index: number): string {
return this.lines[index];
}
set(index: number, line: string): void {
this.lines[index] = line;
}
get lineCount(): number {
return this.lines.length;
}
/**
* Applies patch(es) to the model.
* Multiple patches must be ordered.
* Does not support patches spanning multiple lines.
*/
apply(span: ISpan, content: string): void {
const startLineNumber = span.start.line;
const endLineNumber = span.end.line;
const startLine = this.lines[startLineNumber] || '';
const endLine = this.lines[endLineNumber] || '';
this.lines[startLineNumber] = [
startLine.substring(0, span.start.character),
content,
endLine.substring(span.end.character)
].join('');
for (let i = startLineNumber + 1; i <= endLineNumber; i++) {
this.lines[i] = '';
}
}
toString(): string {
let result = '';
for (let i = 0; i < this.lines.length; i++) {
result += this.lines[i] + this.lineEndings[i];
}
return result;
}
}
// ============================================================================
// Utilities
// ============================================================================
/**
* Parses a localize key or value expression.
* sourceExpression can be "foo", 'foo', `foo` or { key: 'foo', comment: [...] }
*/
export function parseLocalizeKeyOrValue(sourceExpression: string): string | { key: string; comment?: string[] } {
// eslint-disable-next-line no-eval
return eval(`(${sourceExpression})`);
}