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