Skip to content

Commit

Permalink
fix(rosetta): allow only property assignments in object literals (#3065)
Browse files Browse the repository at this point in the history
We'll report on anything else that JavaScript allows you to do (methods, getters, etc).

Fixes #3061.

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
rix0rrr committed Oct 13, 2021
1 parent 26c95f5 commit c783ab7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/jsii-rosetta/lib/languages/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export abstract class DefaultVisitor<C> implements AstHandler<C> {
* - It's not a struct (render as key-value map)
*/
public objectLiteralExpression(node: ts.ObjectLiteralExpression, context: AstRenderer<C>): OTree {
// If any of the elements of the objectLiteralExpression are not a literal property
// assignment, report them. We can't support those.
const unsupported = node.properties.filter(
(p) => !ts.isPropertyAssignment(p) && !ts.isShorthandPropertyAssignment(p),
);
for (const unsup of unsupported) {
context.report(unsup, `Use of ${ts.SyntaxKind[unsup.kind]} in an object literal is not supported.`);
}

const type = context.inferredTypeOfExpression(node);

let isUnknownType = !type;
Expand Down
20 changes: 20 additions & 0 deletions packages/jsii-rosetta/test/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,23 @@ test('rejects ?? operator', () => {

expect(subject.diagnostics[0].messageText).toContain('QuestionQuestionToken');
});

test('rejects function declarations in object literals', () => {
const snippet: TypeScriptSnippet = {
completeSource: 'const x = { method() { return 1; } }',
where: '@aws-cdk.aws-apigateway-README-snippet4',
visibleSource: 'const x = { method() { return 1; } }',
parameters: { lit: 'test/integ.restapi-import.lit.ts' },
strict: false,
};

// WHEN
const subject = new SnippetTranslator(snippet, {
includeCompilerDiagnostics: true,
});
subject.renderUsing(new PythonVisitor());

expect(subject.diagnostics[0].messageText).toContain(
'Use of MethodDeclaration in an object literal is not supported',
);
});

0 comments on commit c783ab7

Please sign in to comment.