Skip to content

Commit

Permalink
remove unused embedded graphql lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Jul 3, 2022
1 parent aeb84dc commit c22885b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,56 @@ query Test {
`);
});

it('finds queries in /* GraphQL */ prefixed templates', async () => {
const text = `
import {gql} from 'react-apollo';
import {B} from 'B';
import A from './A';
const QUERY: string =
/* GraphQL */
\`
query Test {
test {
value
...FragmentsComment
}
}
\${A.fragments.test}
\`
export function Example(arg: string) {}`;

const contents = findGraphQLTags(text, '.ts');
expect(contents[0].template).toEqual(`
query Test {
test {
value
...FragmentsComment
}
}
`);
});

it('finds queries with nested template tag expressions', async () => {
const text = `export default {
else: () => gql\` query {} \`
}`;

const contents = findGraphQLTags(text, '.ts');
expect(contents[0].template).toEqual(` query {} `);
});

it('finds queries with template tags inside call expressions', async () => {
const text = `something({
else: () => gql\` query {} \`
})`;

const contents = findGraphQLTags(text, '.ts');
expect(contents[0].template).toEqual(` query {} `);
});

it('ignores non gql tagged templates', async () => {
const text = `
// @flow
Expand Down
78 changes: 2 additions & 76 deletions packages/graphql-language-service-server/src/findGraphQLTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import {
Expression,
TaggedTemplateExpression,
ObjectExpression,
TemplateLiteral,
} from '@babel/types';

Expand All @@ -28,12 +27,6 @@ const PARSER_OPTIONS: ParserOptions = {
strictMode: false,
};

const CREATE_CONTAINER_FUNCTIONS: { [key: string]: boolean } = {
createFragmentContainer: true,
createPaginationContainer: true,
createRefetchContainer: true,
};

const DEFAULT_STABLE_TAGS = ['graphql', 'graphqls', 'gql'];
export const DEFAULT_TAGS = [...DEFAULT_STABLE_TAGS, 'graphql.experimental'];

Expand Down Expand Up @@ -142,70 +135,8 @@ export function findGraphQLTags(
}
}

if (
!(
(callee.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.name]) ||
(callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'Relay' &&
callee.property.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.property.name])
)
) {
traverse(node, visitors);
return;
}

if ('arguments' in node) {
const fragments = node.arguments[1];
if (fragments.type === 'ObjectExpression') {
fragments.properties.forEach(
(property: ObjectExpression['properties'][0]) => {
if (
'value' in property &&
'loc' in property.value &&
'tag' in property.value
) {
const tagName = getGraphQLTagName(property.value.tag);
const template = getGraphQLText(property.value.quasi);
if (tagName && property.value.loc) {
const loc = property.value.loc;
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column),
);
result.push({
tag: tagName,
template,
range,
});
}
}
},
);
} else if ('tag' in fragments) {
const tagName = getGraphQLTagName(fragments.tag);
const template = getGraphQLText(fragments.quasi);
if (tagName && fragments.loc) {
const loc = fragments.loc;
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column),
);

result.push({
tag: tagName,
template,
range,
});
}
}
// Visit remaining arguments
for (let ii = 2; ii < node.arguments.length; ii++) {
visit(node.arguments[ii], visitors);
}
}
traverse(node, visitors);
return;
}
},
TaggedTemplateExpression: (node: TaggedTemplateExpression) => {
Expand Down Expand Up @@ -284,11 +215,6 @@ function getGraphQLTagName(tag: Expression): string | null {
return null;
}

function getGraphQLText(quasi: TemplateLiteral) {
const quasis = quasi.quasis;
return quasis[0].value.raw;
}

function visit(node: { [key: string]: any }, visitors: TagVisitors) {
const fn = visitors[node.type];
if (fn && fn != null) {
Expand Down

0 comments on commit c22885b

Please sign in to comment.