-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathrosetta-reader.ts
More file actions
335 lines (300 loc) · 10.8 KB
/
rosetta-reader.ts
File metadata and controls
335 lines (300 loc) · 10.8 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import * as spec from '@jsii/spec';
import * as path from 'path';
import { allTypeScriptSnippets } from './jsii/assemblies';
import { TargetLanguage } from './languages';
import * as logging from './logging';
import { transformMarkdown } from './markdown/markdown';
import { MarkdownRenderer } from './markdown/markdown-renderer';
import { ReplaceTypeScriptTransform } from './markdown/replace-typescript-transform';
import { CodeBlock } from './markdown/types';
import {
SnippetParameters,
TypeScriptSnippet,
updateParameters,
ApiLocation,
typeScriptSnippetFromSource,
} from './snippet';
import { snippetKey } from './tablets/key';
import { DEFAULT_TABLET_NAME, LanguageTablet, Translation } from './tablets/tablets';
import { Translator } from './translate';
import { commentToken, pathExists, printDiagnostics } from './util';
export enum UnknownSnippetMode {
/**
* Return the snippet as given (untranslated)
*/
VERBATIM = 'verbatim',
/**
* Live-translate the snippet as best as we can
*/
TRANSLATE = 'translate',
/**
* Throw an error if this occurs
*/
FAIL = 'fail',
}
export interface RosettaOptions {
/**
* Whether or not to live-convert samples
*
* @default UnknownSnippetMode.VERBATIM
*/
readonly unknownSnippets?: UnknownSnippetMode;
/**
* Target languages to use for live conversion
*
* @default All languages
*/
readonly targetLanguages?: readonly TargetLanguage[];
/**
* Whether to include compiler diagnostics in the compilation results.
*/
readonly includeCompilerDiagnostics?: boolean;
/**
* Whether this Rosetta should operate in "loose" mode, where missing literate
* source files and missing fixtures are ignored instead of failing.
*
* @default false
*/
readonly loose?: boolean;
/**
* Adds a disclaimer to start of snippet if it did not compile.
*
* @default false
*/
readonly prefixDisclaimer?: boolean;
}
/**
* Entry point class for consumers of Rosetta tablets (primarily: pacmak)
*
* Rosetta can work in one of two modes:
*
* 1. Live translation of snippets.
* 2. Read translations from a pre-translated tablet (prepared using `jsii-rosetta extract` command).
*
* The second method affords more control over the precise circumstances of
* sample compilation and is recommended, but the first method will do
* when the second one is not necessary.
*/
export class RosettaTabletReader {
/**
* Newly translated samples
*
* In case live translation has been enabled, all samples that have been translated on-the-fly
* are added to this tablet.
*/
public readonly liveTablet = new LanguageTablet();
private readonly loadedTablets: LanguageTablet[] = [];
private readonly extractedSnippets = new Map<string, TypeScriptSnippet>();
private readonly translator: Translator;
private readonly loose: boolean;
private readonly unknownSnippets: UnknownSnippetMode;
private readonly _prefixDisclaimer: boolean;
public constructor(private readonly options: RosettaOptions = {}) {
this.loose = !!options.loose;
this.unknownSnippets = options.unknownSnippets ?? UnknownSnippetMode.VERBATIM;
this.translator = new Translator(options.includeCompilerDiagnostics ?? false);
this._prefixDisclaimer = options.prefixDisclaimer ?? false;
}
/**
* Diagnostics encountered while doing live translation
*/
public get diagnostics() {
return this.translator.diagnostics;
}
/**
* Load a tablet as a source for translateable snippets
*
* Note: the snippets loaded from this tablet will NOT be validated for
* their fingerprints or translator versions! If a matching snippet is found
* in the tablet, it will always be returned, whether or not it is stale.
*/
public async loadTabletFromFile(tabletFile: string) {
const tablet = new LanguageTablet();
await tablet.load(tabletFile);
this.addTablet(tablet);
}
/**
* Directly add a tablet to the list of tablets to load translations from
*/
public addTablet(tablet: LanguageTablet) {
this.loadedTablets.push(tablet);
}
/**
* Add an assembly
*
* If a default tablet file is found in the assembly's directory, it will be
* loaded (and assumed to contain a complete list of translated snippets for
* this assembly already).
*
* Otherwise, if live conversion is enabled, the snippets in the assembly
* become available for live translation later. This is necessary because we probably
* need to fixturize snippets for successful compilation, and the information
* pacmak sends our way later on is not going to be enough to do that.
*/
public async addAssembly(assembly: spec.Assembly, assemblyDir: string) {
const defaultTablet = path.join(assemblyDir, DEFAULT_TABLET_NAME);
if (await pathExists(defaultTablet)) {
try {
await this.loadTabletFromFile(defaultTablet);
return;
} catch (e: any) {
logging.warn(`Error loading ${defaultTablet}: ${e.message}. Skipped.`);
}
}
// Inventarize the snippets from this assembly, but only if there's a chance
// we're going to need them.
if (this.unknownSnippets === UnknownSnippetMode.TRANSLATE) {
for (const tsnip of await allTypeScriptSnippets([{ assembly, directory: assemblyDir }], this.loose)) {
this.extractedSnippets.set(snippetKey(tsnip), tsnip);
}
}
}
/**
* Translate the given snippet for the given target language
*
* This will either:
*
* - Find an existing translation in a tablet and return that, if available.
* - Otherwise, find a fixturized version of this snippet in an assembly that
* was loaded beforehand, and translate it on-the-fly. Finding the fixture
* will be based on the snippet key, which consists of a hash of the
* visible source and the API location.
* - Otherwise, translate the snippet as-is (without fixture information).
*
* This will do and store a full conversion of the given snippet, even if it only
* returns one language. Subsequent retrievals for the same snippet in other
* languages will reuse the translation from cache.
*
* If you are calling this for the side effect of adding translations to the live
* tablet, you only need to do that for one language.
*/
public translateSnippet(source: TypeScriptSnippet, targetLang: TargetLanguage): Translation | undefined {
// Look for it in loaded tablets (or previous conversions)
for (const tab of this.allTablets) {
const ret = tab.lookup(source, targetLang);
if (ret !== undefined) {
return this.prefixDisclaimer(ret, this._prefixDisclaimer);
}
}
if (this.unknownSnippets === UnknownSnippetMode.VERBATIM) {
return this.prefixDisclaimer(
{
language: targetLang,
source: source.visibleSource,
},
this._prefixDisclaimer,
);
}
if (this.unknownSnippets === UnknownSnippetMode.FAIL) {
const message = [
'The following snippet was not found in any of the loaded tablets:',
source.visibleSource,
`Location: ${JSON.stringify(source.location)}`,
`Language: ${targetLang}`,
].join('\n');
throw new Error(message);
}
if (this.options.targetLanguages && !this.options.targetLanguages.includes(targetLang)) {
throw new Error(
`Rosetta configured for live conversion to ${this.options.targetLanguages.join(
', ',
)}, but requested ${targetLang}`,
);
}
// See if we can find a fixturized version of this snippet. If so, use that do the live
// conversion.
const extracted = this.extractedSnippets.get(snippetKey(source));
if (extracted !== undefined) {
const snippet = this.translator.translate(extracted, this.options.targetLanguages);
this.liveTablet.addSnippet(snippet);
return this.prefixDisclaimer(snippet.get(targetLang), this._prefixDisclaimer);
}
// Try to live-convert it as-is.
const snippet = this.translator.translate(source, this.options.targetLanguages);
this.liveTablet.addSnippet(snippet);
return this.prefixDisclaimer(snippet.get(targetLang), this._prefixDisclaimer);
}
/**
* Translate a snippet found in the "@ example" section of a jsii assembly
*
* Behaves exactly like `translateSnippet`, so see that method for documentation.
*/
public translateExample(
apiLocation: ApiLocation,
example: string,
targetLang: TargetLanguage,
strict: boolean,
compileDirectory = process.cwd(),
): Translation {
const location = { api: apiLocation, field: { field: 'example' } } as const;
const snippet = typeScriptSnippetFromSource(example, location, strict, {
[SnippetParameters.$COMPILATION_DIRECTORY]: compileDirectory,
});
const translated = this.translateSnippet(snippet, targetLang);
return translated ?? { language: 'typescript', source: example };
}
/**
* Translate all TypeScript snippets found in a block of Markdown text
*
* For each snippet, behaves exactly like `translateSnippet`, so see that
* method for documentation.
*/
public translateSnippetsInMarkdown(
apiLocation: ApiLocation,
markdown: string,
targetLang: TargetLanguage,
strict: boolean,
translationToCodeBlock: (x: Translation) => CodeBlock = id,
compileDirectory = process.cwd(),
): string {
return transformMarkdown(
markdown,
new MarkdownRenderer(),
new ReplaceTypeScriptTransform(apiLocation, strict, (tsSnip) => {
const translated = this.translateSnippet(
updateParameters(tsSnip, {
[SnippetParameters.$COMPILATION_DIRECTORY]: compileDirectory,
}),
targetLang,
);
if (!translated) {
return undefined;
}
return translationToCodeBlock(translated);
}),
);
}
public printDiagnostics(stream: NodeJS.WritableStream) {
printDiagnostics(this.diagnostics, stream);
}
public get hasErrors() {
return this.diagnostics.some((d) => d.isError);
}
private get allTablets(): LanguageTablet[] {
return [...this.loadedTablets, this.liveTablet];
}
/**
* Adds a disclaimer to the front of the example if the prefixDisclaimer
* flag is set and we know it does not compile.
*/
private prefixDisclaimer(translation: Translation | undefined, prefixDisclaimer: boolean): Translation | undefined {
if (!prefixDisclaimer || translation?.didCompile !== false) {
return translation;
}
const comment = commentToken(translation.language);
const disclaimer = 'Example automatically generated from non-compiling source. May contain errors.';
return {
...translation,
source: `${comment} ${disclaimer}\n${translation.source}`,
};
}
}
function id(x: Translation) {
return x;
}
/**
* Backwards compatibility
*
* @deprecated use RosettaTabletReader instead
*/
export class Rosetta extends RosettaTabletReader {}