diff --git a/packages/jsii-rosetta/lib/jsii/assemblies.ts b/packages/jsii-rosetta/lib/jsii/assemblies.ts index f3cceb2d2a..4cd977d2eb 100644 --- a/packages/jsii-rosetta/lib/jsii/assemblies.ts +++ b/packages/jsii-rosetta/lib/jsii/assemblies.ts @@ -104,7 +104,7 @@ export function allSnippetSources(assembly: spec.Assembly): AssemblySnippetSourc location, }); } - if (docs.example && exampleLooksLikeSource(docs.example)) { + if (docs.example) { ret.push({ type: 'example', source: docs.example, @@ -142,19 +142,6 @@ export function allTypeScriptSnippets(assemblies: readonly LoadedAssembly[], loo return ret; } -/** - * See if the given source text looks like a code sample - * - * Many @examples for properties are examples of values (ARNs, formatted strings) - * not code samples, which should not be translated - * - * If the value contains whitespace (newline, space) then we'll assume it's a code - * sample. - */ -function exampleLooksLikeSource(text: string) { - return !!WHITESPACE.exec(text.trim()); -} - /** * Replaces the file where the original assembly file *should* be found with a new assembly file. * Recalculates the fingerprint of the assembly to avoid tampering detection. @@ -177,5 +164,3 @@ function _fingerprint(assembly: spec.Assembly): spec.Assembly { const fingerprint = crypto.createHash('sha256').update(JSON.stringify(assembly)).digest('base64'); return { ...assembly, fingerprint }; } - -const WHITESPACE = new RegExp('\\s'); diff --git a/packages/jsii-rosetta/test/commands/extract.test.ts b/packages/jsii-rosetta/test/commands/extract.test.ts index 30bfc9948e..9707734c17 100644 --- a/packages/jsii-rosetta/test/commands/extract.test.ts +++ b/packages/jsii-rosetta/test/commands/extract.test.ts @@ -97,3 +97,39 @@ describe('with cache file', () => { } }); }); + +test('do not ignore example strings', async () => { + // Create an assembly in a temp directory + const otherAssembly = await AssemblyFixture.fromSource( + { + 'index.ts': ` + export class ClassA { + /** + * Some method + * @example x + */ + public someMethod() { + } + } + `, + }, + { + name: 'my_assembly', + jsii: DUMMY_ASSEMBLY_TARGETS, + }, + ); + try { + const outputFile = path.join(otherAssembly.directory, 'test.tabl.json'); + await extract.extractSnippets([otherAssembly.directory], { + outputFile, + ...defaultExtractOptions, + }); + + const tablet = await LanguageTablet.fromFile(outputFile); + expect(tablet.count).toEqual(1); + const tr = tablet.tryGetSnippet(tablet.snippetKeys[0]); + expect(tr?.originalSource.source).toEqual('x'); + } finally { + await assembly.cleanup(); + } +});