Skip to content

Commit

Permalink
Add .luau support to RojoResolver (#496)
Browse files Browse the repository at this point in the history
Converts input paths from `.lua` -> `.luau` internally to better handle
results of `PathTranslator.getImportPath()`.
  • Loading branch information
osyrisrblx authored Sep 10, 2024
1 parent 8157f83 commit a863d6f
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/RojoResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import path from "path";
const PACKAGE_ROOT = path.join(__dirname, "..");

const LUA_EXT = ".lua";
const LUAU_EXT = ".luau";
const JSON_EXT = ".json";
const TOML_EXT = ".toml";

const ROJO_MODULE_EXTS = new Set([LUAU_EXT, JSON_EXT, TOML_EXT]);
const ROJO_SCRIPT_EXTS = new Set([LUAU_EXT]);

const INIT_NAME = "init";

Expand Down Expand Up @@ -94,14 +99,14 @@ export enum NetworkType {

function stripRojoExts(filePath: string) {
const ext = path.extname(filePath);
if (ext === LUA_EXT) {
if (ROJO_MODULE_EXTS.has(ext)) {
filePath = filePath.slice(0, -ext.length);
const subext = path.extname(filePath);
if (subext === SERVER_SUBEXT || subext === CLIENT_SUBEXT) {
filePath = filePath.slice(0, -subext.length);
if (ROJO_SCRIPT_EXTS.has(ext)) {
const subext = path.extname(filePath);
if (subext === SERVER_SUBEXT || subext === CLIENT_SUBEXT) {
filePath = filePath.slice(0, -subext.length);
}
}
} else if (ext === JSON_EXT) {
filePath = filePath.slice(0, -ext.length);
}
return filePath;
}
Expand Down Expand Up @@ -146,6 +151,12 @@ function isValidRojoConfig(value: unknown): value is RojoFile {
return validateRojo.get()(value) === true;
}

function convertToLuau(filePath: string) {
const ext = path.extname(filePath);
if (ext === LUA_EXT) return filePath.slice(0, -ext.length) + LUAU_EXT;
return filePath;
}

export const RbxPathParent = Symbol("Parent");
export type RbxPathParent = typeof RbxPathParent;

Expand Down Expand Up @@ -248,8 +259,10 @@ export class RojoResolver {
}

private parsePath(itemPath: string) {
itemPath = convertToLuau(itemPath);
const realPath = fs.pathExistsSync(itemPath) ? fs.realpathSync(itemPath) : itemPath;
if (path.extname(itemPath) === LUA_EXT) {
const ext = path.extname(itemPath);
if (ROJO_MODULE_EXTS.has(ext)) {
this.filePathToRbxPathMap.set(itemPath, [...this.rbxPath]);
} else {
const isDirectory = fs.pathExistsSync(realPath) && fs.statSync(realPath).isDirectory();
Expand Down Expand Up @@ -302,16 +315,20 @@ export class RojoResolver {

public getRbxPathFromFilePath(filePath: string): RbxPath | undefined {
filePath = path.resolve(filePath);
filePath = convertToLuau(filePath);

const rbxPath = this.filePathToRbxPathMap.get(filePath);
if (rbxPath) {
return rbxPath;
}

const ext = path.extname(filePath);
for (const partition of this.partitions) {
if (isPathDescendantOf(filePath, partition.fsPath)) {
const stripped = stripRojoExts(filePath);
const relativePath = path.relative(partition.fsPath, stripped);
const relativeParts = relativePath === "" ? [] : relativePath.split(path.sep);
if (relativeParts[relativeParts.length - 1] === INIT_NAME) {
if (ROJO_SCRIPT_EXTS.has(ext) && relativeParts.at(-1) === INIT_NAME) {
relativeParts.pop();
}
return partition.rbxPath.concat(relativeParts);
Expand All @@ -320,8 +337,15 @@ export class RojoResolver {
}

public getRbxTypeFromFilePath(filePath: string): RbxType {
const subext = path.extname(path.basename(filePath, path.extname(filePath)));
return SUB_EXT_TYPE_MAP.get(subext) ?? RbxType.Unknown;
filePath = convertToLuau(filePath);
const ext = path.extname(filePath);
const subext = path.extname(path.basename(filePath, ext));
if (ROJO_SCRIPT_EXTS.has(ext)) {
return SUB_EXT_TYPE_MAP.get(subext) ?? RbxType.Unknown;
} else {
// non-script exts cannot use .server, .client, etc.
return RbxType.ModuleScript;
}
}

private getContainer(from: Array<RbxPath>, rbxPath?: RbxPath) {
Expand Down

0 comments on commit a863d6f

Please sign in to comment.