Skip to content

Commit

Permalink
Fix escaping in kanel-kysely
Browse files Browse the repository at this point in the history
  • Loading branch information
domdomegg committed Dec 7, 2023
1 parent fbb8d43 commit d7ed61c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions packages/kanel-kysely/src/MakeKyselyConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompositeDetails, InstantiatedConfig } from "kanel";
import { CompositeDetails, escapeIdentifier, InstantiatedConfig } from "kanel";

interface MakeKyselyConfig {
databaseFilename: string;
Expand All @@ -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,
}),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/kanel-kysely/src/processFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const processFile = (
}

const tableImport: TypeImport = {
name: `${selectorName}Table`,
name: tableInterfaceName,
isDefault: true,
path,
isAbsolute: false,
Expand Down
5 changes: 4 additions & 1 deletion packages/kanel/src/ImportGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";

import escapeString from "./escapeString";
import TypeImport from "./TypeImport";

type ImportSet = {
Expand Down Expand Up @@ -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];
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/kanel/src/escapeFieldName.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +14,7 @@ const escapeFieldName = (name: string): string => {
isLegalIdentifier = false;
}

return isLegalIdentifier ? name : `'${name}'`;
return isLegalIdentifier ? name : `'${escapeString(name)}'`;
};

export default escapeFieldName;
3 changes: 1 addition & 2 deletions packages/kanel/src/generators/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -166,7 +165,7 @@ const resolveType = (
);

return {
name: escapeIdentifier(name),
name,
typeImports: [
{
name,
Expand Down
5 changes: 5 additions & 0 deletions packages/kanel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d7ed61c

Please sign in to comment.