-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }"], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** Used for comment text. */ | ||
const escapeComment = (name: string): string => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.