Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escaping table names and fields #506

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions packages/kanel/src/declaration-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ export type DeclarationBase = {
export type TypeDeclaration = DeclarationBase & {
declarationType: "typeDeclaration";
name: string;
/** Must be valid TypeScript */
typeDefinition: string[];
exportAs: "named" | "default";
};

export type InterfacePropertyDeclaration = {
export type InterfacePropertyDeclaration = DeclarationBase & {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't do this is that you can't add a prop declaration directly to a file. But it's a semantic difference, this is fine.

name: string;
comment?: string[];
dimensions: number;
isNullable: boolean;
isOptional: boolean;
typeName: string;
typeImports?: TypeImport[];
};

export type InterfaceDeclaration = DeclarationBase & {
Expand All @@ -40,6 +39,7 @@ export type EnumDeclaration = DeclarationBase & {
export type ConstantDeclaration = DeclarationBase & {
declarationType: "constant";
name: string;
/** Must be valid TypeScript */
type: string | undefined;
value: string | string[];
exportAs: "named" | "default";
Expand Down
40 changes: 40 additions & 0 deletions packages/kanel/src/default-metadata-generators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";

import { defaultGenerateIdentifierType } from "./default-metadata-generators";
import { GenerateIdentifierType } from "./metadata-types";

describe("defaultGenerateIdentifierType", () => {
it("generates correct identifier type", () => {
const result = defaultGenerateIdentifierType(
...([
{ name: "thing_id", type: { kind: "base", fullName: "text" } },
{ name: "my_things", schemaName: "public" },
{ typeMap: {} },
] as Parameters<GenerateIdentifierType>),
);

expect(result).toMatchObject({
declarationType: "typeDeclaration",
comment: ["Identifier type for public.my_things"],
name: "MyThingsThingId",
typeDefinition: ["unknown & { __brand: 'MyThingsThingId' }"],
});
});

it("generates correct identifier type with special characters", () => {
const result = defaultGenerateIdentifierType(
...([
{ name: "special_col!'.", type: { kind: "base", fullName: "text" } },
{ name: "special_table!'.", schemaName: "special_schema!'." },
{ typeMap: {} },
] as Parameters<GenerateIdentifierType>),
);

expect(result).toMatchObject({
declarationType: "typeDeclaration",
comment: ["Identifier type for special_schema!'..special_table!'."],
name: "SpecialTableSpecialCol",
typeDefinition: ["unknown & { __brand: 'SpecialTableSpecialCol' }"],
});
});
});
8 changes: 6 additions & 2 deletions packages/kanel/src/default-metadata-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TableColumn } from "extract-pg-schema";
import { join } from "path";
import { tryParse } from "tagged-comment-parser";

import escapeIdentifier from "./escapeIdentifier";
import escapeString from "./escapeString";
import { CompositeProperty } from "./generators/composite-types";
import resolveType from "./generators/resolveType";
import {
Expand Down Expand Up @@ -67,7 +69,9 @@ export const defaultGenerateIdentifierType: GenerateIdentifierType = (
details,
config,
) => {
const name = toPascalCase(details.name) + toPascalCase(column.name);
const name = escapeIdentifier(
toPascalCase(details.name) + toPascalCase(column.name),
);
const innerType = resolveType(column, details, {
...config,
// Explicitly disable identifier resolution so we get the actual inner type here
Expand All @@ -86,7 +90,7 @@ export const defaultGenerateIdentifierType: GenerateIdentifierType = (
declarationType: "typeDeclaration",
name,
exportAs: "named",
typeDefinition: [`${type} & { __brand: '${name}' }`],
typeDefinition: [`${type} & { __brand: '${escapeString(name)}' }`],
typeImports: imports,
comment: [`Identifier type for ${details.schemaName}.${details.name}`],
};
Expand Down
12 changes: 12 additions & 0 deletions packages/kanel/src/escapeComment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, it } from "vitest";

import escapeComment from "./escapeComment";

it.each([
["an example"],
[" "],
["\"'`\t"],
["/* something */", "/* something *\\/"],
])("should escape string: %s", (s1, s2) => {
expect(escapeComment(s1)).toBe(s2 ?? s1);
});
5 changes: 5 additions & 0 deletions packages/kanel/src/escapeComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Used for comment text. */
const escapeComment = (name: string): string =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I had thought of this but not needed it thus far :-)

name.replaceAll("*/", "*\\/").replaceAll("\n", "\\n");

export default escapeComment;
20 changes: 20 additions & 0 deletions packages/kanel/src/escapeFieldName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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;

if (name.length === 0 || name.trim() !== name) {
isLegalIdentifier = false;
}

try {
new Function("var " + name);
} catch {
isLegalIdentifier = false;
}

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

export default escapeFieldName;
28 changes: 28 additions & 0 deletions packages/kanel/src/escapeIdentifier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, it } from "vitest";

import escapeIdentifier from "./escapeIdentifier";

it.each([
["ShouldNotBeChanged"],
["alsoShouldNotBeChanged"],
[" NeedsTrimming ", "NeedsTrimming"],
[" needsTrimming ", "needsTrimming"],
["an example", "AnExample"],
[" an example", "AnExample"],
["an example", "AnExample"],
[
"[example] this is a table! yes, even with symbols: \"'!.Id",
"ExampleThisIsATableYesEvenWithSymbolsId",
],
["if", "If"],
])("should escape string: %s -> %s", (s1, s2) => {
expect(escapeIdentifier(s1)).toBe(s2 ?? s1);
});

it.each([
["empty string", ""],
["whitespace", " "],
["symbols", "\"'`\t"],
])("should throw if unsalvageable: %s", (_, s) => {
expect(() => escapeIdentifier(s)).toThrow();
});
34 changes: 34 additions & 0 deletions packages/kanel/src/escapeIdentifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { recase } from "@kristiandupont/recase";

const toPascalCase = recase(null, "pascal");

/** Used for identifiers. If the name has symbols or is illegal Typescript, strip out symbols and make it PascalCase. */
// NB: A wider set of things than are accepted by this function are valid identifiers in TypeScript (see https://stackoverflow.com/a/9337047). However, this works well for our purposes.
const escapeIdentifier = (name: string): string => {
const trimmed = name.trim();
let isLegalIdentifier = true;

if (!/^[$A-Z_a-z][\w$]*$/.test(trimmed)) {
isLegalIdentifier = false;
}
try {
new Function(`const ${trimmed} = 1;`);
} catch {
isLegalIdentifier = false;
}

if (isLegalIdentifier) {
return trimmed;
}

const snaked = trimmed.replaceAll(/[^$A-Z_a-z]/g, "_").replaceAll(/_+/g, "_");
const result = toPascalCase(snaked);

if (result.length === 0) {
throw new Error(`Invalid and unsalvageable identifier: ${name}`);
}

return result;
};

export default escapeIdentifier;
18 changes: 0 additions & 18 deletions packages/kanel/src/escapeName.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/kanel/src/escapeString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, it } from "vitest";

import escapeString from "./escapeString";

it.each([
["this should be easy"],
[""],
[" "],
["adam's test"],
['"'],
["'''"],
["' '"],
["\"'`\n\t"],
["\"'`\n\t".repeat(10)],
])("should escape string: %s", (s) => {
const f = new Function(`return '${escapeString(s)}'`);
expect(f()).toBe(s);
});
5 changes: 5 additions & 0 deletions packages/kanel/src/escapeString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Used for single-quoted strings. */
const escapeString = (name: string): string =>
name.replaceAll("'", "\\'").replaceAll("\n", "\\n");

export default escapeString;
7 changes: 6 additions & 1 deletion 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 escapeName } from "./escapeName";
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
22 changes: 22 additions & 0 deletions packages/kanel/src/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,26 @@ describe("processGenerationSetup", () => {
`});`,
]);
});

it("should process a type declaration with special characters", () => {
const declarations: Declaration[] = [
{
declarationType: "typeDeclaration" as const,
name: "[example] this is a table! yes, even with symbols: \"'!.Id",
exportAs: "named",
comment: ["This is a // * /* comment */", "hello"],
typeDefinition: [
"string & { __brand: '[example] this is a table! yes, even with symbols: \"\\'!.Id' }",
],
},
];
const lines = render(declarations, "./");
expect(lines).toEqual([
"/**",
" * This is a // * /* comment *\\/",
" * hello",
" */",
`export type ExampleThisIsATableYesEvenWithSymbolsId = string & { __brand: '[example] this is a table! yes, even with symbols: "\\'!.Id' };`,
]);
});
});
Loading