Skip to content

Commit

Permalink
Remove use of Array.prototype.includes to support older runtimes (apo…
Browse files Browse the repository at this point in the history
…llographql#73)

Also removes `babel-polyfill` in favor of `transform-runtime` to surface these runtime-specific issues in testing.
  • Loading branch information
Justin Schulz committed Jul 12, 2017
1 parent 72d76c3 commit 93c58b9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNEXT
- Fix template env for older runtimes (eg. node 5 and earlier) [Justin Schulz](https://github.com/PepperTeasdale) in [#73](https://github.com/apollographql/eslint-plugin-graphql/pull/73)

### v0.8.2
- Remove `KnownFragmentNames` and `UnusedFragment` from default rules in `literal` env. [Justin Schulz](https://github.com/PepperTeasdale) in [#70](https://github.com/apollographql/eslint-plugin-graphql/pull/70)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"babel-cli": "^6.7.7",
"babel-core": "^6.7.7",
"babel-eslint": "^7.2.3",
"babel-polyfill": "^6.7.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.5.0",
"eslint": "^3.17.0",
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
last,
reduce,
without,
includes,
} from 'lodash';

import * as customRules from './rules';
Expand Down Expand Up @@ -421,7 +422,7 @@ const gqlProcessor = {
postprocess: function(messages) {
// only report graphql-errors
return flatten(messages).filter((message) => {
return keys(rules).map((key) => `graphql/${key}`).includes(message.ruleId);
return includes(keys(rules).map((key) => `graphql/${key}`), message.ruleId);
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file cannot be written with ECMAScript 2015 because it has to load
// the Babel require hook to enable ECMAScript 2015 features!
require('babel-core/register');
require('babel-polyfill');
require('babel-core').transform('code', {
plugins: ['transform-runtime']
});

// The tests, however, can and should be written with ECMAScript 2015.
require('./makeRule');
Expand Down
25 changes: 15 additions & 10 deletions test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { rules } from '../src';
import { RuleTester } from 'eslint';
import schemaJson from './schema.json';
import path from 'path';
import {
includes,
values,
entries,
} from 'lodash';

const schemaJsonFilepath = path.resolve(__dirname, './schema.json');
const secondSchemaJsonFilepath = path.resolve(__dirname, './second-schema.json');
Expand Down Expand Up @@ -823,8 +828,8 @@ const requiredFieldsTestCases = {
validators: 'all',
}];
ruleTester.run('enabled all validators', rule, {
valid: Object.values(validatorCases).map(({pass: code}) => ({options, parserOptions, code})),
invalid: Object.values(validatorCases).map(({fail: code, errors}) => ({options, parserOptions, code, errors})),
valid: values(validatorCases).map(({pass: code}) => ({options, parserOptions, code})),
invalid: values(validatorCases).map(({fail: code, errors}) => ({options, parserOptions, code, errors})),
});

options = [{
Expand All @@ -833,8 +838,8 @@ const requiredFieldsTestCases = {
}];
ruleTester.run('disabled all validators', rule, {
valid: [].concat(
Object.values(validatorCases).map(({pass: code}) => code),
Object.values(validatorCases).map(({fail: code}) => code),
values(validatorCases).map(({pass: code}) => code),
values(validatorCases).map(({fail: code}) => code),
).map(code => ({options, parserOptions, code})),
invalid: [],
});
Expand All @@ -843,19 +848,19 @@ const requiredFieldsTestCases = {
// that can fail. (Excluding test cases that include this validation rule as
// 'alsoBreaks'…sometimes it's hard to make a test that fails exactly one
// validator).
for (const [validatorName, {pass, fail, errors}] of Object.entries(validatorCases)) {
for (const [validatorName, {pass, fail, errors}] of entries(validatorCases)) {
options = [{
schemaJson, tagName: 'gql',
validators: [validatorName],
}];
const otherValidators = (
Object.entries(validatorCases)
.filter(([otherValidatorName, {alsoBreaks}]) => otherValidatorName !== validatorName && !(alsoBreaks || []).includes(validatorName))
entries(validatorCases)
.filter(([otherValidatorName, {alsoBreaks}]) => otherValidatorName !== validatorName && !includes((alsoBreaks || []), validatorName))
.map(([name, testCases]) => testCases)
);
ruleTester.run(`enabled only ${validatorName} validator`, rule, {
valid: [].concat(
Object.values(validatorCases).map(({pass: code}) => code),
values(validatorCases).map(({pass: code}) => code),
otherValidators.map(({fail: code}) => code),
).map(code => ({options, parserOptions, code})),
invalid: [{options, parserOptions, errors, code: fail}],
Expand All @@ -867,8 +872,8 @@ const requiredFieldsTestCases = {
schemaJson, tagName: 'gql',
}];
ruleTester.run('testing named-operations rule', rules['named-operations'], {
valid: Object.values(namedOperationsValidatorCases).map(({pass: code}) => ({options, parserOptions, code})),
invalid: Object.values(namedOperationsValidatorCases).map(({fail: code, errors}) => ({options, parserOptions, code, errors})),
valid: values(namedOperationsValidatorCases).map(({pass: code}) => ({options, parserOptions, code})),
invalid: values(namedOperationsValidatorCases).map(({fail: code, errors}) => ({options, parserOptions, code, errors})),
});

// Validate the required-fields rule
Expand Down
8 changes: 7 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,20 @@ babel-plugin-transform-regenerator@^6.24.1:
dependencies:
regenerator-transform "0.9.11"

babel-plugin-transform-runtime@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-runtime/-/babel-plugin-transform-runtime-6.23.0.tgz#88490d446502ea9b8e7efb0fe09ec4d99479b1ee"
dependencies:
babel-runtime "^6.22.0"

babel-plugin-transform-strict-mode@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz#d5faf7aa578a65bbe591cf5edae04a0c67020758"
dependencies:
babel-runtime "^6.22.0"
babel-types "^6.24.1"

babel-polyfill@^6.23.0, babel-polyfill@^6.7.4:
babel-polyfill@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.23.0.tgz#8364ca62df8eafb830499f699177466c3b03499d"
dependencies:
Expand Down

0 comments on commit 93c58b9

Please sign in to comment.