diff --git a/packages/jsii-rosetta/lib/commands/extract.ts b/packages/jsii-rosetta/lib/commands/extract.ts index b43ac224b8..052c187668 100644 --- a/packages/jsii-rosetta/lib/commands/extract.ts +++ b/packages/jsii-rosetta/lib/commands/extract.ts @@ -99,7 +99,7 @@ export async function extractSnippets( translator.addTabletsToCache(...Object.values(await loadAllDefaultTablets(assemblies))); if (translator.hasCache()) { - const { translations, remaining } = translator.readFromCache(snippets); + const { translations, remaining } = translator.readFromCache(snippets, true, options.includeCompilerDiagnostics); logging.info(`Reused ${translations.length} translations from cache`); snippets = remaining; } diff --git a/packages/jsii-rosetta/lib/rosetta-translator.ts b/packages/jsii-rosetta/lib/rosetta-translator.ts index 7da1fc8aa9..b5f8c7d0ee 100644 --- a/packages/jsii-rosetta/lib/rosetta-translator.ts +++ b/packages/jsii-rosetta/lib/rosetta-translator.ts @@ -83,14 +83,15 @@ export class RosettaTranslator { * * Will remove the cached snippets from the input array. */ - public readFromCache(snippets: TypeScriptSnippet[], addToTablet = true): ReadFromCacheResults { + public readFromCache(snippets: TypeScriptSnippet[], addToTablet = true, compiledOnly = false): ReadFromCacheResults { const remaining = [...snippets]; const translations = new Array(); let i = 0; while (i < remaining.length) { const fromCache = tryReadFromCache(remaining[i], this.cache, this.fingerprinter); - if (fromCache) { + // If compiledOnly is set, do not consider cached snippets that do not compile + if (fromCache && (!compiledOnly || fromCache.snippet.didCompile)) { if (addToTablet) { this.tablet.addSnippet(fromCache); } diff --git a/packages/jsii-rosetta/test/commands/extract.test.ts b/packages/jsii-rosetta/test/commands/extract.test.ts index d15bd55df0..74ff8da273 100644 --- a/packages/jsii-rosetta/test/commands/extract.test.ts +++ b/packages/jsii-rosetta/test/commands/extract.test.ts @@ -258,6 +258,75 @@ describe('with cache file', () => { }); }); +describe('non-compiling cached examples', () => { + let otherAssembly: TestJsiiModule; + let cacheToFile: string; + beforeEach(async () => { + // Create an assembly in a temp directory + otherAssembly = await TestJsiiModule.fromSource( + { + 'index.ts': ` + export class ClassA { + /** + * Some method + * @example x + */ + public someMethod() { + } + } + `, + }, + { + name: 'my_assembly', + jsii: DUMMY_JSII_CONFIG, + }, + ); + + // add non-compiling snippet to cache + cacheToFile = path.join(otherAssembly.moduleDirectory, 'test.tabl.json'); + await extract.extractSnippets([otherAssembly.moduleDirectory], { + cacheToFile, + includeCompilerDiagnostics: true, + validateAssemblies: false, + }); + + const tablet = await LanguageTablet.fromFile(cacheToFile); + expect(tablet.count).toEqual(1); + const tr = tablet.tryGetSnippet(tablet.snippetKeys[0]); + expect(tr?.snippet.didCompile).toBeFalsy(); + }); + + afterEach(async () => assembly.cleanup()); + + test('are ignored with strict mode', async () => { + // second run of extract snippets should still evaluate the snippet + // even though it is present in the cache + const translationFunction = jest.fn().mockResolvedValue({ diagnostics: [], translatedSnippets: [] }); + await extract.extractSnippets([otherAssembly.moduleDirectory], { + cacheToFile, + cacheFromFile: cacheToFile, + includeCompilerDiagnostics: true, + validateAssemblies: false, + translatorFactory: (o) => new MockTranslator(o, translationFunction), + }); + + expect(translationFunction).toHaveBeenCalledTimes(1); + }); + + test('are utilized with strict mode off', async () => { + const translationFunction = jest.fn().mockResolvedValue({ diagnostics: [], translatedSnippets: [] }); + await extract.extractSnippets([otherAssembly.moduleDirectory], { + cacheToFile, + cacheFromFile: cacheToFile, + includeCompilerDiagnostics: false, + validateAssemblies: false, + translatorFactory: (o) => new MockTranslator(o, translationFunction), + }); + + expect(translationFunction).toHaveBeenCalledTimes(0); + }); +}); + test('do not ignore example strings', async () => { // Create an assembly in a temp directory const otherAssembly = await TestJsiiModule.fromSource(