Skip to content

Commit 1eba826

Browse files
committed
Adding more test cases, handling default env.
Making sure to check the invalid fragment or variable case in other environments, as well as ensuring that the default env works similar to how Apollo does.
1 parent a7ee76f commit 1eba826

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function replaceExpressions(node, context, env) {
190190

191191
chunks.push(chunk);
192192

193-
if (env === 'apollo') {
193+
if (!env || env === 'apollo') {
194194
// In Apollo, interpolation is only valid outside top-level structures like `query` or `mutation`.
195195
// We'll check to make sure there's an equivalent set of opening and closing brackets, otherwise
196196
// we're attempting to do an invalid interpolation.
@@ -223,7 +223,7 @@ function replaceExpressions(node, context, env) {
223223
// Ellipsis cancels out extra characters
224224
const placeholder = strWithLen(nameLength);
225225
chunks.push('...' + placeholder);
226-
} else if (env === 'apollo') {
226+
} else if (!env || env === 'apollo') {
227227
// In Apollo, fragment interpolation is only valid outside of brackets
228228
// Since we don't know what we'd interpolate here (that occurs at runtime),
229229
// we're not going to do anything with this interpolation.

test/makeRule.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const parserOptions = {
3030
parserOptions,
3131
code: 'const x = gql`{ number }`',
3232
},
33+
{
34+
options,
35+
parserOptions,
36+
code: 'const x = gql`{ number } ${x}`',
37+
},
3338
],
3439

3540
invalid: [
@@ -51,6 +56,17 @@ const parserOptions = {
5156
type: 'TaggedTemplateExpression'
5257
}]
5358
},
59+
{
60+
options,
61+
parserOptions,
62+
code: 'const x = gql`{ ${x} }`',
63+
errors: [{
64+
message: 'Invalid interpolation - fragment interpolation must occur outside of the brackets.',
65+
type: 'Identifier',
66+
line: 1,
67+
column: 19
68+
}]
69+
},
5470
]
5571
});
5672
}
@@ -94,7 +110,7 @@ const parserOptions = {
94110
code: 'const x = myGraphQLTag`{ ${x} }`',
95111
errors: [{
96112
type: 'Identifier',
97-
message: 'Invalid interpolation - not a valid fragment or variable.'
113+
message: 'Invalid interpolation - fragment interpolation must occur outside of the brackets.'
98114
}]
99115
},
100116
]
@@ -111,7 +127,7 @@ const parserOptions = {
111127
{
112128
options,
113129
parserOptions,
114-
code: 'const x = gql`{ number } ${SomeFragment}`',
130+
code: 'const x = gql`{ number } ${x}`',
115131
},
116132
],
117133

@@ -274,6 +290,29 @@ const parserOptions = {
274290
column: 19
275291
}]
276292
},
293+
{
294+
options,
295+
parserOptions,
296+
code: `
297+
client.query(gql\`
298+
{
299+
allFilms {
300+
films {
301+
\${filmInfo}
302+
}
303+
}
304+
}
305+
\`).then(result => {
306+
console.log(result.allFilms.films);
307+
});
308+
`,
309+
errors: [{
310+
message: 'Invalid interpolation - not a valid fragment or variable.',
311+
type: 'Identifier',
312+
line: 6,
313+
column: 21
314+
}]
315+
},
277316
]
278317
});
279318
}

0 commit comments

Comments
 (0)