@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
63 lines (62 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToESTree = void 0;
const graphql_1 = require("graphql");
const utils_js_1 = require("./utils.js");
function convertToESTree(node, schema) {
const typeInfo = schema && new graphql_1.TypeInfo(schema);
const visitor = {
leave(node, key, parent) {
const leadingComments = 'description' in node && node.description
? [
{
type: node.description.block ? 'Block' : 'Line',
value: node.description.value,
},
]
: [];
const calculatedTypeInfo = typeInfo
? {
argument: typeInfo.getArgument(),
defaultValue: typeInfo.getDefaultValue(),
directive: typeInfo.getDirective(),
enumValue: typeInfo.getEnumValue(),
fieldDef: typeInfo.getFieldDef(),
inputType: typeInfo.getInputType(),
parentInputType: typeInfo.getParentInputType(),
parentType: typeInfo.getParentType(),
gqlType: typeInfo.getType(),
}
: {};
const rawNode = () => {
if (parent && key !== undefined) {
return parent[key];
}
return node.kind === graphql_1.Kind.DOCUMENT
? {
...node,
definitions: node.definitions.map(definition => definition.rawNode()),
}
: node;
};
const commonFields = {
...node,
type: node.kind,
loc: (0, utils_js_1.convertLocation)(node.loc),
range: [node.loc.start, node.loc.end],
leadingComments,
// Use function to prevent RangeError: Maximum call stack size exceeded
typeInfo: () => calculatedTypeInfo,
rawNode,
};
return 'type' in node
? {
...commonFields,
gqlType: node.type,
}
: commonFields;
},
};
return (0, graphql_1.visit)(node, typeInfo ? (0, graphql_1.visitWithTypeInfo)(typeInfo, visitor) : visitor);
}
exports.convertToESTree = convertToESTree;