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

feat: Resolve interpolation referring call expression #1234

Merged
merged 1 commit into from Mar 15, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -68,3 +68,20 @@ exports[`resolve string combinination pattern should return combined string with
...Foo
}"
`;

exports[`resolve string combinination pattern should return combined string with template in call expression 1`] = `
"


fragment Foo on Hoge {
name
}

fragment Piyo on Hoge {
...Foo
}

query {
...Piyo
}"
`;
38 changes: 38 additions & 0 deletions src/ts-ast-util/template-expression-resolver.test.ts
Expand Up @@ -505,6 +505,44 @@ describe(TemplateExpressionResolver.prototype.resolve, () => {
expect(actual?.combinedText).toMatchSnapshot();
});

it('should return combined string with template in call expression', () => {
const langService = createTestingLanguageService({
files: [
{
fileName: 'main.ts',
content: `
const fragment1 = gql(\`
fragment Foo on Hoge {
name
}
\`);
const fragment2 = gql(\`
\${fragment1}
fragment Piyo on Hoge {
...Foo
}
\`);
const query = \`
\${fragment2}
query {
...Piyo
}\`;
`,
},
],
});
const source = langService.getProgram()!.getSourceFile('main.ts');
if (!source) return fail();
const [, node] = findAllNodes(source, node => ts.isTemplateExpression(node));
const resolver = new TemplateExpressionResolver(
langService,
() => '',
() => false,
);
const actual = resolver.resolve('main.ts', node as ts.TemplateExpression).resolvedInfo;
expect(actual?.combinedText).toMatchSnapshot();
});

it('should return combined string with hopping reference', () => {
const langService = createTestingLanguageService({
files: [
Expand Down
7 changes: 7 additions & 0 deletions src/ts-ast-util/template-expression-resolver.ts
Expand Up @@ -371,6 +371,13 @@ export class TemplateExpressionResolver {
} else {
return setValueToCache(getValueForTemplateExpression(node.template, dependencies));
}
} else if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.arguments.length > 0) {
const firstArgNode = node.arguments[0];
if (ts.isNoSubstitutionTemplateLiteral(firstArgNode)) {
return setValueToCache({ text: firstArgNode.text, dependencies });
} else if (ts.isTemplateExpression(firstArgNode)) {
return setValueToCache(getValueForTemplateExpression(firstArgNode, dependencies));
}
} else if (ts.isTemplateExpression(node)) {
return setValueToCache(getValueForTemplateExpression(node, dependencies));
}
Expand Down