Skip to content

Commit

Permalink
remove fragment naming enforcement from JS compiler
Browse files Browse the repository at this point in the history
Reviewed By: josephsavona

Differential Revision: D23167481

fbshipit-source-id: 123f344f0aa51f9d9edeac695ec5adfd73c6482e
  • Loading branch information
kassens authored and facebook-github-bot committed Aug 17, 2020
1 parent 5d46555 commit ff1c10b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,22 @@ describe('RelayFindGraphQLTags', () => {
).toEqual(['fragment FindGraphQLTags on User { name }']);
});

it('throws for invalid container fragment names', () => {
expect(() =>
find(`
createFragmentContainer(Foo, {
foo: graphql\`fragment FindGraphQLTags_notFoo on User { name }\`,
});
`),
).toThrow(
'FindGraphQLTags: Container fragment names must be ' +
'`<ModuleName>_<propName>`. Got `FindGraphQLTags_notFoo`, expected ' +
'`FindGraphQLTags_foo`.',
);
});

it('parses container fragments with valid names', () => {
expect(
find(`
createFragmentContainer(Foo, {
foo: graphql\`fragment FindGraphQLTags_foo on User { name }\`,
});
// No longer validates that property name and fragment name match
createFragmentContainer(Foo, {
foo: graphql\`fragment FindGraphQLTags_notFoo on User { name }\`,
});
`),
).toEqual(['fragment FindGraphQLTags_foo on User { name }']);
).toEqual([
'fragment FindGraphQLTags_foo on User { name }',
'fragment FindGraphQLTags_notFoo on User { name }',
]);
});
});
});
97 changes: 0 additions & 97 deletions packages/relay-compiler/language/javascript/FindGraphQLTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,76 +49,6 @@ function find(text: string): $ReadOnlyArray<GraphQLTag> {
const ast = babylon.parse(text, BABYLON_OPTIONS);

const visitors = {
CallExpression: node => {
const callee = node.callee;
if (
!(
(callee.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.name]) ||
(callee.kind === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.value === 'Relay' &&
callee.property.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.property.name])
)
) {
traverse(node, visitors);
return;
}
const fragments = node.arguments[1];
if (fragments.type === 'ObjectExpression') {
fragments.properties.forEach(property => {
invariant(
property.type === 'ObjectProperty' &&
property.key.type === 'Identifier' &&
property.value.type === 'TaggedTemplateExpression',
'FindGraphQLTags: `%s` expects fragment definitions to be ' +
'`key: graphql`.',
node.callee.name,
);
invariant(
isGraphQLModernOrDeprecatedTag(property.value.tag),
'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' +
'with `graphql`, got `%s`.',
node.callee.name,
getSourceTextForLocation(text, property.value.tag.loc),
);
if (isGraphQLTag(property.value.tag)) {
result.push({
keyName: property.key.name,
template: getGraphQLText(property.value.quasi),
sourceLocationOffset: getSourceLocationOffset(
property.value.quasi,
),
});
}
});
} else {
invariant(
fragments && fragments.type === 'TaggedTemplateExpression',
'FindGraphQLTags: `%s` expects a second argument of fragment ' +
'definitions.',
node.callee.name,
);
invariant(
isGraphQLModernOrDeprecatedTag(fragments.tag),
'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' +
'with `graphql`, got `%s`.',
node.callee.name,
getSourceTextForLocation(text, fragments.tag.loc),
);
result.push({
keyName: null,
template: getGraphQLText(fragments.quasi),
sourceLocationOffset: getSourceLocationOffset(fragments.quasi),
});
}

// Visit remaining arguments
for (let ii = 2; ii < node.arguments.length; ii++) {
visit(node.arguments[ii], visitors);
}
},
TaggedTemplateExpression: node => {
if (isGraphQLTag(node.tag)) {
result.push({
Expand All @@ -133,12 +63,6 @@ function find(text: string): $ReadOnlyArray<GraphQLTag> {
return result;
}

const CREATE_CONTAINER_FUNCTIONS = Object.create(null, {
createFragmentContainer: {value: true},
createPaginationContainer: {value: true},
createRefetchContainer: {value: true},
});

const IGNORED_KEYS = {
comments: true,
end: true,
Expand All @@ -154,13 +78,6 @@ function isGraphQLTag(tag): boolean {
return tag.type === 'Identifier' && tag.name === 'graphql';
}

function isGraphQLModernOrDeprecatedTag(tag): boolean {
return (
tag.type === 'Identifier' &&
(tag.name === 'graphql' || tag.name === 'graphql_DEPRECATED')
);
}

function getTemplateNode(quasi) {
const quasis = quasi.quasis;
invariant(
Expand All @@ -170,10 +87,6 @@ function getTemplateNode(quasi) {
return quasis[0];
}

function getGraphQLText(quasi): string {
return getTemplateNode(quasi).value.raw;
}

function getSourceLocationOffset(quasi) {
const loc = getTemplateNode(quasi).loc.start;
return {
Expand All @@ -182,16 +95,6 @@ function getSourceLocationOffset(quasi) {
};
}

function getSourceTextForLocation(text, loc) {
if (loc == null) {
return '(source unavailable)';
}
const lines = text.split('\n').slice(loc.start.line - 1, loc.end.line);
lines[0] = lines[0].slice(loc.start.column);
lines[lines.length - 1] = lines[lines.length - 1].slice(0, loc.end.column);
return lines.join('\n');
}

function invariant(condition, msg, ...args) {
if (!condition) {
throw new Error(util.format(msg, ...args));
Expand Down

0 comments on commit ff1c10b

Please sign in to comment.