-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
utils.ts
216 lines (188 loc) · 6.69 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { statSync } from 'fs';
import { dirname } from 'path';
import { Lexer, GraphQLSchema, Source, ObjectTypeDefinitionNode, ObjectTypeExtensionNode, Kind } from 'graphql';
import { AST } from 'eslint';
import { asArray, Source as LoaderSource } from '@graphql-tools/utils';
import lowerCase from 'lodash.lowercase';
import { GraphQLESLintRuleContext } from './types';
import { SiblingOperations } from './sibling-operations';
import { UsedFields, ReachableTypes } from './graphql-ast';
import { GraphQLESTreeNode } from './estree-parser';
export function requireSiblingsOperations(
ruleName: string,
context: GraphQLESLintRuleContext
): SiblingOperations | never {
if (!context.parserServices) {
throw new Error(
`Rule '${ruleName}' requires 'parserOptions.operations' to be set and loaded. See http://bit.ly/graphql-eslint-operations for more info`
);
}
if (!context.parserServices.siblingOperations.available) {
throw new Error(
`Rule '${ruleName}' requires 'parserOptions.operations' to be set and loaded. See http://bit.ly/graphql-eslint-operations for more info`
);
}
return context.parserServices.siblingOperations;
}
export function requireGraphQLSchemaFromContext(
ruleName: string,
context: GraphQLESLintRuleContext
): GraphQLSchema | never {
if (!context.parserServices) {
throw new Error(
`Rule '${ruleName}' requires 'parserOptions.schema' to be set. See http://bit.ly/graphql-eslint-schema for more info`
);
}
if (!context.parserServices.hasTypeInfo) {
throw new Error(
`Rule '${ruleName}' requires 'parserOptions.schema' to be set and schema to be loaded. See http://bit.ly/graphql-eslint-schema for more info`
);
}
return context.parserServices.schema;
}
export function requireReachableTypesFromContext(
ruleName: string,
context: GraphQLESLintRuleContext
): ReachableTypes | never {
const schema = requireGraphQLSchemaFromContext(ruleName, context);
return context.parserServices.reachableTypes(schema);
}
export function requireUsedFieldsFromContext(ruleName: string, context: GraphQLESLintRuleContext): UsedFields | never {
const schema = requireGraphQLSchemaFromContext(ruleName, context);
const siblings = requireSiblingsOperations(ruleName, context);
return context.parserServices.usedFields(schema, siblings);
}
function getLexer(source: Source): Lexer {
// GraphQL v14
const gqlLanguage = require('graphql/language');
if (gqlLanguage && gqlLanguage.createLexer) {
return gqlLanguage.createLexer(source, {});
}
// GraphQL v15
const { Lexer: LexerCls } = require('graphql');
if (LexerCls && typeof LexerCls === 'function') {
return new LexerCls(source);
}
throw new Error(`Unsupported GraphQL version! Please make sure to use GraphQL v14 or newer!`);
}
export function extractTokens(source: Source): AST.Token[] {
const lexer = getLexer(source);
const tokens: AST.Token[] = [];
let token = lexer.advance();
while (token && token.kind !== '<EOF>') {
tokens.push({
type: token.kind as any,
loc: {
start: {
line: token.line,
column: token.column,
},
end: {
line: token.line,
column: token.column,
},
},
value: token.value,
range: [token.start, token.end],
});
token = lexer.advance();
}
return tokens;
}
export const normalizePath = (path: string): string => (path || '').replace(/\\/g, '/');
/**
* https://github.com/prettier/eslint-plugin-prettier/blob/76bd45ece6d56eb52f75db6b4a1efdd2efb56392/eslint-plugin-prettier.js#L71
* Given a filepath, get the nearest path that is a regular file.
* The filepath provided by eslint may be a virtual filepath rather than a file
* on disk. This attempts to transform a virtual path into an on-disk path
*/
export const getOnDiskFilepath = (filepath: string): string => {
try {
if (statSync(filepath).isFile()) {
return filepath;
}
} catch (err) {
// https://github.com/eslint/eslint/issues/11989
if (err.code === 'ENOTDIR') {
return getOnDiskFilepath(dirname(filepath));
}
}
return filepath;
};
export const getTypeName = node => ('type' in node ? getTypeName(node.type) : node.name.value);
// Small workaround for the bug in older versions of @graphql-tools/load
// Can be removed after graphql-config bumps to a new version
export const loaderCache: Record<string, LoaderSource[]> = new Proxy(Object.create(null), {
get(cache, key) {
const value = cache[key];
if (value) {
return asArray(value);
}
return undefined;
},
set(cache, key, value) {
if (value) {
cache[key] = asArray(value);
}
return true;
},
});
type ObjectTypeNode = GraphQLESTreeNode<ObjectTypeDefinitionNode | ObjectTypeExtensionNode>;
const isObjectType = (node: ObjectTypeNode): boolean =>
[Kind.OBJECT_TYPE_DEFINITION, Kind.OBJECT_TYPE_EXTENSION].includes(node.type);
export const isQueryType = (node: ObjectTypeNode): boolean => isObjectType(node) && node.name.value === 'Query';
export const isMutationType = (node: ObjectTypeNode): boolean => isObjectType(node) && node.name.value === 'Mutation';
export const isSubscriptionType = (node: ObjectTypeNode): boolean =>
isObjectType(node) && node.name.value === 'Subscription';
export enum CaseStyle {
camelCase = 'camelCase',
pascalCase = 'PascalCase',
snakeCase = 'snake_case',
upperCase = 'UPPER_CASE',
kebabCase = 'kebab-case',
}
const pascalCase = (str: string): string =>
lowerCase(str)
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
export const camelCase = (str: string): string => {
const result = pascalCase(str);
return result.charAt(0).toLowerCase() + result.slice(1);
};
export const convertCase = (style: CaseStyle, str: string): string => {
switch (style) {
case CaseStyle.camelCase:
return camelCase(str);
case CaseStyle.pascalCase:
return pascalCase(str);
case CaseStyle.snakeCase:
return lowerCase(str).replace(/ /g, '_');
case CaseStyle.upperCase:
return lowerCase(str).replace(/ /g, '_').toUpperCase();
case CaseStyle.kebabCase:
return lowerCase(str).replace(/ /g, '-');
}
};
export function getLocation(
loc: Partial<AST.SourceLocation>,
fieldName = '',
offset?: { offsetStart?: number; offsetEnd?: number }
): AST.SourceLocation {
const { start } = loc;
/*
* ESLint has 0-based column number
* https://eslint.org/docs/developer-guide/working-with-rules#contextreport
*/
const { offsetStart = 1, offsetEnd = 1 } = offset ?? {};
return {
start: {
line: start.line,
column: start.column - offsetStart,
},
end: {
line: start.line,
column: start.column - offsetEnd + fieldName.length,
},
};
}