Skip to content

Commit ff1c10b

Browse files
kassensfacebook-github-bot
authored andcommittedAug 17, 2020
remove fragment naming enforcement from JS compiler
Reviewed By: josephsavona Differential Revision: D23167481 fbshipit-source-id: 123f344f0aa51f9d9edeac695ec5adfd73c6482e
1 parent 5d46555 commit ff1c10b

File tree

2 files changed

+9
-112
lines changed

2 files changed

+9
-112
lines changed
 

‎packages/relay-compiler/core/__tests__/RelayFindGraphQLTags-test.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -181,28 +181,22 @@ describe('RelayFindGraphQLTags', () => {
181181
).toEqual(['fragment FindGraphQLTags on User { name }']);
182182
});
183183

184-
it('throws for invalid container fragment names', () => {
185-
expect(() =>
186-
find(`
187-
createFragmentContainer(Foo, {
188-
foo: graphql\`fragment FindGraphQLTags_notFoo on User { name }\`,
189-
});
190-
`),
191-
).toThrow(
192-
'FindGraphQLTags: Container fragment names must be ' +
193-
'`<ModuleName>_<propName>`. Got `FindGraphQLTags_notFoo`, expected ' +
194-
'`FindGraphQLTags_foo`.',
195-
);
196-
});
197-
198184
it('parses container fragments with valid names', () => {
199185
expect(
200186
find(`
201187
createFragmentContainer(Foo, {
202188
foo: graphql\`fragment FindGraphQLTags_foo on User { name }\`,
203189
});
190+
191+
// No longer validates that property name and fragment name match
192+
createFragmentContainer(Foo, {
193+
foo: graphql\`fragment FindGraphQLTags_notFoo on User { name }\`,
194+
});
204195
`),
205-
).toEqual(['fragment FindGraphQLTags_foo on User { name }']);
196+
).toEqual([
197+
'fragment FindGraphQLTags_foo on User { name }',
198+
'fragment FindGraphQLTags_notFoo on User { name }',
199+
]);
206200
});
207201
});
208202
});

‎packages/relay-compiler/language/javascript/FindGraphQLTags.js

-97
Original file line numberDiff line numberDiff line change
@@ -49,76 +49,6 @@ function find(text: string): $ReadOnlyArray<GraphQLTag> {
4949
const ast = babylon.parse(text, BABYLON_OPTIONS);
5050

5151
const visitors = {
52-
CallExpression: node => {
53-
const callee = node.callee;
54-
if (
55-
!(
56-
(callee.type === 'Identifier' &&
57-
CREATE_CONTAINER_FUNCTIONS[callee.name]) ||
58-
(callee.kind === 'MemberExpression' &&
59-
callee.object.type === 'Identifier' &&
60-
callee.object.value === 'Relay' &&
61-
callee.property.type === 'Identifier' &&
62-
CREATE_CONTAINER_FUNCTIONS[callee.property.name])
63-
)
64-
) {
65-
traverse(node, visitors);
66-
return;
67-
}
68-
const fragments = node.arguments[1];
69-
if (fragments.type === 'ObjectExpression') {
70-
fragments.properties.forEach(property => {
71-
invariant(
72-
property.type === 'ObjectProperty' &&
73-
property.key.type === 'Identifier' &&
74-
property.value.type === 'TaggedTemplateExpression',
75-
'FindGraphQLTags: `%s` expects fragment definitions to be ' +
76-
'`key: graphql`.',
77-
node.callee.name,
78-
);
79-
invariant(
80-
isGraphQLModernOrDeprecatedTag(property.value.tag),
81-
'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' +
82-
'with `graphql`, got `%s`.',
83-
node.callee.name,
84-
getSourceTextForLocation(text, property.value.tag.loc),
85-
);
86-
if (isGraphQLTag(property.value.tag)) {
87-
result.push({
88-
keyName: property.key.name,
89-
template: getGraphQLText(property.value.quasi),
90-
sourceLocationOffset: getSourceLocationOffset(
91-
property.value.quasi,
92-
),
93-
});
94-
}
95-
});
96-
} else {
97-
invariant(
98-
fragments && fragments.type === 'TaggedTemplateExpression',
99-
'FindGraphQLTags: `%s` expects a second argument of fragment ' +
100-
'definitions.',
101-
node.callee.name,
102-
);
103-
invariant(
104-
isGraphQLModernOrDeprecatedTag(fragments.tag),
105-
'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' +
106-
'with `graphql`, got `%s`.',
107-
node.callee.name,
108-
getSourceTextForLocation(text, fragments.tag.loc),
109-
);
110-
result.push({
111-
keyName: null,
112-
template: getGraphQLText(fragments.quasi),
113-
sourceLocationOffset: getSourceLocationOffset(fragments.quasi),
114-
});
115-
}
116-
117-
// Visit remaining arguments
118-
for (let ii = 2; ii < node.arguments.length; ii++) {
119-
visit(node.arguments[ii], visitors);
120-
}
121-
},
12252
TaggedTemplateExpression: node => {
12353
if (isGraphQLTag(node.tag)) {
12454
result.push({
@@ -133,12 +63,6 @@ function find(text: string): $ReadOnlyArray<GraphQLTag> {
13363
return result;
13464
}
13565

136-
const CREATE_CONTAINER_FUNCTIONS = Object.create(null, {
137-
createFragmentContainer: {value: true},
138-
createPaginationContainer: {value: true},
139-
createRefetchContainer: {value: true},
140-
});
141-
14266
const IGNORED_KEYS = {
14367
comments: true,
14468
end: true,
@@ -154,13 +78,6 @@ function isGraphQLTag(tag): boolean {
15478
return tag.type === 'Identifier' && tag.name === 'graphql';
15579
}
15680

157-
function isGraphQLModernOrDeprecatedTag(tag): boolean {
158-
return (
159-
tag.type === 'Identifier' &&
160-
(tag.name === 'graphql' || tag.name === 'graphql_DEPRECATED')
161-
);
162-
}
163-
16481
function getTemplateNode(quasi) {
16582
const quasis = quasi.quasis;
16683
invariant(
@@ -170,10 +87,6 @@ function getTemplateNode(quasi) {
17087
return quasis[0];
17188
}
17289

173-
function getGraphQLText(quasi): string {
174-
return getTemplateNode(quasi).value.raw;
175-
}
176-
17790
function getSourceLocationOffset(quasi) {
17891
const loc = getTemplateNode(quasi).loc.start;
17992
return {
@@ -182,16 +95,6 @@ function getSourceLocationOffset(quasi) {
18295
};
18396
}
18497

185-
function getSourceTextForLocation(text, loc) {
186-
if (loc == null) {
187-
return '(source unavailable)';
188-
}
189-
const lines = text.split('\n').slice(loc.start.line - 1, loc.end.line);
190-
lines[0] = lines[0].slice(loc.start.column);
191-
lines[lines.length - 1] = lines[lines.length - 1].slice(0, loc.end.column);
192-
return lines.join('\n');
193-
}
194-
19598
function invariant(condition, msg, ...args) {
19699
if (!condition) {
197100
throw new Error(util.format(msg, ...args));

0 commit comments

Comments
 (0)
Please sign in to comment.