Skip to content

Commit

Permalink
lint: formatted codebase to pass eslint test
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnelson committed Nov 14, 2018
1 parent 34a013d commit f1e9011
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 303 deletions.
95 changes: 46 additions & 49 deletions src/file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ import path from 'path';
import isGlob from 'is-glob';
import Glob from 'glob';

const recursiveReadDirSync = dir =>
fs.readdirSync(dir)
.reduce((files, file) => (
fs.statSync(path.join(dir, file)).isDirectory() ?
files.concat(recursiveReadDirSync(path.join(dir, file))) :
files.concat(path.join(dir, file))
),
[]);
const recursiveReadDirSync = dir => fs
.readdirSync(dir)
.reduce(
(files, file) => (fs.statSync(path.join(dir, file)).isDirectory()
? files.concat(recursiveReadDirSync(path.join(dir, file)))
: files.concat(path.join(dir, file))),
[],
);

const readDirSync = dir =>
fs.readdirSync(dir)
.reduce((files, file) => (
fs.statSync(path.join(dir, file)).isDirectory() ?
files :
files.concat(path.join(dir, file))
),
[]);
const readDirSync = dir => fs
.readdirSync(dir)
.reduce(
(files, file) => (fs.statSync(path.join(dir, file)).isDirectory()
? files
: files.concat(path.join(dir, file))),
[],
);

const readGlobSync = (pattern, options) =>
Glob.sync(pattern, options);
const readGlobSync = (pattern, options) => Glob.sync(pattern, options);

