@graphql-codegen/flutter-freezed
Version:
GraphQL Code Generator plugin to generate Freezed models from your GraphQL schema
130 lines (127 loc) • 6.17 kB
JavaScript
import { appliesOnBlock } from '../utils.js';
import { Pattern } from './pattern.js';
import { APPLIES_ON_ALL_BLOCKS, DART_SCALARS, defaultFreezedPluginConfig, } from './plugin-config.js';
export class Config {
}
Config.camelCasedEnums = (config) => {
const _camelCasedEnums = config.camelCasedEnums;
if (_camelCasedEnums === true) {
return 'camelCase';
}
else if (_camelCasedEnums === false) {
return undefined;
}
return _camelCasedEnums;
};
Config.copyWith = (config, typeName) => {
return Config.enableWithBooleanOrTypeFieldName(config.copyWith, typeName);
};
Config.customScalars = (config, graphqlScalar) => {
var _a, _b, _c;
return (_c = (_b = (_a = config.customScalars) === null || _a === void 0 ? void 0 : _a[graphqlScalar]) !== null && _b !== void 0 ? _b : DART_SCALARS[graphqlScalar]) !== null && _c !== void 0 ? _c : graphqlScalar;
};
Config.defaultValues = (config, blockAppliesOn, typeName, fieldName) => {
var _a, _b, _c, _d;
const decorator = (defaultValue) => (defaultValue ? `@Default(${defaultValue})\n` : '');
const defaultValue = (_d = (_c = (_b = (_a = config.defaultValues) === null || _a === void 0 ? void 0 : _a.filter(([pattern, , configAppliesOn]) => Pattern.findLastConfiguration(pattern, typeName, fieldName) &&
appliesOnBlock(configAppliesOn, blockAppliesOn))) === null || _b === void 0 ? void 0 : _b.slice(-1)) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d[1];
return decorator(defaultValue);
};
Config.deprecated = (config, blockAppliesOn, typeName, fieldName) => {
var _a, _b, _c;
const isDeprecated = ((_c = (_b = (_a = config.deprecated) === null || _a === void 0 ? void 0 : _a.filter(([pattern, configAppliesOn]) => Pattern.findLastConfiguration(pattern, typeName, fieldName) &&
appliesOnBlock(configAppliesOn, blockAppliesOn))) === null || _b === void 0 ? void 0 : _b.slice(-1)) === null || _c === void 0 ? void 0 : _c[0]) !== undefined;
return isDeprecated ? '@deprecated\n' : '';
};
Config.equal = (config, typeName) => {
return Config.enableWithBooleanOrTypeFieldName(config.equal, typeName);
};
Config.escapeDartKeywords = (config, blockAppliesOn, typeName, fieldName) => {
const escapeDartKeywords = config.escapeDartKeywords;
if (escapeDartKeywords === true) {
return ['', '_']; // use a suffix `_`
}
else if (typeName && Array.isArray(escapeDartKeywords) && escapeDartKeywords.length > 0) {
const [, prefix, suffix] = escapeDartKeywords
.filter(([pattern, , , configAppliesOn]) => Pattern.findLastConfiguration(pattern, typeName, fieldName) &&
appliesOnBlock(configAppliesOn !== null && configAppliesOn !== void 0 ? configAppliesOn : [...APPLIES_ON_ALL_BLOCKS], blockAppliesOn))
.slice(-1)[0];
return [prefix, suffix];
}
return ['', '']; // no suffix
};
Config.final = (config, blockAppliesOn, typeName, fieldName) => {
var _a, _b, _c;
return (((_c = (_b = (_a = config.final) === null || _a === void 0 ? void 0 : _a.filter(([pattern, configAppliesOn]) => Pattern.findLastConfiguration(pattern, typeName, fieldName) &&
appliesOnBlock(configAppliesOn, blockAppliesOn))) === null || _b === void 0 ? void 0 : _b.slice(-1)) === null || _c === void 0 ? void 0 : _c[0]) !== undefined);
};
/* static fromJsonToJson = ( // TODO: @next-version
config: FlutterFreezedPluginConfig,
blockAppliesOn?: readonly AppliesOnParameters[],
typeName?: TypeName,
fieldName?: FieldName
) => {
const fromJsonToJson = config.fromJsonToJson;
if (typeName && fieldName && Array.isArray(fromJsonToJson)) {
const [, classOrFunctionName, useClassConverter] = (fromJsonToJson ?? [])
?.filter(
([pattern, , , appliesOn]) =>
Pattern.findLastConfiguration(pattern, typeName, fieldName) && appliesOnBlock(appliesOn, blockAppliesOn)
)
?.slice(-1)?.[0];
return [classOrFunctionName, useClassConverter] as [classOrFunctionName: string, useClassConverter?: boolean];
} else if (typeName && fromJsonToJson instanceof TypeNamePattern) {
return Pattern.findLastConfiguration(fromJsonToJson, typeName);
}
return fromJsonToJson as boolean;
};
*/
Config.ignoreTypes = (config, typeName) => {
const ignoreTypes = config.ignoreTypes;
if (ignoreTypes) {
const isIgnored = Pattern.findLastConfiguration(ignoreTypes, typeName);
return isIgnored ? [typeName.value] : [];
}
return [];
};
Config.immutable = (config, typeName) => {
return Config.enableWithBooleanOrTypeFieldName(config.immutable, typeName);
};
Config.makeCollectionsUnmodifiable = (config, typeName) => {
return Config.enableWithBooleanOrTypeFieldName(config.makeCollectionsUnmodifiable, typeName);
};
Config.mergeTypes = (config, typeName) => {
var _a, _b;
return (_b = (_a = config.mergeTypes) === null || _a === void 0 ? void 0 : _a[typeName.value]) !== null && _b !== void 0 ? _b : [];
};
Config.mutableInputs = (config, typeName) => {
return Config.enableWithBooleanOrTypeFieldName(config.mutableInputs, typeName);
};
Config.privateEmptyConstructor = (config, typeName) => {
return Config.enableWithBooleanOrTypeFieldName(config.privateEmptyConstructor, typeName);
};
Config.unionClass = ( /* config: FlutterFreezedPluginConfig, index: number, unionTypeName: TypeName */) => {
// const unionClass = config['unionClass'];
return undefined;
};
Config.unionKey = ( /* config: FlutterFreezedPluginConfig, typeName: TypeName */) => {
return undefined;
};
Config.unionValueCase = ( /* config: FlutterFreezedPluginConfig, typeName: TypeName */) => {
return undefined;
};
Config.unionValueFactoryDecorator = () => {
return undefined;
};
Config.enableWithBooleanOrTypeFieldName = (value, typeName) => {
if (typeof value === 'boolean') {
return value;
}
else if (value !== undefined && typeName !== undefined) {
return Pattern.findLastConfiguration(value, typeName);
}
return undefined;
};
Config.create = (...config) => {
return Object.assign({}, defaultFreezedPluginConfig, ...config);
};