Skip to content

Commit

Permalink
fix: Make transformer convert callExpression matching tag condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Mar 15, 2024
1 parent 5026b77 commit 951dd5b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 9 deletions.
60 changes: 60 additions & 0 deletions src/transformer/__snapshots__/transformer.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ exports[`transformer GraphQL document transformation should ignore TemplateExpre
"
`;

exports[`transformer GraphQL document transformation should ignore arguments which are not template literal in CallExpression node even if the node matches tag name 1`] = `
"const query = hoge('abc', 100);
"
`;

exports[`transformer GraphQL document transformation should ignore template argument in CallExpression when the node does not matche tag name 1`] = `
"const query = hoge(\`abc\`);
"
`;

exports[`transformer GraphQL document transformation should transform NoSubstitutionTemplateLiteral 1`] = `
"const query = {
kind: "Document",
Expand Down Expand Up @@ -115,6 +125,56 @@ exports[`transformer GraphQL document transformation should transform TemplateEx
"
`;

exports[`transformer GraphQL document transformation should transform first template argument in CallExpression when the node matches tag name 1`] = `
"const query = {
kind: "Document",
definitions: [{
kind: "OperationDefinition",
operation: "query",
variableDefinitions: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [{
kind: "Field",
name: {
kind: "Name",
value: "hello"
},
arguments: [],
directives: []
}]
}
}]
};
"
`;

exports[`transformer GraphQL document transformation should transform first template expression argument in CallExpression when the node matches tag name 1`] = `
"const query = {
kind: "Document",
definitions: [{
kind: "OperationDefinition",
operation: "query",
variableDefinitions: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [{
kind: "Field",
name: {
kind: "Name",
value: "hello"
},
arguments: [],
directives: []
}]
}
}]
};
"
`;

exports[`transformer GraphQL document transformation should transform inner document with documentTransformers 1`] = `
"const query = {
kind: "Document",
Expand Down
72 changes: 70 additions & 2 deletions src/transformer/transformer.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ts from 'typescript';
import { DocumentNode, parse, visit } from 'graphql';

import { parseTagConfig } from '../ts-ast-util';
import { parseTagConfig, type TagConfig } from '../ts-ast-util';
import { getTransformer } from './transformer';

function transformAndPrint({
Expand All @@ -13,7 +13,7 @@ function transformAndPrint({
documentTransformers = [],
enabled = true,
}: {
tag?: string;
tag?: TagConfig;
target: 'text' | 'object';
docContent: string;
tsContent: string;
Expand Down Expand Up @@ -153,6 +153,74 @@ describe('transformer', () => {
).toMatchSnapshot();
});

it('should transform first template argument in CallExpression when the node matches tag name', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge(\`abc\`);
`,
tag: { name: 'hoge', ignoreFunctionCallExpression: false },
docContent: `
query {
hello
}
`,
target: 'object',
}),
).toMatchSnapshot();
});

it('should transform first template expression argument in CallExpression when the node matches tag name', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge(\`abc\${def}\`);
`,
tag: { name: 'hoge', ignoreFunctionCallExpression: false },
docContent: `
query {
hello
}
`,
target: 'object',
}),
).toMatchSnapshot();
});

it('should ignore template argument in CallExpression when the node does not matche tag name', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge(\`abc\`);
`,
tag: { name: 'foo', ignoreFunctionCallExpression: false },
docContent: `
query {
hello
}
`,
target: 'object',
}),
).toMatchSnapshot();
});

it('should ignore arguments which are not template literal in CallExpression node even if the node matches tag name', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge('abc', 100);
`,
tag: { name: 'hoge', ignoreFunctionCallExpression: false },
docContent: `
query {
hello
}
`,
target: 'object',
}),
).toMatchSnapshot();
});

it('should transform to 0 literal when removeFragmentDefinitions: true and document has only fragments', () => {
expect(
transformAndPrint({
Expand Down
12 changes: 5 additions & 7 deletions src/transformer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,16 @@ export function getTransformer({
return (ctx: ts.TransformationContext) => {
const visit = (node: ts.Node): ts.Node | undefined => {
if (!getEnabled()) return node;
if (tag.names.length > 1) return node;
let templateNode: ts.NoSubstitutionTemplateLiteral | ts.TemplateExpression | undefined = undefined;

if (ts.isImportDeclaration(node) && tag.names[0]) {
return removeAliasFromImportDeclaration(node, tag.names[0]);
if (ts.isImportDeclaration(node) && tag.names.length > 0) {
return removeAliasFromImportDeclaration(node, tag.names);
}

if (
ts.isTaggedTemplateExpression(node) &&
(!tag.names.length || !!getTemplateNodeUnder(node, { ...tag, allowFunctionCallExpression: false }))
) {
if (ts.isTaggedTemplateExpression(node) && (!tag.names.length || !!getTemplateNodeUnder(node, tag))) {
templateNode = node.template;
} else if (ts.isCallExpression(node) && !!getTemplateNodeUnder(node, tag)) {
templateNode = node.arguments[0] as ts.TemplateLiteral;
} else if (tag.allowNotTaggedTemplate && ts.isNoSubstitutionTemplateLiteral(node)) {
templateNode = node;
} else if (tag.allowNotTaggedTemplate && ts.isTemplateExpression(node)) {
Expand Down

0 comments on commit 951dd5b

Please sign in to comment.