Skip to content

Commit f3aad95

Browse files
committed
Fix tsquery due to typescript upgrade
1 parent 454a9cf commit f3aad95

3 files changed

Lines changed: 6 additions & 5 deletions

File tree

tools/workspace-plugin/src/generators/handler/generator.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ describe('handler generator', () => {
3131
const serverlessConfig = tree.read(`stacks/${options.project}/serverless.ts`);
3232
const contents = serverlessConfig.toString()
3333

34-
const nodes = tsquery.query(contents, `Identifier[name=${options.name}] ~ ObjectLiteralExpression`)
35-
expect(nodes).toHaveLength(1)
34+
const nodes = tsquery.query(contents, `PropertyAssignment:has(Identifier[name=functions]) StringLiteral`)
35+
expect(nodes).toHaveLength(2)
3636

3737
expect(tree.exists(`stacks/${options.project}/src/${options.name}.handler.ts`)).toBeTruthy()
3838
expect(tree.exists(`stacks/${options.project}/src/${options.name}.handler.spec.ts`)).toBeTruthy()

tools/workspace-plugin/src/generators/handler/generator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function handlerGenerator(
1919
generateFiles(tree, join(__dirname, '../shared-files/handle'), projectRoot, { ...options, fileName });
2020

2121
const serverlessConfig = join(projectRoot, 'serverless.ts');
22-
const newContent = tsquery.replace(tree.read(serverlessConfig).toString(), 'Identifier[name=functions] ~ ObjectLiteralExpression', node => {
22+
const newContent = tsquery.replace(tree.read(serverlessConfig).toString(), 'PropertyAssignment:has(Identifier[name=functions]) ObjectLiteralExpression', node => {
2323
const propAst = factory.createPropertyAssignment(
2424
factory.createStringLiteral(options.name),
2525
factory.createObjectLiteralExpression(
@@ -39,7 +39,8 @@ export async function handlerGenerator(
3939
// Kinda hacky as we move into stringy stuff, but it works for what we need
4040
const content = node.getChildren()[1];
4141

42-
return `{${content.getFullText()}${print(propAst)}}`;
42+
const fullText = content.getFullText()
43+
return fullText.includes('{') ? `{${fullText}${print(propAst)}}` : node.getFullText();
4344
});
4445
tree.write(serverlessConfig, newContent);
4546

tools/workspace-plugin/src/generators/stack/generator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('stack generator', () => {
3232
const serverlessConfig = tree.read(`stacks/${options.stack}/serverless.ts`);
3333
const contents = serverlessConfig.toString()
3434

35-
const nodes = tsquery.query(contents, `Identifier[name=${options.handler}] ~ ObjectLiteralExpression`)
35+
const nodes = tsquery.query(contents, `PropertyAssignment:has(Identifier[name=${options.handler}]) StringLiteral`)
3636
expect(nodes).toHaveLength(1)
3737
});
3838
});

0 commit comments

Comments
 (0)