Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rosetta): stop skipping example values #3128

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions packages/jsii-rosetta/lib/jsii/assemblies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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');
36 changes: 36 additions & 0 deletions packages/jsii-rosetta/test/commands/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});