Skip to content

feat(dts-gen): Characters check for typename compliant with ECMA262. #3042

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

Merged
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
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unicode character class escape may not be converted by a transpiler such as Babel.

return identifiers.every((identifier) => identifierRegex.test(identifier));
};
Loading