Created
July 5, 2020 13:46
-
-
Save id-ilych/abbd534863a170dd6ccdf39265af1ff4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
const config = require('../graphql-schema-linter.config.js') | |
// Aaa prefix is used to put this rule on top of the list | |
// which is required for excludes to work properly | |
// This rule allows to add exclusions (defined in graphql-schema.config.js) | |
function AaaWithExcludes (context) { | |
const stack = [] | |
let currentType | |
let currentField | |
let currentParameter | |
let excludes = {} | |
for (const [rule, items] of Object.entries(config.excludes)) { | |
excludes[rule] = new Set(items) | |
} | |
const originalReportError = context.reportError | |
context.reportError = function (err) { | |
const rule = err.ruleName | |
const items = excludes[rule] | |
if (items) { | |
if (items.has(currentType) || | |
items.has(currentField) || | |
items.has(currentParameter) | |
) { | |
return | |
} | |
} | |
originalReportError.apply(this, arguments) | |
} | |
return { | |
enter (node) { | |
if (node.name === undefined) { | |
stack.push(null) | |
return | |
} | |
if (node.name.kind !== 'Name') { | |
const err = `Unexpected node name kind: ${node.name.kind}` | |
console.group() | |
console.error(err) | |
console.error('node:') | |
console.error(node) | |
console.groupEnd() | |
throw new Error(err) | |
} | |
const name = node.name.value | |
switch (node.kind) { | |
case 'ObjectTypeDefinition': | |
case 'InterfaceTypeDefinition': | |
case 'InputObjectTypeDefinition': | |
currentType = name | |
stack.push('type') | |
break | |
case 'FieldDefinition': | |
currentField = `${currentType}.${name}` | |
stack.push('field') | |
break | |
case 'InputValueDefinition': | |
currentParameter = `${currentField}.${name}` | |
stack.push('parameter') | |
break | |
default: | |
stack.push(null) | |
break | |
} | |
}, | |
leave () { | |
switch (stack.pop()) { | |
case 'type': | |
currentType = undefined | |
break | |
case 'field': | |
currentField = undefined | |
break | |
case 'parameter': | |
currentParameter = undefined | |
break | |
} | |
} | |
} | |
} | |
module.exports = { AaaWithExcludes } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment