1 | import autoBind from 'auto-bind';
|
2 | import { convertFactory } from './naming.js';
|
3 | export class BaseVisitor {
|
4 | constructor(rawConfig, additionalConfig) {
|
5 | var _a;
|
6 | this._declarationBlockConfig = {};
|
7 | this._parsedConfig = {
|
8 | convert: convertFactory(rawConfig),
|
9 | typesPrefix: rawConfig.typesPrefix || '',
|
10 | typesSuffix: rawConfig.typesSuffix || '',
|
11 | externalFragments: rawConfig.externalFragments || [],
|
12 | fragmentImports: rawConfig.fragmentImports || [],
|
13 | addTypename: !rawConfig.skipTypename,
|
14 | nonOptionalTypename: !!rawConfig.nonOptionalTypename,
|
15 | useTypeImports: !!rawConfig.useTypeImports,
|
16 | dedupeFragments: !!rawConfig.dedupeFragments,
|
17 | allowEnumStringTypes: !!rawConfig.allowEnumStringTypes,
|
18 | inlineFragmentTypes: (_a = rawConfig.inlineFragmentTypes) !== null && _a !== void 0 ? _a : 'inline',
|
19 | emitLegacyCommonJSImports: rawConfig.emitLegacyCommonJSImports === undefined ? true : !!rawConfig.emitLegacyCommonJSImports,
|
20 | ...(additionalConfig || {}),
|
21 | };
|
22 | this.scalars = {};
|
23 | Object.keys(this.config.scalars || {}).forEach(key => {
|
24 | this.scalars[key] = this.config.scalars[key].type;
|
25 | });
|
26 | autoBind(this);
|
27 | }
|
28 | getVisitorKindContextFromAncestors(ancestors) {
|
29 | if (!ancestors) {
|
30 | return [];
|
31 | }
|
32 | return ancestors.map(t => t.kind).filter(Boolean);
|
33 | }
|
34 | get config() {
|
35 | return this._parsedConfig;
|
36 | }
|
37 | convertName(node, options) {
|
38 | const useTypesPrefix = typeof (options && options.useTypesPrefix) === 'boolean' ? options.useTypesPrefix : true;
|
39 | const useTypesSuffix = typeof (options && options.useTypesSuffix) === 'boolean' ? options.useTypesSuffix : true;
|
40 | let convertedName = '';
|
41 | if (useTypesPrefix) {
|
42 | convertedName += this.config.typesPrefix;
|
43 | }
|
44 | convertedName += this.config.convert(node, options);
|
45 | if (useTypesSuffix) {
|
46 | convertedName += this.config.typesSuffix;
|
47 | }
|
48 | return convertedName;
|
49 | }
|
50 | getOperationSuffix(node, operationType) {
|
51 | const { omitOperationSuffix = false, dedupeOperationSuffix = false } = this.config;
|
52 | const operationName = typeof node === 'string' ? node : node.name ? node.name.value : '';
|
53 | return omitOperationSuffix
|
54 | ? ''
|
55 | : dedupeOperationSuffix && operationName.toLowerCase().endsWith(operationType.toLowerCase())
|
56 | ? ''
|
57 | : operationType;
|
58 | }
|
59 | getFragmentSuffix(node) {
|
60 | return this.getOperationSuffix(node, 'Fragment');
|
61 | }
|
62 | getFragmentName(node) {
|
63 | return this.convertName(node, {
|
64 | suffix: this.getFragmentSuffix(node),
|
65 | useTypesPrefix: false,
|
66 | });
|
67 | }
|
68 | getFragmentVariableName(node) {
|
69 | const { omitOperationSuffix = false, dedupeOperationSuffix = false, fragmentVariableSuffix = 'FragmentDoc', fragmentVariablePrefix = '', } = this.config;
|
70 | const fragmentName = typeof node === 'string' ? node : node.name.value;
|
71 | const suffix = omitOperationSuffix
|
72 | ? ''
|
73 | : dedupeOperationSuffix &&
|
74 | fragmentName.toLowerCase().endsWith('fragment') &&
|
75 | fragmentVariableSuffix.toLowerCase().startsWith('fragment')
|
76 | ? fragmentVariableSuffix.substring('fragment'.length)
|
77 | : fragmentVariableSuffix;
|
78 | return this.convertName(node, {
|
79 | prefix: fragmentVariablePrefix,
|
80 | suffix,
|
81 | useTypesPrefix: false,
|
82 | });
|
83 | }
|
84 | getPunctuation(_declarationKind) {
|
85 | return '';
|
86 | }
|
87 | }
|