diff --git a/index.js b/index.js index 757d53a..2c2a773 100644 --- a/index.js +++ b/index.js @@ -40,14 +40,20 @@ exports.compile = function compile(source, options) { }; var validOptions = { - "compilerOptions": "object", - "filePath": "string", - "moduleName": "string", - "typings": "array" + "compilerOptions": "Object", + "filePath": "String", + "moduleName": "String", + "typings": "Array" }; var validOptionsMsg = "Valid options are" + "compilerOptions, filePath, moduleName, and typings"; +function checkType(option, optionName) { + if (! option) return true; + + return option.constructor.name === validOptions[optionName]; +} + function validateAndConvertOptions(options) { if (! options) return; @@ -59,7 +65,7 @@ function validateAndConvertOptions(options) { validOptionsMsg); } - if (typeof options[option] !== validOptions[option]) { + if (! checkType(options[option], option)) { throw new Error(option + " should be of type " + validOptions[option]); } diff --git a/package.json b/package.json index a70ccc2..0343f5c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "meteor-typescript", "author": "@barbatus", - "version": "0.5.2", + "version": "0.5.5", "license": "MIT", "description": "TypeScript wrapper package for use with Meteor", "keywords": [ diff --git a/tests/ts.spec.js b/tests/ts.spec.js index 675f51d..2c183ac 100644 --- a/tests/ts.spec.js +++ b/tests/ts.spec.js @@ -1,53 +1,85 @@ var meteorTS = require("../index"); -describe("meteor-typescript", function() { +describe("meteor-typescript -> ", function() { - var testCodeLine = "export const foo = 'foo'"; + describe("testing exports and options -> ", function() { + var testCodeLine = "export const foo = 'foo'"; - it("should compile with defaults", function() { - var result = meteorTS.compile(testCodeLine); - expect(result.code.indexOf("exports.foo")).toEqual(0); - }); + it("should compile with defaults", function() { + var result = meteorTS.compile(testCodeLine); + expect(result.code.indexOf("exports.foo")).toEqual(0); + }); - it("should throw on wrong option", function() { - var test = function() { - meteorTS.compile(testCodeLine, { - "wrong": true - }); - }; - expect(test).toThrow(); - }); + it("should throw on wrong option", function() { + var test = function() { + meteorTS.compile(testCodeLine, { + "wrong": true + }); + }; + expect(test).toThrow(); + }); - it("should recognize preset options", function() { - var result = meteorTS.compile(testCodeLine, { - compilerOptions: { - module: "system" - } + it("should allow null options", function() { + var result = meteorTS.compile(testCodeLine, { + compilerOptions: null, + typings: undefined + }); + expect(result.code).not.toBeNull(); }); - expect(result.code.indexOf("System.register")).toEqual(0); - }); - it("should add module with moduleName name when moduleName is set", - function() { + it("should recognize preset options", function() { var result = meteorTS.compile(testCodeLine, { compilerOptions: { module: "system" - }, - moduleName: "fooModule" + } }); - expect(result.code.indexOf("System.register(\"fooModule\"")) - .toEqual(0); + expect(result.code).toContain("System.register"); }); - it("should throw on wrong compiler option", function() { - var test = function() { - meteorTS.compile(testCodeLine, { + it("should add module with moduleName name when moduleName is set", + function() { + var result = meteorTS.compile(testCodeLine, { compilerOptions: { - module: "wrong" - } + module: "system" + }, + moduleName: "fooModule" + }); + expect(result.code.indexOf("System.register(\"fooModule\"")) + .toEqual(0); }); - }; - expect(test).toThrow(); + + it("should throw on wrong compiler option", function() { + var test = function() { + meteorTS.compile(testCodeLine, { + compilerOptions: { + module: "wrong" + } + }); + }; + expect(test).toThrow(); + }); + }); + + describe("testing diagnostics and typings -> ", function() { + var codeLineWithImport = "import {api} from 'lib'; export const foo = 'foo'"; + + it("should contain a semantic error when some module undefined", function() { + var result = meteorTS.compile(codeLineWithImport); + + expect(result.diagnostics.semanticErrors).not.toBeNull(); + expect(result.diagnostics.semanticErrors.length).toEqual(1); + var error = result.diagnostics.semanticErrors[0].message; + expect(error).toContain("Cannot find module 'lib'"); + }); + + it("declaration file with module declaration should remove an error", function() { + var result = meteorTS.compile(codeLineWithImport, { + typings: ["typings/lib.d.ts"] + }); + + expect(result.diagnostics.semanticErrors).not.toBeNull(); + expect(result.diagnostics.semanticErrors.length).toEqual(0); + }); }); }); diff --git a/tests/typings/lib.d.ts b/tests/typings/lib.d.ts new file mode 100644 index 0000000..1c44253 --- /dev/null +++ b/tests/typings/lib.d.ts @@ -0,0 +1,3 @@ +declare module "lib" { + export let api = {}; +} diff --git a/typescript.js b/typescript.js index 1e656dc..69dc7f7 100644 --- a/typescript.js +++ b/typescript.js @@ -32,7 +32,7 @@ exports.compile = function compile(fileContent, options) { var compilerHost = _.extend({}, defaultHost, customHost); var fileNames = [filePath]; if (options.typings) { - fileNames.concat(options.typings); + fileNames = fileNames.concat(options.typings); } var program = ts.createProgram(fileNames, compilerOptions, compilerHost);