From fefe7c937f8f41b38a43b80986eef6ba3578902f Mon Sep 17 00:00:00 2001 From: tuchida Date: Tue, 5 Nov 2024 08:28:02 +0900 Subject: [PATCH] feat(dts-gen): Characters check for typename compliant with ECMA262. (#3042) --- .../docs/field-type-definition-guide.md | 7 ---- .../src/validators/__tests__/args.test.ts | 33 +++++++++++++++++-- packages/dts-gen/src/validators/args.ts | 8 ++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/packages/dts-gen/docs/field-type-definition-guide.md b/packages/dts-gen/docs/field-type-definition-guide.md index 1025c0c165..e00098bfc2 100644 --- a/packages/dts-gen/docs/field-type-definition-guide.md +++ b/packages/dts-gen/docs/field-type-definition-guide.md @@ -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. diff --git a/packages/dts-gen/src/validators/__tests__/args.test.ts b/packages/dts-gen/src/validators/__tests__/args.test.ts index 8b6e8b852c..f663173fef 100644 --- a/packages/dts-gen/src/validators/__tests__/args.test.ts +++ b/packages/dts-gen/src/validators/__tests__/args.test.ts @@ -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 = { @@ -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", () => { diff --git a/packages/dts-gen/src/validators/args.ts b/packages/dts-gen/src/validators/args.ts index 6917def304..94c23b5843 100644 --- a/packages/dts-gen/src/validators/args.ts +++ b/packages/dts-gen/src/validators/args.ts @@ -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}`); @@ -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)); };