From d7ed61c521882d7ab2acac7fe3256d5db950630d Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Thu, 7 Dec 2023 21:53:56 +0000 Subject: [PATCH] Fix escaping in kanel-kysely --- package-lock.json | 4 ++-- packages/kanel-kysely/src/MakeKyselyConfig.ts | 14 +++++++++----- packages/kanel-kysely/src/processFile.ts | 2 +- packages/kanel/src/ImportGenerator.ts | 5 ++++- packages/kanel/src/escapeFieldName.ts | 4 +++- packages/kanel/src/generators/resolveType.ts | 3 +-- packages/kanel/src/index.ts | 5 +++++ 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3169f5b6..0dcf59c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9069,7 +9069,7 @@ } }, "packages/kanel": { - "version": "3.6.0", + "version": "3.7.0", "license": "MIT", "dependencies": { "@kristiandupont/recase": "^1.2.0", @@ -9117,7 +9117,7 @@ } }, "packages/kanel-zod": { - "version": "1.2.3", + "version": "1.3.0", "license": "MIT", "dependencies": { "@kristiandupont/recase": "^1.2.1" diff --git a/packages/kanel-kysely/src/MakeKyselyConfig.ts b/packages/kanel-kysely/src/MakeKyselyConfig.ts index 1fed1e3d..fdc8f5b2 100644 --- a/packages/kanel-kysely/src/MakeKyselyConfig.ts +++ b/packages/kanel-kysely/src/MakeKyselyConfig.ts @@ -1,4 +1,4 @@ -import { CompositeDetails, InstantiatedConfig } from "kanel"; +import { CompositeDetails, escapeIdentifier, InstantiatedConfig } from "kanel"; interface MakeKyselyConfig { databaseFilename: string; @@ -19,10 +19,14 @@ interface MakeKyselyConfig { export const defaultConfig: MakeKyselyConfig = { databaseFilename: "Database", getKyselyItemMetadata: (d, selectorName, canInitialize, canMutate) => ({ - tableInterfaceName: `${selectorName}Table`, - selectableName: selectorName, - insertableName: canInitialize ? `New${selectorName}` : undefined, - updatableName: canMutate ? `${selectorName}Update` : undefined, + tableInterfaceName: `${escapeIdentifier(selectorName)}Table`, + selectableName: escapeIdentifier(selectorName), + insertableName: canInitialize + ? `New${escapeIdentifier(selectorName)}` + : undefined, + updatableName: canMutate + ? `${escapeIdentifier(selectorName)}Update` + : undefined, }), }; diff --git a/packages/kanel-kysely/src/processFile.ts b/packages/kanel-kysely/src/processFile.ts index d305636c..8a3338af 100644 --- a/packages/kanel-kysely/src/processFile.ts +++ b/packages/kanel-kysely/src/processFile.ts @@ -194,7 +194,7 @@ const processFile = ( } const tableImport: TypeImport = { - name: `${selectorName}Table`, + name: tableInterfaceName, isDefault: true, path, isAbsolute: false, diff --git a/packages/kanel/src/ImportGenerator.ts b/packages/kanel/src/ImportGenerator.ts index 9cd34335..58b83564 100644 --- a/packages/kanel/src/ImportGenerator.ts +++ b/packages/kanel/src/ImportGenerator.ts @@ -1,5 +1,6 @@ import path from "path"; +import escapeString from "./escapeString"; import TypeImport from "./TypeImport"; type ImportSet = { @@ -114,7 +115,9 @@ class ImportGenerator { importParts.push(bracketedImportString); } - const line = `import ${importParts.join(", ")} from '${relativePath}';`; + const line = `import ${importParts.join(", ")} from '${escapeString( + relativePath, + )}';`; return [line]; }); } diff --git a/packages/kanel/src/escapeFieldName.ts b/packages/kanel/src/escapeFieldName.ts index 8ef74a70..7f89345b 100644 --- a/packages/kanel/src/escapeFieldName.ts +++ b/packages/kanel/src/escapeFieldName.ts @@ -1,3 +1,5 @@ +import escapeString from "./escapeString"; + /** Used for object fields. If the name is illegal Typescript, put it in quotes. */ const escapeFieldName = (name: string): string => { let isLegalIdentifier = true; @@ -12,7 +14,7 @@ const escapeFieldName = (name: string): string => { isLegalIdentifier = false; } - return isLegalIdentifier ? name : `'${name}'`; + return isLegalIdentifier ? name : `'${escapeString(name)}'`; }; export default escapeFieldName; diff --git a/packages/kanel/src/generators/resolveType.ts b/packages/kanel/src/generators/resolveType.ts index 7d83a656..eb52d7ad 100644 --- a/packages/kanel/src/generators/resolveType.ts +++ b/packages/kanel/src/generators/resolveType.ts @@ -10,7 +10,6 @@ import { tryParse } from "tagged-comment-parser"; import { InstantiatedConfig } from "../config-types"; import Details from "../Details"; -import escapeIdentifier from "../escapeIdentifier"; import TypeDefinition from "../TypeDefinition"; import { CompositeDetails, CompositeProperty } from "./composite-types"; @@ -166,7 +165,7 @@ const resolveType = ( ); return { - name: escapeIdentifier(name), + name, typeImports: [ { name, diff --git a/packages/kanel/src/index.ts b/packages/kanel/src/index.ts index c6d12456..e49d11c9 100644 --- a/packages/kanel/src/index.ts +++ b/packages/kanel/src/index.ts @@ -2,7 +2,12 @@ export * from "./config-types"; export * from "./declaration-types"; export * from "./default-metadata-generators"; export { default as Details } from "./Details"; +export { default as escapeComment } from "./escapeComment"; export { default as escapeFieldName } from "./escapeFieldName"; +// For backwards compatibility +export { default as escapeName } from "./escapeFieldName"; +export { default as escapeIdentifier } from "./escapeIdentifier"; +export { default as escapeString } from "./escapeString"; export { CompositeDetails, CompositeProperty,