const getSchemaFiles = (dir, recursive, globOptions) => {
if (isGlob(dir)) {
Expand All @@ -38,45 +37,43 @@ const getSchemaFiles = (dir, recursive, globOptions) => {

const DEFAULT_EXTENSIONS = ['.ts', '.js', '.gql', '.graphql', '.graphqls'];

const fileLoader = (folderPath,
{
recursive = false,
extensions = DEFAULT_EXTENSIONS,
globOptions = {},
} = {}) => {
const fileLoader = (
folderPath,
{ recursive = false, extensions = DEFAULT_EXTENSIONS, globOptions = {} } = {},
) => {
const dir = folderPath;
const schemafiles = getSchemaFiles(dir, recursive, globOptions);

const files = schemafiles
.map(f => ({ f, pathObj: path.parse(f) }))
.filter(({ pathObj }) => pathObj.name.toLowerCase() !== 'index')
.filter(({ pathObj }) => extensions.includes(pathObj.ext))
.map(({ f, pathObj }) => {
let returnVal;
.map(f => ({ f, pathObj: path.parse(f) }))
.filter(({ pathObj }) => pathObj.name.toLowerCase() !== 'index')
.filter(({ pathObj }) => extensions.includes(pathObj.ext))
.map(({ f, pathObj }) => {
let returnVal;

switch (pathObj.ext) {
case '.ts':
case '.js': {
const file = require(f); // eslint-disable-line
returnVal = file.default || file;
break;
}
switch (pathObj.ext) {
case '.ts':
case '.js': {
const file = require(f); // eslint-disable-line
returnVal = file.default || file;
break;
}

case '.graphqls':
case '.gql':
case '.graphql': {
const file = fs.readFileSync(f, 'utf8');
returnVal = file;
break;
}
case '.graphqls':
case '.gql':
case '.graphql': {
const file = fs.readFileSync(f, 'utf8');
returnVal = file;
break;
}

default:
// we don't know how to handle other extensions
}
default:
// we don't know how to handle other extensions
}

return returnVal;
})
.filter(v => !!v); // filter files that we don't know how to handle
return returnVal;
})
.filter(v => !!v); // filter files that we don't know how to handle

return files;
};
Expand Down
162 changes: 84 additions & 78 deletions src/merge_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import { getDescription } from 'graphql/utilities/buildASTSchema';
// TODO: Refactor code and switch to using print from graphql directly.
import print from './utilities/astPrinter';
import { makeSchema, mergeableTypes } from './utilities/makeSchema';
import { isObjectTypeDefinition, isObjectSchemaDefinition, isEnumTypeDefinition } from './utilities/astHelpers';
import {
isObjectTypeDefinition,
isObjectSchemaDefinition,
isEnumTypeDefinition,
} from './utilities/astHelpers';

const _isMergeableTypeDefinition = (def, all) =>
isObjectTypeDefinition(def) && (mergeableTypes.includes(def.name.value) || all);
const _isMergeableTypeDefinition = (def, all) => isObjectTypeDefinition(def) && (mergeableTypes.includes(def.name.value) || all);

const _isNonMergeableTypeDefinition = (def, all) => !_isMergeableTypeDefinition(def, all);

const _makeCommentNode = value => ({ kind: 'Comment', value });

const _addCommentsToAST = (nodes = [], flatten = true) => {
const astWithComments = nodes.map(
(node) => {
const description = getDescription(node, { commentDescriptions: true });
if (description) {
return [_makeCommentNode(description), node];
}
const astWithComments = nodes.map((node) => {
const description = getDescription(node, { commentDescriptions: true });
if (description) {
return [_makeCommentNode(description), node];
}

return [node];
},
);
return [node];
});

if (flatten) {
return astWithComments.reduce((a, b) => a.concat(b), []);
Expand All @@ -33,79 +34,81 @@ const _addCommentsToAST = (nodes = [], flatten = true) => {
return astWithComments;
};

const _makeRestDefinitions = (defs, all = false) =>
defs
.filter(def => _isNonMergeableTypeDefinition(def, all) && !isObjectSchemaDefinition(def))
.map((def) => {
if (isObjectTypeDefinition(def) || isEnumTypeDefinition(def)) {
return {
...def,
fields: def.fields ? _addCommentsToAST(def.fields) : undefined,
values: def.values ? _addCommentsToAST(def.values) : undefined,
};
}
const _makeRestDefinitions = (defs, all = false) => defs
.filter(def => _isNonMergeableTypeDefinition(def, all) && !isObjectSchemaDefinition(def))
.map((def) => {
if (isObjectTypeDefinition(def) || isEnumTypeDefinition(def)) {
return {
...def,
fields: def.fields ? _addCommentsToAST(def.fields) : undefined,
values: def.values ? _addCommentsToAST(def.values) : undefined,
};
}

return def;
});
return def;
});

// Gracefully handle nested pathing of GraphQL types
// skips any attributes that can't be found in path (i.e. type.type.type.value => type.type.value)
// returns chained-property value path of tracker attribute later used for comparison
// (i.e. 'ListType.NamedType.SomeValue' !== 'NonNullType.ListType.NamedType.SomeValue')
const _getGraphQLPath = (path, theObj, tracker) => {
let result;
path
.split('.')
.reduce((o, x) => {
if (o && o[x]) {
const v = o[x].hasOwnProperty.call(tracker) ?
o[x][tracker] : o[x];
if (!result) result = v;
else result += `.${v}`;
return o[x];
}
return o;
}, theObj);
path.split('.').reduce((o, x) => {
if (o && o[x]) {
const v = o[x].hasOwnProperty.call(tracker) ? o[x][tracker] : o[x];
if (!result) result = v;
else result += `.${v}`;
return o[x];
}
return o;
}, theObj);
return result;
};

const _makeMergedFieldDefinitions = (merged, candidate) => _addCommentsToAST(candidate.fields)
.reduce((fields, field) => {
const original = merged.fields.find(base => base.name && typeof base.name.value !== 'undefined' &&
field.name && typeof field.name.value !== 'undefined' &&
base.name.value === field.name.value);
if (!original) {
fields.push(field);
} else if (field.type.kind === 'NamedType') {
const fieldName = (field.type.name && field.type.name.value) || null;
const originalName = (original.type.name && original.type.name.value) || null;
if (!fieldName || !originalName || (fieldName !== originalName)) {
throw new Error(`Conflicting types for ${merged.name.value}.${fieldName}: ${fieldName || 'undefined'} != ${originalName}`);
}
} else if (field.type.kind === 'NonNullType' || field.type.kind === 'ListType') {
const path = _getGraphQLPath('type.type.type.value', field, 'kind');
const originalPath = _getGraphQLPath('type.type.type.value', original, 'kind');

if (path !== originalPath) {
throw new Error(
`Conflicting types for ${merged.name.value}.${field.name.value}: ` +
`${path} != ${originalPath}`,
);
}
const _makeMergedFieldDefinitions = (merged, candidate) => _addCommentsToAST(candidate.fields).reduce((fields, field) => {
const original = merged.fields.find(
base => base.name
&& typeof base.name.value !== 'undefined'
&& field.name
&& typeof field.name.value !== 'undefined'
&& base.name.value === field.name.value,
);
if (!original) {
fields.push(field);
} else if (field.type.kind === 'NamedType') {
const fieldName = (field.type.name && field.type.name.value) || null;
const originalName = (original.type.name && original.type.name.value) || null;
if (!fieldName || !originalName || fieldName !== originalName) {
throw new Error(
`Conflicting types for ${merged.name.value}.${fieldName}: ${fieldName
|| 'undefined'} != ${originalName}`,
);
}

// retain directives of both fields.
if (original) {
original.directives = original.directives.concat(field.directives);
original.directives = original.directives.reduce((current, next) => {
if (current.findIndex(n => n.name.value === next.name.value) === -1) {
current.push(next);
}
return current;
}, []);
} else if (field.type.kind === 'NonNullType' || field.type.kind === 'ListType') {
const path = _getGraphQLPath('type.type.type.value', field, 'kind');
const originalPath = _getGraphQLPath('type.type.type.value', original, 'kind');

if (path !== originalPath) {
throw new Error(
`Conflicting types for ${merged.name.value}.${field.name.value}: `
+ `${path} != ${originalPath}`,
);
}
return fields;
}, merged.fields);
}

// retain directives of both fields.
if (original) {
original.directives = original.directives.concat(field.directives);
original.directives = original.directives.reduce((current, next) => {
if (current.findIndex(n => n.name.value === next.name.value) === -1) {
current.push(next);
}
return current;
}, []);
}
return fields;
}, merged.fields);

const _makeMergedDefinitions = (defs, all = false) => {
// TODO: This function can be cleaner!
Expand Down Expand Up @@ -133,16 +136,18 @@ const _makeMergedDefinitions = (defs, all = false) => {
fields: _makeMergedFieldDefinitions(mergableDefs[name], def),
},
};
}, {
},
{
Query: null,
Mutation: null,
Subscription: null,
},
);

return Object
.values(groupedMergableDefinitions)
.reduce((array, def) => (def ? [...array, def] : array), []);
return Object.values(groupedMergableDefinitions).reduce(
(array, def) => (def ? [...array, def] : array),
[],
);
};

const _makeDocumentWithDefinitions = definitions => ({
Expand All @@ -164,8 +169,9 @@ const mergeTypes = (types, options = { all: false }) => {
.reduce((defs, newDef) => [...defs, ...newDef], []);

const mergedDefs = _makeMergedDefinitions(allDefs, options.all);
const rest = _addCommentsToAST(_makeRestDefinitions(allDefs, options.all), false)
.map(printDefinitions);
const rest = _addCommentsToAST(_makeRestDefinitions(allDefs, options.all), false).map(
printDefinitions,
);
const schemaDefs = allDefs.filter(isObjectSchemaDefinition);
const schema = printDefinitions([makeSchema(mergedDefs, schemaDefs), ...mergedDefs]);

Expand Down
19 changes: 7 additions & 12 deletions src/utilities/astHelpers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { Kind } from 'graphql';

const hasDefinitionWithName = (nodes, name) =>
nodes.findIndex(node => node.name.value === name) !== -1;
const hasDefinitionWithName = (nodes, name) => nodes.findIndex(node => node.name.value === name) !== -1;

const isObjectTypeDefinition = def => (
def.kind === Kind.OBJECT_TYPE_DEFINITION ||
def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
def.kind === Kind.SCALAR_TYPE_DEFINITION ||
def.kind === Kind.ENUM_TYPE_DEFINITION ||
def.kind === Kind.INTERFACE_TYPE_DEFINITION
);
const isObjectTypeDefinition = def => def.kind === Kind.OBJECT_TYPE_DEFINITION
|| def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION
|| def.kind === Kind.SCALAR_TYPE_DEFINITION
|| def.kind === Kind.ENUM_TYPE_DEFINITION
|| def.kind === Kind.INTERFACE_TYPE_DEFINITION;

const isEnumTypeDefinition = def => (
def.kind === Kind.ENUM_TYPE_DEFINITION
);
const isEnumTypeDefinition = def => def.kind === Kind.ENUM_TYPE_DEFINITION;

const isObjectSchemaDefinition = def => def.kind === Kind.SCHEMA_DEFINITION;

Expand Down
Loading

0 comments on commit f1e9011

Please sign in to comment.