Skip to content

Commit

Permalink
Adding more test cases, handling default env.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jnwng committed Dec 8, 2016
1 parent be88ab5 commit 3cbddfb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ function replaceExpressions(node, context, env) {

chunks.push(chunk);

if (env === 'apollo') {
if (!env || env === 'apollo') {
// In Apollo, interpolation is only valid outside top-level structures like `query` or `mutation`.
// We'll check to make sure there's an equivalent set of opening and closing brackets, otherwise
// we're attempting to do an invalid interpolation.
Expand Down Expand Up @@ -294,7 +294,7 @@ function replaceExpressions(node, context, env) {
// Ellipsis cancels out extra characters
const placeholder = strWithLen(nameLength);
chunks.push('...' + placeholder);
} else if (env === 'apollo') {
} else if (!env || env === 'apollo') {
// In Apollo, fragment interpolation is only valid outside of brackets
// Since we don't know what we'd interpolate here (that occurs at runtime),
// we're not going to do anything with this interpolation.
Expand Down
45 changes: 42 additions & 3 deletions test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ const parserOptions = {
options,
parserOptions,
code: 'const x = gql.segmented`height: 12px;`'
}
},
{
options,
parserOptions,
code: 'const x = gql`{ number } ${x}`',
},
],

invalid: [
Expand All @@ -67,6 +72,17 @@ const parserOptions = {
type: 'TaggedTemplateExpression'
}]
},
{
options,
parserOptions,
code: 'const x = gql`{ ${x} }`',
errors: [{
message: 'Invalid interpolation - fragment interpolation must occur outside of the brackets.',
type: 'Identifier',
line: 1,
column: 19
}]
},
]
});
}
Expand Down Expand Up @@ -110,7 +126,7 @@ const parserOptions = {
code: 'const x = myGraphQLTag`{ ${x} }`',
errors: [{
type: 'Identifier',
message: 'Invalid interpolation - not a valid fragment or variable.'
message: 'Invalid interpolation - fragment interpolation must occur outside of the brackets.'
}]
},
]
Expand All @@ -127,7 +143,7 @@ const parserOptions = {
{
options,
parserOptions,
code: 'const x = gql`{ number } ${SomeFragment}`',
code: 'const x = gql`{ number } ${x}`',
},
],

Expand Down Expand Up @@ -290,6 +306,29 @@ const parserOptions = {
column: 19
}]
},
{
options,
parserOptions,
code: `
client.query(gql\`
{
allFilms {
films {
\${filmInfo}
}
}
}
\`).then(result => {
console.log(result.allFilms.films);
});
`,
errors: [{
message: 'Invalid interpolation - not a valid fragment or variable.',
type: 'Identifier',
line: 6,
column: 21
}]
},
]
});
}
Expand Down

0 comments on commit 3cbddfb

Please sign in to comment.