Skip to content

Commit

Permalink
feat(dts-gen): Characters check for typename compliant with ECMA262. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchida authored Nov 4, 2024
1 parent 1d2c0bf commit fefe7c9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
7 changes: 0 additions & 7 deletions packages/dts-gen/docs/field-type-definition-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,3 @@ Additional to fields of `com.cybozu.kintone.AwesomeFields`,
this type includes `$id`, `$revision`, create time, creator, update time and update time fields.

This fields will be included when you refer to a saved record.

## Notes

`namespace` and `type-name` convention:

- Starts with a letter (`a-z` or `A-Z`), underscore (`_`), or dollar sign (`$`).
- Can be followed by any alphanumeric, underscores, or dollar signs.
33 changes: 30 additions & 3 deletions packages/dts-gen/src/validators/__tests__/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { validateArgs } from "../args";

const identifierConventionMsg = `The namespace and type-name convention:
- Starts with a letter (\`a-z\` or \`A-Z\`), underscore (\`_\`), or dollar sign (\`$\`).
- Can be followed by any alphanumeric, underscores, or dollar signs.`;
const identifierConventionMsg = `In the ECMA262 specification, this is an invalid string as IdentifierName.`;
const invalidNamespaceMessage = `Invalid namespace option!\n${identifierConventionMsg}`;
const invalidTypeNameMessage = `Invalid type-name option!\n${identifierConventionMsg}`;
const baseInput = {
Expand Down Expand Up @@ -82,6 +80,35 @@ const patterns = [
},
},
},
{
description: "should not error when type-name contains japanese characters",
input: {
...baseInput,
typeName: "案件管理",
},
expected: {
failure: undefined,
},
},
{
description: "should not error when type-name is only symbols",
input: {
...baseInput,
typeName: "$$$",
},
expected: {
failure: undefined,
},
},
{
description: "should error when type-name starts with a space",
input: { ...baseInput, typeName: " test" },
expected: {
failure: {
errorMessage: invalidTypeNameMessage,
},
},
},
];

describe("validateInput", () => {
Expand Down
8 changes: 3 additions & 5 deletions packages/dts-gen/src/validators/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export const validateArgs = (args: ParsedArgs) => {
throw new Error("--base-url (KINTONE_BASE_URL) must be specified");
}

const identifierConventionMsg = `The namespace and type-name convention:
- Starts with a letter (\`a-z\` or \`A-Z\`), underscore (\`_\`), or dollar sign (\`$\`).
- Can be followed by any alphanumeric, underscores, or dollar signs.`;
const identifierConventionMsg = `In the ECMA262 specification, this is an invalid string as IdentifierName.`;

if (args.namespace && !isValidIdentifier(args.namespace)) {
throw new Error(`Invalid namespace option!\n${identifierConventionMsg}`);
Expand All @@ -19,11 +17,11 @@ export const validateArgs = (args: ParsedArgs) => {
};

/**
* https://developer.mozilla.org/en-US/docs/Glossary/Identifier
* https://262.ecma-international.org/14.0/index.html#prod-IdentifierName
* @param targetIdentifier
*/
const isValidIdentifier = (targetIdentifier: string): boolean => {
const identifiers = targetIdentifier.split(".");
const identifierRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
const identifierRegex = /^[\p{ID_Start}_$][\p{ID_Continue}$\u200C\u200D]*$/u;
return identifiers.every((identifier) => identifierRegex.test(identifier));
};

0 comments on commit fefe7c9

Please sign in to comment.