Skip to content

Commit

Permalink
Add general options validation,
Browse files Browse the repository at this point in the history
add ES3 and CommonJS to the default compiler options,
add jasmine-node and multiple unit tests

This part of the effort to create an official TypeScript compiler:
Urigo/angular2-meteor#89
Urigo/angular2-meteor#90
Urigo/angular2-meteor#102
  • Loading branch information
barbatus committed Feb 14, 2016
1 parent e6be42a commit fcf4607
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 42 deletions.
53 changes: 41 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var tsCompile = require("./typescript").compile;
var Cache = require("./cache").Cache;
var _ = require("underscore");

exports.setCacheDir = function setCacheDir(cacheDir) {
function setCacheDir(cacheDir) {
if (compileCache && compileCache.cacheDir === cacheDir) {
return;
}
Expand All @@ -16,12 +16,19 @@ exports.setCacheDir = function setCacheDir(cacheDir) {
}, cacheDir);
};

exports.setCacheDir = setCacheDir;

var compileCache;
exports.compile = function compile(source, options) {
options = options ? convertOptionsOrThrow(options) :
{compilerOptions: getDefaultCompilerOptions()};
validateAndConvertOptions(options);

if (! options)
options = {compilerOptions: getDefaultCompilerOptions()};

if (! options.compilerOptions)
options.compilerOptions = getDefaultCompilerOptions();

if (! options.useCache) {
if (options.compilerOptions.useCache) {
return tsCompile(source, options);
}

Expand All @@ -32,17 +39,39 @@ exports.compile = function compile(source, options) {
return compileCache.get(source, options);
};

function convertOptionsOrThrow(options) {
if (! options.compilerOptions) return null;
var validOptions = {
"compilerOptions": "object",
"filePath": "string",
"moduleName": "string",
"typings": "array"
};
var validOptionsMsg = "Valid options are" +
"compilerOptions, filePath, moduleName, and typings";

var compilerOptions = convertCompilerOptionsOrThrow(options.compilerOptions);
var result = _.clone(options);
result.compilerOptions = compilerOptions;
function validateAndConvertOptions(options) {
if (! options) return;

return result;
}
// Validate top level options.
for (var option in options) {
if (options.hasOwnProperty(option)) {
if (validOptions[option] === undefined) {
throw new Error("Unknown option: " + option + "." +
validOptionsMsg);
}

exports.convertOptionsOrThrow = convertOptionsOrThrow;
if (typeof options[option] !== validOptions[option]) {
throw new Error(option + " should be of type " +
validOptions[option]);
}
}
}

// Validate and convert compilerOptions.
if (options.compilerOptions) {
options.compilerOptions = convertCompilerOptionsOrThrow(
options.compilerOptions);
}
}

exports.getDefaultOptions = function getDefaultOptions() {
return {
Expand Down
19 changes: 9 additions & 10 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
var ts = require("typescript");
var _ = require("underscore");

function getCompilerOptions(customOptions) {
var compilerOptions = ts.getDefaultCompilerOptions();
function presetCompilerOptions(customOptions) {
if (! customOptions) return;

_.extend(compilerOptions, customOptions);

// Support decorators by default.
compilerOptions.experimentalDecorators = true;
var compilerOptions = _.clone(customOptions);

// Declaration files are expected to
// be generated separately.
Expand Down Expand Up @@ -43,21 +40,23 @@ function getCompilerOptions(customOptions) {
return compilerOptions;
}

exports.getCompilerOptions = getCompilerOptions;
exports.presetCompilerOptions = presetCompilerOptions;

// Default compiler options.
function getDefaultCompilerOptions() {
return {
module : ts.ModuleKind.None,
target: ts.ScriptTarget.ES5,
module : ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES3,
sourceMap: true,
noResolve: false,
diagnostics: true,
// Custom option to turn on/off cache.
useCache: true,
// Always emit class metadata,
// especially useful for Angular2.
emitDecoratorMetadata: true
emitDecoratorMetadata: true,
// Support decorators by default.
experimentalDecorators: true
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"random-js": "^1.0.3"
},
"devDependencies": {
"mocha": "^2.2.5"
"jasmine-node": "^1.14.5"
}
}
6 changes: 1 addition & 5 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/usr/bin/env bash

cd $(dirname $0)
TEST_DIR=$(pwd)

TYPESCRIPT_CACHE_DIR=${TEST_DIR}/.cache
export TYPESCRIPT_CACHE_DIR

node tests.js
jasmine-node * --config "TYPESCRIPT_CACHE_DIR" ".cache"
12 changes: 0 additions & 12 deletions tests/tests.js

This file was deleted.

53 changes: 53 additions & 0 deletions tests/ts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var meteorTS = require("../index");

describe("meteor-typescript", 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 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"
}
});
expect(result.code.indexOf("System.register")).toEqual(0);
});

it("should add module with moduleName name when moduleName is set",
function() {
var result = meteorTS.compile(testCodeLine, {
compilerOptions: {
module: "system"
},
moduleName: "fooModule"
});
expect(result.code.indexOf("System.register(\"fooModule\""))
.toEqual(0);
});

it("should throw on wrong compiler option", function() {
var test = function() {
meteorTS.compile(testCodeLine, {
compilerOptions: {
module: "wrong"
}
});
};
expect(test).toThrow();
});

});
4 changes: 2 additions & 2 deletions typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

var assert = require("assert");
var ts = require("typescript");
var getCompilerOptions = require("./options").getCompilerOptions;
var presetCompilerOptions = require("./options").presetCompilerOptions;
var _ = require("underscore");
var deepHash = require("./utils").deepHash;

exports.compile = function compile(fileContent, options) {
var compilerOptions = getCompilerOptions(
var compilerOptions = presetCompilerOptions(
options.compilerOptions);

var filePath = options.filePath || deepHash(fileContent) + ".ts";
Expand Down

0 comments on commit fcf4607

Please sign in to comment.