Skip to content

Commit

Permalink
code-gen: error when JS keywords are used as group names (#638)
Browse files Browse the repository at this point in the history
I made the same mistake twice already. The backend generators don't mind it at all, but in the react-query generator, we also dump a `useApi()` and something like the following is not allowed:

```ts
const { static } = useApi();
```

Closes #636
  • Loading branch information
dirkdev98 authored Feb 1, 2021
1 parent ca21d6d commit 70121ff
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 18 deletions.
6 changes: 5 additions & 1 deletion gen/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ export function applyCodeGenStructure(app) {
key: "sqlThrowingValidators",
},
{
key: "duplicateShortName",
key: "sqlDuplicateShortName",
shortName: T.string(),
firstName: T.string(),
secondName: T.string(),
},
{
key: "coreReservedGroupName",
groupName: T.string(),
},
),
],
}),
Expand Down
88 changes: 74 additions & 14 deletions packages/code-gen/src/generated/anonymous-validators.js

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

2 changes: 1 addition & 1 deletion packages/code-gen/src/generated/types.js

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

4 changes: 3 additions & 1 deletion packages/code-gen/src/generator/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export function exitOnErrorsOrReturn(context) {
str += `Relation from '${error.referencedByType}' is missing the inverse 'T.oneToMany()' on '${error.typeName}'.
Add 'T.oneToMany("${error.relationOwnKey}", T.reference("${error.referencedByGroup}", "${error.referencedByType}"))' to the 'relations()' call on '${error.typeName}'.
`;
} else if (error.key === "duplicateShortName") {
} else if (error.key === "sqlDuplicateShortName") {
str += `Short name '${error.shortName}' is used by both '${error.firstName}' and '${error.secondName}'.
These short name values should be unique. Please call '.shortName()' on one or both of these types to set a custom value.`;
} else if (error.key === "coreReservedGroupName") {
str += `Group '${error.groupName}' is a JavaScript or TypeScript reserved keyword. Please use another group name.`;
} else {
str += `[${error.key}]: ${JSON.stringify(error, null, 2)}`;
}
Expand Down
71 changes: 71 additions & 0 deletions packages/code-gen/src/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export async function generate(logger, options, structure) {
setupMemoizedTypes(context);
exitOnErrorsOrReturn(context);

checkReservedGroupNames(context);
exitOnErrorsOrReturn(context);

// Do initial sql checks and load in the types
// This way the validators are generated
if (context.options.enabledGenerators.indexOf("sql") !== -1) {
Expand Down Expand Up @@ -230,3 +233,71 @@ export function shouldGenerateModules(logger) {
return true;
}
}

/**
* Check if a group uses one of the reserved keywords
* Sourced from
* https://github.com/microsoft/TypeScript/blob/66ecfcbd04b8234855a673adb85e5cff3f8458d4/src/compiler/types.ts#L112
*
* @param {CodeGenContext} context
*/
function checkReservedGroupNames(context) {
const keywords = [
// Reserved words
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"final",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
// And strict mode included
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
];

for (const group of Object.keys(context.structure)) {
if (keywords.indexOf(group.toLowerCase()) !== -1) {
context.errors.push({
key: "coreReservedGroupName",
groupName: group,
});
}
}
}
2 changes: 1 addition & 1 deletion packages/code-gen/src/generator/sql/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function addShortNamesToQueryEnabledObjects(context) {

if (knownShortNames[type.shortName]) {
context.errors.push({
key: "duplicateShortName",
key: "sqlDuplicateShortName",
shortName: type.shortName,
firstName: type.name,
secondName: knownShortNames[type.shortName],
Expand Down

0 comments on commit 70121ff

Please sign in to comment.