From c98a76e0e28255e8129cbd4f13761b4698ca9bee Mon Sep 17 00:00:00 2001 From: kzvi <38590027+kzvi@users.noreply.github.com> Date: Sun, 15 Dec 2024 10:59:01 -0800 Subject: [PATCH] Make computePackageNameErrors more accurate (#100) --- scripts/src/lib/template/java.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/src/lib/template/java.ts b/scripts/src/lib/template/java.ts index 446b407f..f2ea7bef 100644 --- a/scripts/src/lib/template/java.ts +++ b/scripts/src/lib/template/java.ts @@ -60,7 +60,14 @@ export function getJavaVersion(minecraftVersion: string): JavaVersion { return JAVA_21; } -const JAVA_PACKAGE_REGEX = /^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]$/; +const JAVA_PACKAGE_REGEX = /^[a-z_][a-z0-9_]*(\.[a-z_][a-z0-9_]*)*$/; +const JAVA_RESERVED_WORDS = ` + abstract continue for new switch assert default goto package synchronized + boolean do if private this break double implements protected throw byte else + import public throws case enum instanceof return transient catch extends int + short try char final interface static void class finally long strictfp + volatile const float native super while _ true false null +`.trim().split(/\s+/); const RESERVED_PACKAGE_PREFIXES = ["net.minecraft.", "com.mojang.", "net.fabricmc.", "java."]; export function computePackageNameErrors(packageName: string): string[] { @@ -70,6 +77,11 @@ export function computePackageNameErrors(packageName: string): string[] { errorList.push("Package name is not a valid Java package name!"); } + const reservedWordsFound = packageName.split('.').filter(c => JAVA_RESERVED_WORDS.includes(c)); + if (reservedWordsFound.length != 0) { + errorList.push(`Package name contains illegal component: '${reservedWordsFound[0]}'`); + } + for (let prefix of RESERVED_PACKAGE_PREFIXES) { if (packageName.toLowerCase().startsWith(prefix)) { errorList.push(`Package name starts with '${prefix}', which is reserved!`);