@query-key-gen/generator
Version:
Generated for [Vite](https://vitejs.dev)
1,412 lines (1,395 loc) • 57.4 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
import path3 from "path";
// ../common/src/core/Programmer.ts
import fg from "fast-glob";
import path from "path";
import ts from "typescript";
var Programmer = class {
constructor(config) {
// getCompilerOptions = () => {
// const { options: compilerOptions } = ts.parseJsonConfigFileContent(
// ts.readConfigFile('tsconfig.node.json', ts.sys.readFile).config,
// {
// fileExists: ts.sys.fileExists,
// readFile: ts.sys.readFile,
// readDirectory: ts.sys.readDirectory,
// useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames
// },
// path.dirname('.')
// );
// return compilerOptions;
// };
this.findFiles = (program) => __async(this, null, function* () {
const config = this.config;
const sourceFiles = program.getSourceFiles().filter((item) => {
return !item.isDeclarationFile;
});
const outputFile = ts.createSourceFile(config.output, "", this.compilerOptions.target);
const projectFiles = sourceFiles.filter((item) => {
const relativePath = path.relative(process.cwd(), item.fileName);
const outputPath = path.relative(process.cwd(), config.output);
return !relativePath.includes(outputPath);
});
return {
outputFile,
projectFiles
};
});
this.config = config;
this.compilerOptions = {
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ESNext
};
}
// Getter로 createProgram 정의
get create() {
return () => __async(this, null, function* () {
const files = yield fg(this.config.path, {
onlyFiles: true
});
const builder = ts.createIncrementalProgram({
rootNames: files,
options: this.compilerOptions
});
const program = builder.getProgram();
return {
program,
checker: program.getTypeChecker()
};
});
}
};
// ../common/src/core/GenericRunner.ts
var GenericRunner = class {
constructor(programmer, config) {
this.programmer = programmer;
this.config = config;
}
process() {
return __async(this, null, function* () {
console.log("\u{1F527} Base process logic");
});
}
execute() {
return this.process();
}
};
// src/core/QueryKeyGenerator.ts
import ts13 from "typescript";
// src/factories/ExportSpecifierFactory.ts
import ts2 from "typescript";
var ExportSpecifierFactory;
((ExportSpecifierFactory2) => {
const factory2 = ts2.factory;
const exportDeclaration = (exportSpecifier) => {
return factory2.createExportDeclaration(
void 0,
false,
exportSpecifier,
void 0,
void 0
);
};
ExportSpecifierFactory2.named = (name) => {
return exportDeclaration(
factory2.createNamedExports([
factory2.createExportSpecifier(false, void 0, factory2.createIdentifier(name))
])
);
};
})(ExportSpecifierFactory || (ExportSpecifierFactory = {}));
// src/factories/ObjectLiteralExpressionFactory.ts
import ts3 from "typescript";
var ObjectLiteralExpressionFactory;
((ObjectLiteralExpressionFactory2) => {
const factory2 = ts3.factory;
const objectLiteralExpression = (properties) => {
return factory2.createObjectLiteralExpression(properties, true);
};
ObjectLiteralExpressionFactory2.write = (properties) => {
return objectLiteralExpression(properties);
};
})(ObjectLiteralExpressionFactory || (ObjectLiteralExpressionFactory = {}));
// src/factories/QueryKeyStructureFactory.ts
import ts6 from "typescript";
// src/types/QueryNodeType.ts
var QueryNodeKind = {
StringLiteral: "StringLiteral",
Symbol: "Symbol",
EnumMember: "EnumMember",
TypeOfKeyword: "TypeOfKeyword",
Tuple: "Tuple"
};
// src/utils/index.ts
import { execSync } from "child_process";
import fs from "fs";
import path2 from "path";
import ts4, { factory } from "typescript";
var TypeGuard = {
symbol: (node) => {
return typeof node === "object" && "escapedName" in node;
},
stringLiteral: (node) => {
return ts4.isStringLiteral(node);
},
tsType: (node) => {
return typeof node === "object" && "symbol" in node;
},
tuple: (node) => {
return ts4.isTupleTypeNode(node);
}
};
var nodeByName = {
identifier: (name) => {
return factory.createIdentifier(name);
},
stringLiteral: (name) => {
return factory.createStringLiteral(name.replace(/'/g, ""));
}
};
var toCamelCase = (str) => {
return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, letter) => letter.toUpperCase());
};
var makeQueryKeyName = (literal, separator) => {
return literal.map((node) => {
const name = node.name;
if (node.kind !== QueryNodeKind.StringLiteral) {
return `{${name}}`;
}
return name;
}).join(separator);
};
var groupByFirstName = (data) => {
return data.reduce(
(acc, group) => {
if (group.length > 0) {
const firstItem = group[0];
const firstItemName = firstItem == null ? void 0 : firstItem.name;
if (!firstItemName) {
return acc;
}
const camelCaseName = toCamelCase(firstItemName);
if (!acc[camelCaseName]) {
acc[camelCaseName] = [];
}
acc[camelCaseName].push(group);
}
return acc;
},
{}
);
};
var prettify = (output) => {
const prettier = path2.resolve("./node_modules/.bin/prettier");
if (fs.existsSync(prettier)) execSync(`${prettier} --write --cache ${output}`);
};
var logger = (() => {
const prefix = "[QUERY-KEY-GENERATOR]: ";
const log = () => {
return {
warn: (msg) => {
console.warn(`[WARN]${prefix}${msg}`);
},
error: (msg) => {
console.error(`[ERROR]${prefix}${msg}`);
},
info: (msg) => {
console.log(`[INFO]${prefix}${msg}`);
}
};
};
return log();
})();
// src/factories/TypeConverterFactory.ts
import ts5 from "typescript";
var TypeConverterFactory;
((TypeConverterFactory2) => {
const factory2 = ts5.factory;
const typeCache = /* @__PURE__ */ new WeakMap();
TypeConverterFactory2.transform = (symbol, checker, isTypeReference) => {
const type = checker.getTypeOfSymbol(symbol);
const declaration = symbol.declarations;
const valueDeclaration = symbol.valueDeclaration;
if (valueDeclaration && ts5.isParameter(valueDeclaration)) {
const type2 = valueDeclaration.type;
if (type2) {
if (ts5.isTupleTypeNode(type2)) {
return (0, TypeConverterFactory2.tuple)(type2, checker);
}
if (ts5.isArrayTypeNode(type2)) {
return array(type2, checker);
}
if (ts5.isUnionTypeNode(type2)) {
return unionOrIntersection(type2, checker, true);
}
if (ts5.isTypeLiteralNode(type2)) {
return typeLiteral(type2, checker);
}
if (ts5.isIntersectionTypeNode(type2)) {
return unionOrIntersection(type2, checker, false);
}
if (ts5.isTypeReferenceNode(type2)) {
const symbol2 = checker.getSymbolAtLocation(type2.typeName);
if (symbol2) {
return (0, TypeConverterFactory2.transform)(symbol2, checker, true);
}
}
}
}
if (valueDeclaration && ts5.isEnumDeclaration(valueDeclaration)) {
return (0, TypeConverterFactory2.enumToUnionFromEnumDeclaration)(valueDeclaration, checker);
}
if (declaration && isTypeReference) {
const typeNode2 = typeDeclaration(declaration, checker);
if (typeNode2) {
return typeNode2;
}
}
const typeName = checker.typeToString(type);
const isKeywordTypeNode = keywordTypeNode(typeName);
if (isKeywordTypeNode) {
return isKeywordTypeNode;
}
if (type.isIntersection()) {
return intersection(type, checker);
}
if (type.isNumberLiteral()) {
return factory2.createLiteralTypeNode(factory2.createNumericLiteral(typeName));
}
if (type.isStringLiteral()) {
const name = typeName.replace(/"/g, "");
return factory2.createLiteralTypeNode(factory2.createStringLiteral(name));
}
if (typeCache.has(type)) {
return typeCache.get(type);
}
const typeNode = literal(type, checker);
typeCache.set(type, typeNode);
return typeNode;
};
const keywordTypeNode = (name) => {
switch (name) {
case "string":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.StringKeyword);
case "number":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.NumberKeyword);
case "boolean":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.BooleanKeyword);
case "any":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.AnyKeyword);
case "undefined":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.UndefinedKeyword);
case "null":
return factory2.createLiteralTypeNode(factory2.createNull());
case "unknown":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.UnknownKeyword);
case "never":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.NeverKeyword);
case "object":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.ObjectKeyword);
case "void":
return factory2.createKeywordTypeNode(ts5.SyntaxKind.VoidKeyword);
}
return void 0;
};
const literalKindToKeyword = (element) => {
const literal2 = element.literal;
switch (literal2.kind) {
case ts5.SyntaxKind.NullKeyword:
return keywordTypeNode("null");
case ts5.SyntaxKind.TrueKeyword:
return factory2.createLiteralTypeNode(factory2.createTrue());
case ts5.SyntaxKind.FalseKeyword:
return factory2.createLiteralTypeNode(factory2.createFalse());
case ts5.SyntaxKind.NumericLiteral:
return factory2.createLiteralTypeNode(
factory2.createNumericLiteral(literal2.text)
);
case ts5.SyntaxKind.StringLiteral:
return factory2.createLiteralTypeNode(
factory2.createStringLiteral(literal2.text)
);
case ts5.SyntaxKind.BigIntLiteral:
return factory2.createLiteralTypeNode(
factory2.createBigIntLiteral(literal2.text)
);
}
return void 0;
};
const literal = (type, checker) => {
const properties = type.getProperties().map((prop) => {
const isOptional = TypeConverterFactoryGuard.isOptional(prop);
return factory2.createPropertySignature(
void 0,
prop.getName(),
isOptional ? factory2.createToken(ts5.SyntaxKind.QuestionToken) : void 0,
(0, TypeConverterFactory2.transform)(prop, checker)
);
});
return factory2.createTypeLiteralNode(properties);
};
const generateProperties = (type, checker, isReadonly) => {
const properties = type.getProperties();
const propertyNames = /* @__PURE__ */ new Set();
properties.forEach((prop) => {
propertyNames.add(prop.getName());
});
return properties.map((prop) => {
return factory2.createPropertySignature(
isReadonly ? [factory2.createToken(ts5.SyntaxKind.ReadonlyKeyword)] : void 0,
prop.getName(),
void 0,
(0, TypeConverterFactory2.transform)(prop, checker)
);
});
};
const unionOrIntersection = (typeNode, checker, isUnion) => {
const types = typeNode.types;
const node = types.map((element) => {
const kind = element.kind;
if (kind === ts5.SyntaxKind.StringKeyword) {
return keywordTypeNode("string");
}
if (kind === ts5.SyntaxKind.NumberKeyword) {
return keywordTypeNode("number");
}
if (kind === ts5.SyntaxKind.BooleanKeyword) {
return keywordTypeNode("boolean");
}
if (kind === ts5.SyntaxKind.UndefinedKeyword) {
return keywordTypeNode("undefined");
}
if (kind === ts5.SyntaxKind.ObjectKeyword) {
return keywordTypeNode("object");
}
if (kind === ts5.SyntaxKind.AnyKeyword) {
return keywordTypeNode("any");
}
if (ts5.isLiteralTypeNode(element)) {
const keyword = literalKindToKeyword(element);
if (keyword) {
return keyword;
}
}
if (ts5.isTypeLiteralNode(element)) {
return typeLiteral(element, checker);
}
if (ts5.isTypeReferenceNode(element)) {
const symbol = checker.getSymbolAtLocation(element.typeName);
if (symbol) {
return (0, TypeConverterFactory2.transform)(symbol, checker, true);
}
}
return void 0;
}).filter(Boolean);
return isUnion ? factory2.createUnionTypeNode(node) : factory2.createIntersectionTypeNode(node);
};
const intersection = (intersection2, checker) => {
const types = intersection2.types;
const intersectionNode = types.map((t) => {
const typeName = checker.typeToString(t);
if (TypeConverterFactoryGuard.isObjectType(t)) {
const properties = generateProperties(t, checker);
return factory2.createTypeLiteralNode(properties);
}
if (TypeConverterFactoryGuard.isLiteralType(t)) {
return factory2.createLiteralTypeNode(
factory2.createStringLiteral(typeName.replace(/"/g, ""))
);
}
return keywordTypeNode(typeName);
}).filter(Boolean);
return factory2.createIntersectionTypeNode(intersectionNode);
};
TypeConverterFactory2.enumToUnion = (type, checker) => {
var _a, _b, _c;
const enumValues = Array.from((_c = (_b = (_a = type.symbol) == null ? void 0 : _a.exports) == null ? void 0 : _b.values()) != null ? _c : []);
const unionType = enumValues.map((value) => {
const declaration = value.valueDeclaration;
if (declaration && ts5.isEnumMember(declaration)) {
const name = value.getName();
const enumValue = checker.getConstantValue(declaration);
if (typeof enumValue === "string") {
return ts5.factory.createLiteralTypeNode(
ts5.factory.createStringLiteral(enumValue)
);
} else if (typeof enumValue === "number") {
return ts5.factory.createLiteralTypeNode(
ts5.factory.createNumericLiteral(enumValue.toString())
);
} else {
return ts5.factory.createTypeReferenceNode(name, void 0);
}
}
return void 0;
}).filter(Boolean);
return factory2.createUnionTypeNode(unionType);
};
TypeConverterFactory2.enumToUnionFromEnumDeclaration = (type, checker) => {
const members = type.members;
const unionType = members.map((member) => {
const initial = member.initializer;
if (!initial) return void 0;
if (ts5.isStringLiteral(initial)) {
return factory2.createLiteralTypeNode(factory2.createStringLiteral(initial.text));
}
if (ts5.isNumericLiteral(initial)) {
return factory2.createLiteralTypeNode(
factory2.createNumericLiteral(initial.text)
);
}
if (ts5.isPropertyAccessExpression(initial)) {
const symbol = checker.getSymbolAtLocation(initial.name);
if (symbol) {
return (0, TypeConverterFactory2.transform)(symbol, checker);
}
}
return void 0;
}).filter(Boolean);
return factory2.createUnionTypeNode(unionType);
};
TypeConverterFactory2.typeofKeyword = (type, checker) => {
const properties = generateProperties(type, checker, true);
return factory2.createTypeLiteralNode(properties);
};
TypeConverterFactory2.tuple = (type, checker) => {
const elements = type.elements;
const node = elements.map((element) => {
const kind = element.kind;
if (kind === ts5.SyntaxKind.StringKeyword) {
return keywordTypeNode("string");
}
if (kind === ts5.SyntaxKind.NumberKeyword) {
return keywordTypeNode("number");
}
if (kind === ts5.SyntaxKind.BooleanKeyword) {
return keywordTypeNode("boolean");
}
if (kind === ts5.SyntaxKind.UndefinedKeyword) {
return keywordTypeNode("undefined");
}
if (kind === ts5.SyntaxKind.ObjectKeyword) {
return keywordTypeNode("object");
}
if (kind === ts5.SyntaxKind.AnyKeyword) {
return keywordTypeNode("any");
}
if (kind === ts5.SyntaxKind.UnknownKeyword) {
return keywordTypeNode("unknown");
}
if (kind === ts5.SyntaxKind.NeverKeyword) {
return keywordTypeNode("never");
}
if (kind === ts5.SyntaxKind.VoidKeyword) {
return keywordTypeNode("void");
}
if (ts5.isLiteralTypeNode(element)) {
const keyword = literalKindToKeyword(element);
if (keyword) {
return keyword;
}
}
if (ts5.isTypeLiteralNode(element)) {
return typeLiteral(element, checker);
}
if (ts5.isTypeReferenceNode(element)) {
const symbol = checker.getSymbolAtLocation(element.typeName);
if (symbol) {
return (0, TypeConverterFactory2.transform)(symbol, checker);
}
}
return void 0;
}).filter(Boolean);
return factory2.createTupleTypeNode(node);
};
const array = (type, checker) => {
const element = type.elementType;
if (element.kind === ts5.SyntaxKind.StringKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.StringKeyword)
);
}
if (element.kind === ts5.SyntaxKind.NumberKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.NumberKeyword)
);
}
if (element.kind === ts5.SyntaxKind.BooleanKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.BooleanKeyword)
);
}
if (element.kind === ts5.SyntaxKind.UndefinedKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.UndefinedKeyword)
);
}
if (element.kind === ts5.SyntaxKind.NullKeyword) {
return factory2.createArrayTypeNode(factory2.createLiteralTypeNode(factory2.createNull()));
}
if (element.kind === ts5.SyntaxKind.AnyKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.AnyKeyword)
);
}
if (element.kind === ts5.SyntaxKind.ObjectKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.ObjectKeyword)
);
}
if (element.kind === ts5.SyntaxKind.UnknownKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.UnknownKeyword)
);
}
if (element.kind === ts5.SyntaxKind.NeverKeyword) {
return factory2.createArrayTypeNode(
factory2.createKeywordTypeNode(ts5.SyntaxKind.NeverKeyword)
);
}
if (ts5.isTypeLiteralNode(element)) {
return factory2.createArrayTypeNode(typeLiteral(element, checker));
}
if (ts5.isParenthesizedTypeNode(element)) {
const type2 = element.type;
if (ts5.isUnionTypeNode(type2)) {
return factory2.createArrayTypeNode(unionOrIntersection(type2, checker, true));
}
if (ts5.isIntersectionTypeNode(type2)) {
return factory2.createArrayTypeNode(unionOrIntersection(type2, checker, false));
}
}
return void 0;
};
const typeLiteral = (type, checker) => {
const properties = type.members.map((member) => {
const name = member.name;
if (ts5.isPropertySignature(member) && member.type && name) {
const type2 = member.type;
if (type2.kind === ts5.SyntaxKind.StringKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createKeywordTypeNode(ts5.SyntaxKind.StringKeyword)
);
}
if (type2.kind === ts5.SyntaxKind.NumberKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createKeywordTypeNode(ts5.SyntaxKind.NumberKeyword)
);
}
if (type2.kind === ts5.SyntaxKind.BooleanKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createKeywordTypeNode(ts5.SyntaxKind.BooleanKeyword)
);
}
if (type2.kind === ts5.SyntaxKind.NullKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createLiteralTypeNode(factory2.createNull())
);
}
if (type2.kind === ts5.SyntaxKind.AnyKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createKeywordTypeNode(ts5.SyntaxKind.AnyKeyword)
);
}
if (type2.kind === ts5.SyntaxKind.UndefinedKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createKeywordTypeNode(ts5.SyntaxKind.UndefinedKeyword)
);
}
if (type2.kind === ts5.SyntaxKind.LiteralType && ts5.isLiteralTypeNode(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
literalKindToKeyword(type2)
);
}
if (type2.kind === ts5.SyntaxKind.UnknownKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
keywordTypeNode("unknown")
);
}
if (type2.kind === ts5.SyntaxKind.NeverKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
keywordTypeNode("never")
);
}
if (type2.kind === ts5.SyntaxKind.VoidKeyword) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
keywordTypeNode("void")
);
}
if (ts5.isUnionTypeNode(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
unionOrIntersection(type2, checker, true)
);
}
if (ts5.isIntersectionTypeNode(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
unionOrIntersection(type2, checker, false)
);
}
if (ts5.isStringLiteral(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createLiteralTypeNode(factory2.createStringLiteral(type2.text))
);
}
if (ts5.isNumericLiteral(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createLiteralTypeNode(factory2.createNumericLiteral(type2.text))
);
}
if (ts5.isTypeLiteralNode(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
typeLiteral(type2, checker)
);
}
if (ts5.isTypeReferenceNode(type2)) {
const symbol = checker.getSymbolAtLocation(type2.typeName);
const typeArgument = type2.typeArguments;
if (symbol && !typeArgument) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
(0, TypeConverterFactory2.transform)(symbol, checker, true)
);
}
if (symbol && typeArgument) {
const node = typeArgument.map((t) => {
if (ts5.isTypeReferenceNode(t)) {
const symbol2 = checker.getSymbolAtLocation(t.typeName);
if (symbol2) {
return (0, TypeConverterFactory2.transform)(symbol2, checker, true);
}
}
if (ts5.isLiteralTypeNode(t)) {
return literalKindToKeyword(t);
}
if (ts5.isTypeLiteralNode(t)) {
return typeLiteral(t, checker);
}
if (ts5.isUnionTypeNode(t)) {
return unionOrIntersection(t, checker, true);
}
if (ts5.isIntersectionTypeNode(t)) {
return unionOrIntersection(t, checker, false);
}
return t;
}).filter(Boolean);
return factory2.createPropertySignature(
void 0,
name,
void 0,
factory2.createTypeReferenceNode(
factory2.createIdentifier("Record"),
node
)
);
}
}
if (ts5.isTupleTypeNode(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
(0, TypeConverterFactory2.tuple)(type2, checker)
);
}
if (ts5.isArrayTypeNode(type2)) {
return factory2.createPropertySignature(
void 0,
name,
void 0,
array(type2, checker)
);
}
}
return void 0;
}).filter(Boolean);
return factory2.createTypeLiteralNode(properties);
};
const typeDeclaration = (type, checker) => {
return type.map((declaration) => {
if (ts5.isTypeAliasDeclaration(declaration)) {
const type2 = declaration.type;
if (ts5.isTypeLiteralNode(type2)) {
return typeLiteral(type2, checker);
}
}
if (ts5.isInterfaceDeclaration(declaration)) {
return void 0;
}
return void 0;
}).filter(Boolean)[0];
};
})(TypeConverterFactory || (TypeConverterFactory = {}));
var TypeConverterFactoryGuard = {
isObjectType: (type) => {
return !!(type.flags & ts5.TypeFlags.Object);
},
isLiteralType: (type) => {
return !!(type.flags & ts5.TypeFlags.StringLiteral) || !!(type.flags & ts5.TypeFlags.NumberLiteral) || !!(type.flags & ts5.TypeFlags.BooleanLiteral);
},
isOptional: (symbol) => {
return symbol.valueDeclaration && (ts5.isParameter(symbol.valueDeclaration) || ts5.isPropertySignature(symbol.valueDeclaration)) ? !!symbol.valueDeclaration.questionToken : !!(symbol.getFlags() & ts5.SymbolFlags.Optional);
}
};
// src/factories/QueryKeyStructureFactory.ts
var QueryKeyStructureFactory;
((QueryKeyStructureFactory2) => {
const factory2 = ts6.factory;
QueryKeyStructureFactory2.write = ({
identifierName,
node,
checker
}) => {
const isStringLiteral = node.every((item) => TypeGuard.stringLiteral(item.type));
const arrayLiteralExpression = arrayLiteral(node);
if (isStringLiteral) {
return factory2.createPropertyAssignment(
factory2.createIdentifier(identifierName),
arrayLiteralExpression
);
}
const parameterDeclarations = parameter(
node.filter((item) => !TypeGuard.stringLiteral(item.type)),
checker
);
return factory2.createPropertyAssignment(
factory2.createIdentifier(identifierName),
factory2.createArrowFunction(
void 0,
void 0,
[...parameterDeclarations],
void 0,
factory2.createToken(ts6.SyntaxKind.EqualsGreaterThanToken),
arrayLiteralExpression
)
);
};
const arrayLiteral = (node) => {
const literal = node.map((item) => {
if (TypeGuard.stringLiteral(item.type)) {
return nodeByName.stringLiteral(item.name);
}
if (TypeGuard.symbol(item.type)) {
return nodeByName.identifier(item.name);
}
if (TypeGuard.tsType(item.type)) {
return nodeByName.identifier(item.name);
}
return nodeByName.identifier(item.name);
}).filter(Boolean);
const arrayLiteralExpression = factory2.createArrayLiteralExpression(literal, false);
return arrayLiteralExpression;
};
const parameter = (node, checker) => {
return node.map((item) => {
if (TypeGuard.symbol(item.type)) {
const isOptional = TypeConverterFactoryGuard.isOptional(item.type);
return factory2.createParameterDeclaration(
void 0,
void 0,
nodeByName.identifier(item.name),
isOptional ? factory2.createToken(ts6.SyntaxKind.QuestionToken) : void 0,
TypeConverterFactory.transform(item.type, checker),
void 0
);
}
if (TypeGuard.tsType(item.type) && item.kind === QueryNodeKind.EnumMember) {
const isOptional = TypeConverterFactoryGuard.isOptional(item.symbol);
return factory2.createParameterDeclaration(
void 0,
void 0,
nodeByName.identifier(item.name),
isOptional ? factory2.createToken(ts6.SyntaxKind.QuestionToken) : void 0,
TypeConverterFactory.enumToUnion(item.type, checker),
void 0
);
}
if (TypeGuard.tsType(item.type) && item.kind === QueryNodeKind.TypeOfKeyword) {
const isOptional = TypeConverterFactoryGuard.isOptional(item.symbol);
const typeOfNode = TypeConverterFactory.typeofKeyword(item.type, checker);
return factory2.createParameterDeclaration(
void 0,
void 0,
nodeByName.identifier(item.name),
isOptional ? factory2.createToken(ts6.SyntaxKind.QuestionToken) : void 0,
typeOfNode,
void 0
);
}
if (TypeGuard.tuple(item.type) && item.kind === QueryNodeKind.Tuple) {
const isOptional = TypeConverterFactoryGuard.isOptional(item.symbol);
const tupleNode = TypeConverterFactory.tuple(item.type, checker);
return factory2.createParameterDeclaration(
void 0,
void 0,
nodeByName.identifier(item.name),
isOptional ? factory2.createToken(ts6.SyntaxKind.QuestionToken) : void 0,
tupleNode,
void 0
);
}
return void 0;
}).filter(Boolean);
};
})(QueryKeyStructureFactory || (QueryKeyStructureFactory = {}));
// src/factories/QueryKeyStatementFactory.ts
import ts8 from "typescript";
// src/factories/VariableDeclarationFactory.ts
import ts7 from "typescript";
var VariableDeclarationFactory;
((VariableDeclarationFactory2) => {
const factory2 = ts7.factory;
VariableDeclarationFactory2.write = (name, node, isConst = false) => {
const typeNode = () => {
if (isConst) {
return factory2.createAsExpression(
node,
factory2.createTypeReferenceNode(factory2.createIdentifier("const"), void 0)
);
}
return node;
};
return factory2.createVariableDeclaration(
factory2.createIdentifier(name),
void 0,
void 0,
typeNode()
);
};
})(VariableDeclarationFactory || (VariableDeclarationFactory = {}));
var VariableDeclarationListFactory;
((VariableDeclarationListFactory2) => {
const factory2 = ts7.factory;
VariableDeclarationListFactory2.write = (variables, flag = ts7.NodeFlags.Const) => {
return factory2.createVariableDeclarationList(variables, flag);
};
})(VariableDeclarationListFactory || (VariableDeclarationListFactory = {}));
// src/factories/QueryKeyStatementFactory.ts
var QueryKeyStatementFactory;
((QueryKeyStatementFactory2) => {
const factory2 = ts8.factory;
QueryKeyStatementFactory2.write = (propertyAssignments, keyName, factoryPrefix) => {
return factory2.createVariableStatement(
void 0,
VariableDeclarationListFactory.write(
[
VariableDeclarationFactory.write(
keyName + factoryPrefix,
ObjectLiteralExpressionFactory.write([...propertyAssignments]),
true
)
],
ts8.NodeFlags.Const
)
);
};
})(QueryKeyStatementFactory || (QueryKeyStatementFactory = {}));
// src/factories/GlobalQueryKeyStatement.ts
import ts9 from "typescript";
var GlobalQueryKeyStatement;
((GlobalQueryKeyStatement2) => {
const factory2 = ts9.factory;
GlobalQueryKeyStatement2.write = (keyStatement, keyName, factoryPrefix) => {
return factory2.createVariableStatement(
[factory2.createToken(ts9.SyntaxKind.ExportKeyword)],
factory2.createVariableDeclarationList(
[
factory2.createVariableDeclaration(
factory2.createIdentifier(keyName),
void 0,
void 0,
factory2.createAsExpression(
factory2.createObjectLiteralExpression(
[
...keyStatement.map((item) => {
return factory2.createShorthandPropertyAssignment(
factory2.createIdentifier(item.key + factoryPrefix)
);
})
],
true
),
factory2.createTypeReferenceNode(
factory2.createIdentifier("const"),
void 0
)
)
)
],
ts9.NodeFlags.Const
)
);
};
})(GlobalQueryKeyStatement || (GlobalQueryKeyStatement = {}));
// src/queryFinder/QueryKeyFinder.ts
import ts10 from "typescript";
var QueryKeyFinder;
((QueryKeyFinder2) => {
const PROPERTY_NAME = "queryKey";
QueryKeyFinder2.findQueryKey = ({
useQueries,
checker
}) => {
const result = useQueries.map((value) => {
const arrayLiteral = visitor(value, checker);
return arrayLiteral;
}).flat();
return result;
};
const visitor = (value, checker) => {
const arrayLiteral = [];
const visit = (node) => {
if (ts10.isCallExpression(node)) {
const expression = node.expression;
const args = node.arguments;
args.forEach((arg) => {
if (ts10.isPropertyAccessExpression(arg)) {
const symbol = checker.getSymbolAtLocation(arg);
if (symbol && symbol.valueDeclaration) {
const valueDeclaration = symbol.valueDeclaration;
if (ts10.isPropertyAssignment(valueDeclaration)) {
const initializer = valueDeclaration.initializer;
if (initializer && ts10.isCallExpression(initializer)) {
visit(initializer);
}
}
}
}
if (ts10.isCallExpression(arg)) {
const expression2 = arg.expression;
visit(expression2);
}
if (ts10.isIdentifier(arg)) {
const symbol = checker.getSymbolAtLocation(arg);
if (symbol && symbol.valueDeclaration) {
const valueDeclaration = symbol.valueDeclaration;
if (ts10.isVariableDeclaration(valueDeclaration)) {
const initializer = valueDeclaration.initializer;
if (initializer) {
visit(initializer);
}
}
}
}
if (ts10.isObjectLiteralExpression(arg)) {
visit(arg);
}
});
if (ts10.isIdentifier(expression)) {
const symbol = checker.getSymbolAtLocation(expression);
if (symbol && symbol.valueDeclaration) {
const valueDeclaration = symbol.valueDeclaration;
if (ts10.isVariableDeclaration(valueDeclaration)) {
const initializer = valueDeclaration.initializer;
if (initializer && ts10.isCallExpression(initializer)) {
visit(initializer);
}
}
}
}
if (ts10.isPropertyAccessExpression(expression)) {
const symbol = checker.getSymbolAtLocation(expression);
if (symbol && symbol.valueDeclaration) {
const valueDeclaration = symbol.valueDeclaration;
if (ts10.isPropertyAssignment(valueDeclaration)) {
const initializer = valueDeclaration.initializer;
if (initializer && ts10.isArrowFunction(initializer)) {
const body = initializer.body;
if (ts10.isArrayLiteralExpression(body)) {
arrayLiteral.push(makeArrayLiteral(body.elements, checker));
}
if (ts10.isBlock(body)) {
const statements = body.statements;
statements.forEach((statement) => {
if (ts10.isReturnStatement(statement)) {
const expression2 = statement.expression;
if (expression2 && ts10.isCallExpression(expression2)) {
const argument = expression2.arguments;
const callExpression = expression2.expression;
argument.forEach((arg) => {
if (ts10.isObjectLiteralExpression(arg)) {
arrayLiteral.push(
objectExpression(arg, checker)
);
}
});
visit(callExpression);
}
}
});
}
if (ts10.isArrowFunction(body)) {
visit(body);
}
if (ts10.isCallExpression(body)) {
visit(body);
}
}
}
}
}
}
if (ts10.isIdentifier(node)) {
const identifierSymbol = checker.getSymbolAtLocation(node);
if (identifierSymbol) {
const valueDeclaration = identifierSymbol.valueDeclaration;
if (valueDeclaration && ts10.isVariableDeclaration(valueDeclaration)) {
const objectInitializer = valueDeclaration.initializer;
if (objectInitializer && ts10.isObjectLiteralExpression(objectInitializer)) {
const targetProperty = findPropertyByQueryKey(
objectInitializer.properties
);
if (targetProperty && ts10.isPropertyAssignment(targetProperty)) {
const targetInitializer = targetProperty.initializer;
if (ts10.isArrayLiteralExpression(targetInitializer)) {
arrayLiteral.push(
makeArrayLiteral(targetInitializer.elements, checker)
);
}
if (ts10.isArrowFunction(targetInitializer)) {
const body = targetInitializer.body;
if (ts10.isArrayLiteralExpression(body)) {
arrayLiteral.push(makeArrayLiteral(body.elements, checker));
}
if (ts10.isBlock(body)) {
const statements = body.statements;
statements.forEach((statement) => {
visit(statement);
});
}
if (ts10.isParenthesizedExpression(body)) {
visit(body.expression);
}
}
}
}
if (objectInitializer && ts10.isArrowFunction(objectInitializer)) {
const body = objectInitializer.body;
if (ts10.isCallExpression(body)) {
visit(body);
}
if (ts10.isBlock(body)) {
const statements = body.statements;
statements.forEach((statement) => {
visit(statement);
});
}
if (ts10.isParenthesizedExpression(body)) {
visit(body.expression);
}
}
if (objectInitializer && ts10.isCallExpression(objectInitializer)) {
visit(objectInitializer);
}
}
if (valueDeclaration && ts10.isFunctionDeclaration(valueDeclaration)) {
const body = valueDeclaration.body;
if (body && ts10.isBlock(body)) {
visit(body);
}
}
}
}
if (ts10.isObjectLiteralExpression(node)) {
const properties = node.properties;
const isQueries = properties.some(
(property) => ts10.isPropertyAssignment(property) && property.name.getText() === "queries"
);
if (isQueries) {
properties.forEach((property) => {
if (ts10.isPropertyAssignment(property) && property.name.getText() === "queries") {
const initializer = property.initializer;
visit(initializer);
}
});
return;
}
arrayLiteral.push(objectExpression(node, checker));
}
ts10.forEachChild(node, visit);
};
visit(value);
if (arrayLiteral.flat().length === 0) {
logger.warn("This function is not provided. : \n" + value.getText());
}
return arrayLiteral;
};
const objectExpression = (node, checker) => {
let arrayLiteral = [];
const properties = node.properties;
properties.forEach((property) => {
var _a;
if (ts10.isPropertyAssignment(property) && property.name.getText() === "queryKey") {
const initializer = property.initializer;
if (ts10.isArrayLiteralExpression(initializer)) {
arrayLiteral = makeArrayLiteral(initializer.elements, checker);
}
if (ts10.isCallExpression(initializer)) {
const expression = initializer.expression;
if (ts10.isIdentifier(expression)) {
const valueDeclaration = (_a = checker.getSymbolAtLocation(expression)) == null ? void 0 : _a.valueDeclaration;
if (valueDeclaration && ts10.isVariableDeclaration(valueDeclaration)) {
const initializer2 = valueDeclaration.initializer;
if (initializer2 && ts10.isArrowFunction(initializer2)) {
const body = initializer2.body;
if (ts10.isArrayLiteralExpression(body)) {
const elements = body.elements;
arrayLiteral = makeArrayLiteral(elements, checker);
}
}
}
if (valueDeclaration && ts10.isFunctionDeclaration(valueDeclaration)) {
const body = valueDeclaration.body;
if (body && ts10.isBlock(body)) {
const statements = body.statements;
statements.forEach((statement) => {
if (ts10.isReturnStatement(statement)) {
const expression2 = statement.expression;
if (expression2 && ts10.isArrayLiteralExpression(expression2)) {
arrayLiteral = makeArrayLiteral(
expression2.elements,
checker
);
}
}
});
}
}
}
}
if (ts10.isPropertyAccessExpression(initializer)) {
arrayLiteral = propertyAccessExpression(initializer, checker);
}
if (ts10.isCallExpression(initializer)) {
const expression = initializer.expression;
if (ts10.isPropertyAccessExpression(expression)) {
arrayLiteral = propertyAccessExpression(expression, checker);
}
}
}
});
return arrayLiteral;
};
const makeArrayLiteral = (elements, checker) => {
return elements.map((el) => {
var _a, _b, _c;
if (ts10.isStringLiteral(el)) {
return {
name: el.text,
type: el,
kind: QueryNodeKind.StringLiteral
};
}
if (ts10.isIdentifier(el)) {
const symbol = checker.getSymbolAtLocation(el);
const type = checker.getTypeAtLocation(el);
const valueDeclaration = symbol == null ? void 0 : symbol.valueDeclaration;
const typeKind = (_b = (_a = type.symbol) == null ? void 0 : _a.valueDeclaration) == null ? void 0 : _b.kind;
if (valueDeclaration && ts10.isParameter(valueDeclaration)) {
if (((_c = valueDeclaration.type) == null ? void 0 : _c.kind) === ts10.SyntaxKind.TypeQuery) {
return {
name: el.text,
type,
symbol,
kind: QueryNodeKind.TypeOfKeyword
};
}
if (valueDeclaration.type && ts10.isTupleTypeNode(valueDeclaration.type)) {
const tupleNode = valueDeclaration.type;
return {
name: el.text,
type: tupleNode,
symbol,
kind: QueryNodeKind.Tuple
};
}
}
if (typeKind === ts10.SyntaxKind.EnumMember || typeKind === ts10.SyntaxKind.EnumDeclaration) {
return {
name: el.text,
type,
symbol,
kind: QueryNodeKind.EnumMember
};
}
if (symbol && symbol.getDeclarations()) {
return {
name: symbol.getName(),
type: symbol,
kind: QueryNodeKind.Symbol
};
}
}
return void 0;
}).filter(Boolean);
};
const propertyAccessExpression = (node, checker) => {
var _a;
const expression = node.expression;
const propertyName = node.name.getText();
const arrayLiteral = [];
if (expression && ts10.isIdentifier(expression)) {
const valueDeclaration = (_a = checker.getSymbolAtLocation(expression)) == null ? void 0 : _a.valueDeclaration;
if (valueDeclaration && ts10.isVariableDeclaration(valueDeclaration)) {
const objectInitializer = valueDeclaration.initializer;
if (objectInitializer && ts10.isObjectLiteralExpression(objectInitializer)) {
const targetProperty = objectInitializer.properties.find(
(prop) => ts10.isPropertyAssignment(prop) && prop.name.getText() === propertyName
);
if (targetProperty && ts10.isPropertyAssignment(targetProperty)) {
const targetInitializer = targetProperty.initializer;
if (ts10.isArrayLiteralExpression(targetInitializer)) {
arrayLiteral.push(
...makeArrayLiteral(targetInitializer.elements, checker)
);
}
if (ts10.isArrowFunction(targetInitializer)) {
const body = targetInitializer.body;
if (ts10.isArrayLiteralExpression(body)) {
arrayLiteral.push(...makeArrayLiteral(body.elements, checker));
}
}
if (ts10.isCallExpression(targetInitializer)) {
arrayLiteral.push(...visitor(targetInitializer, checker).flat());
}
}
}
}
}
return arrayLiteral;
};
const findPropertyByQueryKey = (properties) => {
for (const prop of properties) {
if (ts10.isPropertyAssignment(prop)) {
const initializer = prop.initializer;
if (ts10.isArrowFunction(initializer)) {
if (ts10.isBlock(initializer.body)) {
const statements = initializer.body.statements;
for (const statement of statements) {
if (ts10.isReturnStatement(statement)) {
const expression = statement.expression;
if (expression && ts10.isObjectLiteralExpression(expression)) {
return findPropertyByQueryKey(expression.properties);
}
}
}
}
if (ts10.isParenthesizedExpression(initializer.body)) {
const expression = initializer.body.expression;
if (expression && ts10.isObjectLiteralExpression(expression)) {
return findPropertyByQueryKey(expression.properties);
}
}
}
if (ts10.isObjectLiteralExpression(initializer)) {
return findPropertyByQueryKey(initializer.properties);
}
if (ts10.isIdentifier(prop.name) && prop.name.getText() === PROPERTY_NAME) {
return prop;
}
}
}
return void 0;
};
})(QueryKeyFinder || (QueryKeyFinder = {}));
// src/queryFinder/UseQueryFinder.ts
import ts11 from "typescript";
var UseQueryFinder;
((UseQueryFinder2) => {
const matchQuery = [
"useQuery",
"useSuspenseQuery",
"useInfiniteQuery",
"useQueries",
"useSuspenseQueries"
];
UseQueryFinder2.findUseQuery = ({ sourceFiles }) => {
const result = sourceFiles.map((sourceFile) => {
return findCallExpressions(sourceFile);
}).flat();
retu