roblox-ts
Version:
<div align="center"><img width=25% src="https://i.imgur.com/yCjHmng.png"></div> <h1 align="center"><a href="https://roblox-ts.github.io/">roblox-ts</a></h1> <div align="center">A TypeScript-to-Lua Compiler for Roblox</div> <br> <div align="center"> <a hr
436 lines • 19.5 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const mocha_1 = require("mocha");
const path_1 = __importDefault(require("path"));
const util_1 = __importDefault(require("util"));
const CompilerError_1 = require("./errors/CompilerError");
const DiagnosticError_1 = require("./errors/DiagnosticError");
const ProjectError_1 = require("./errors/ProjectError");
const Project_1 = require("./Project");
const utility_1 = require("./utility");
/* tslint:disable:object-literal-sort-keys */
const errorMatrix = {
"diagnostic.spec.ts": {
message: "should not allow diagnostic errors",
instance: DiagnosticError_1.DiagnosticError,
},
"var.spec.ts": {
message: "should not allow var keyword",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoVarKeyword,
},
"reservedLuaKeywords.spec.ts": {
message: "should not allow usage of reserved lua keywords",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.ReservedKeyword,
},
"breakLabel.spec.ts": {
message: "should not allow usage of break labels",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoLabeledStatement,
},
"continueLabel.spec.ts": {
message: "should not allow usage of continue labels",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoLabeledStatement,
},
"constructorReturn.spec.ts": {
message: "should not allow return in class constructor",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoConstructorReturn,
},
"null.spec.ts": {
message: "should not allow usage of null",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoNull,
},
"reservedMetamethod.spec.ts": {
message: "should not allow usage of reserved metamethod names",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.ReservedMethodName,
},
"spreadDestructure.spec.ts": {
message: "should not allow usage of spread in destructure statements",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.SpreadDestructuring,
},
"roactSubClass.spec.tsx": {
message: "should not allow subclasses of roact components",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.RoactSubClassesNotSupported,
},
"roactJsxText.spec.tsx": {
message: "should not allow text between jsx elements",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.RoactJsxTextNotSupported,
},
"roactNew.spec.tsx": {
message: "should not allow roact components to be created with new keyword",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.RoactNoNewComponentAllowed,
},
"invalidId.spec.ts": {
message: "should not allow invalid identifiers to be used",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidIdentifier,
},
"luaTupleInConditional.spec.ts": {
message: "should not allow LuaTuples in conditionals",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.LuaTupleInConditional,
},
"luaTupleInIf.spec.ts": {
message: "should not allow LuaTuples in if statements",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.LuaTupleInConditional,
},
"luaTupleInElseIf.spec.ts": {
message: "should not allow LuaTuples in else if statements",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.LuaTupleInConditional,
},
"reservedId.spec.ts": {
message: "should not allow reserved identifiers to be used",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.RobloxTSReservedIdentifier,
},
// TODO: Readd these tests
// "invalidAccess.spec.server.ts": {
// message: "should not allow client only API to be accessed by server code",
// instance: CompilerError,
// type: CompilerErrorType.InvalidClientOnlyAPIAccess,
// },
// "invalidAccess.spec.client.ts": {
// message: "should not allow server only API to be accessed by client code",
// instance: CompilerError,
// type: CompilerErrorType.InvalidServerOnlyAPIAccess,
// },
"equalsEquals.ts": {
message: "should not allow ==",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoEqualsEquals,
},
"exclamationEquals.ts": {
message: "should not allow !=",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoExclamationEquals,
},
"dynamicImport.spec.ts": {
message: "should not allow dynamic imports",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoDynamicImport,
},
"macroIndex.spec.ts": {
message: "should not allowing indexing macro methods without call",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidMacroIndex,
},
"classPrototype.spec.ts": {
message: "should not allowing indexing class prototype",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoClassPrototype,
},
"indexFunction.spec.ts": {
message: "should not allowing indexing functions",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoFunctionIndex,
},
"invalidMetamethod.spec.ts": {
message: "should not allowing reserved metamethods",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.UndefinableMetamethod,
},
"unexpectedInitializerForOf.spec.ts": {
message: "should not allow expressions as initializers in for-of loops",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.UnexpectedInitializer,
},
"disallowForIn.spec.ts": {
message: "should not allow for-in loops",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.ForInLoop,
},
"exportNonModule.spec.server.ts": {
message: "should not allow exporting from a non-ModuleScript",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.ExportInNonModuleScript,
},
"typeof.spec.ts": {
message: "should not allow typeof operator",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoTypeOf,
},
"any/index.spec.ts": {
message: "should not allow indexing type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/call.spec.ts": {
message: "should not allow calling type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/pass.spec.ts": {
message: "should not allow passing type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/computedAccess.spec.ts": {
message: "should not allow computed accessing type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/computedAccess2.spec.ts": {
message: "should not allow computed accessing type any #2",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"any/func.spec.ts": {
message: "should not allow functions that return type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/add.spec.ts": {
message: "should not allow adding type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/sub.spec.ts": {
message: "should not allow subtracting type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/mul.spec.ts": {
message: "should not allow multiplying type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"any/div.spec.ts": {
message: "should not allow dividing type any",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.NoAny,
},
"methodDestructure/arr.spec.1.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"methodDestructure/arr.spec.2.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"methodDestructure/arr.spec.3.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidMacroIndex,
},
"methodDestructure/arr.spec.4.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"methodDestructure/arr.spec.5.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"methodDestructure/arr.spec.6.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.BadDestructuringType,
},
"methodDestructure/arr.spec.7.ts": {
message: "should not allow method indexing from arrays",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.BadDestructuringType,
},
"methodDestructure/map.spec.1.ts": {
message: "should not allow indexing from Maps",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"methodDestructure/map.spec.2.ts": {
message: "should not allow indexing from Maps",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"methodDestructure/union.spec.ts": {
message: "should not allow Array and Object unions to be indexed from",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.InvalidComputedIndex,
},
"roactInitMethod.spec.ts": {
message: "should not allow init in Roact class",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.RoactNoReservedMethods,
},
"tupleLength1.spec.ts": {
message: "should not allow indexing the length property of tuples",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.TupleLength,
},
"tupleLength2.spec.ts": {
message: "should not allow indexing the length property of tuples",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.TupleLength,
},
"tupleLength3.spec.ts": {
message: "should not allow indexing the length property of tuples",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.TupleLength,
},
"extendedMapClass.spec.ts": {
message: "should not allow creating classes which extend Map",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.BadClassExtends,
},
"extendedSetClass.spec.ts": {
message: "should not allow creating classes which extend Set",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.BadClassExtends,
},
"getters.spec.ts": {
message: "should not allow getters",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.GettersSettersDisallowed,
},
"setters.spec.ts": {
message: "should not allow setters",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.GettersSettersDisallowed,
},
"staticGetters.spec.ts": {
message: "should not allow getters",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.GettersSettersDisallowed,
},
"staticSetters.spec.ts": {
message: "should not allow setters",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.GettersSettersDisallowed,
},
"funcExpMethodCall.spec.ts": {
message: "should not allow calling from function method expressions",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.BadFunctionExpressionMethodCall,
},
"superCallback.spec.ts": {
message: "should not allow super callback calls!",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.BadSuperCall,
},
"extendArrayIterator.spec.ts": {
message: "should not allow Symbol.iterator on classes which extend from Array",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.DefaultIteratorOnArrayExtension,
},
"extendArrayIterator.spec.1.ts": {
message: "should not allow Symbol.iterator on classes which extend from Array",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.DefaultIteratorOnArrayExtension,
},
"extendArrayIterator.spec.2.ts": {
message: "should not allow Symbol.iterator on classes which extend from Array",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.DefaultIteratorOnArrayExtension,
},
"extendArrayIterator.spec.3.ts": {
message: "should not allow Symbol.iterator on classes which extend from Array",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.DefaultIteratorOnArrayExtension,
},
"computedMethodInClass.spec.ts": {
message: "should not allow computed method names in classes",
instance: CompilerError_1.CompilerError,
type: CompilerError_1.CompilerErrorType.ClassWithComputedMethodNames,
},
};
/* tslint:enable:object-literal-sort-keys */
const compilerArgs = {
ci: true,
includePath: "../lib",
modulesPath: "modules",
noInclude: true,
project: "tests",
rojo: "",
};
const srcFolder = path_1.default.resolve("tests", "src");
const project = new Project_1.Project(compilerArgs);
function compile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
return project.compileFileByPath(filePath);
});
}
function testFolder(folderPath) {
for (const name of fs_extra_1.default.readdirSync(folderPath)) {
if (name !== "errors") {
mocha_1.it(name, () => __awaiter(this, void 0, void 0, function* () {
process.exitCode = 0;
try {
yield compile(path_1.default.join(folderPath, name));
}
catch (e) {
if (e instanceof CompilerError_1.CompilerError) {
throw new Error(util_1.default.format("%s:%d:%d - %s %s", path_1.default.relative(srcFolder, e.node.getSourceFile().getFilePath()), e.node.getStartLineNumber(), e.node.getNonWhitespaceStart() - e.node.getStartLinePos(), utility_1.red("Compiler Error:"), e.message));
}
else if (e instanceof ProjectError_1.ProjectError) {
throw new Error(util_1.default.format("%s %s", utility_1.red("Project Error:"), e.message));
}
else if (e instanceof DiagnosticError_1.DiagnosticError) {
throw new Error(`DiagnosticError:\n${e.errors.join("\n")}`);
}
else {
throw new Error(`Unexpected Error: ${String(e)}`);
}
}
if (process.exitCode !== 0) {
throw new Error("non-zero exit code");
}
}));
}
}
}
mocha_1.describe("compile integration tests", () => testFolder(srcFolder));
mocha_1.describe("compile integration tests for imports", () => testFolder(path_1.default.join(srcFolder, "imports")));
mocha_1.describe("compile error unit tests", () => {
for (const file in errorMatrix) {
mocha_1.it(errorMatrix[file].message, done => {
compile(path_1.default.join(srcFolder, "errors", file))
.then(() => done("Did not throw!"))
.catch(e => {
if (e instanceof errorMatrix[file].instance && errorMatrix[file].type === undefined) {
done();
}
else if (e instanceof errorMatrix[file].instance && errorMatrix[file].type === e.type) {
done();
}
else if (e instanceof CompilerError_1.CompilerError) {
done(`Unexpected CompilerError: ${CompilerError_1.CompilerErrorType[e.type]}`);
}
else if (e instanceof ProjectError_1.ProjectError) {
done(`Unexpected ProjectError: ${ProjectError_1.ProjectErrorType[e.type]}`);
}
else if (e instanceof DiagnosticError_1.DiagnosticError) {
done(`Unexpected DiagnosticError:\n${e.errors.join("\n")}`);
}
else {
done(`Unexpected error: ${String(e)}`);
}
});
});
}
});
//# sourceMappingURL=test.js.map