Skip to content

Commit fcf4607

Browse files
committed
Add general options validation,
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
1 parent e6be42a commit fcf4607

File tree

7 files changed

+107
-42
lines changed

7 files changed

+107
-42
lines changed

index.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var tsCompile = require("./typescript").compile;
66
var Cache = require("./cache").Cache;
77
var _ = require("underscore");
88

9-
exports.setCacheDir = function setCacheDir(cacheDir) {
9+
function setCacheDir(cacheDir) {
1010
if (compileCache && compileCache.cacheDir === cacheDir) {
1111
return;
1212
}
@@ -16,12 +16,19 @@ exports.setCacheDir = function setCacheDir(cacheDir) {
1616
}, cacheDir);
1717
};
1818

19+
exports.setCacheDir = setCacheDir;
20+
1921
var compileCache;
2022
exports.compile = function compile(source, options) {
21-
options = options ? convertOptionsOrThrow(options) :
22-
{compilerOptions: getDefaultCompilerOptions()};
23+
validateAndConvertOptions(options);
24+
25+
if (! options)
26+
options = {compilerOptions: getDefaultCompilerOptions()};
27+
28+
if (! options.compilerOptions)
29+
options.compilerOptions = getDefaultCompilerOptions();
2330

24-
if (! options.useCache) {
31+
if (options.compilerOptions.useCache) {
2532
return tsCompile(source, options);
2633
}
2734

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

35-
function convertOptionsOrThrow(options) {
36-
if (! options.compilerOptions) return null;
42+
var validOptions = {
43+
"compilerOptions": "object",
44+
"filePath": "string",
45+
"moduleName": "string",
46+
"typings": "array"
47+
};
48+
var validOptionsMsg = "Valid options are" +
49+
"compilerOptions, filePath, moduleName, and typings";
3750

38-
var compilerOptions = convertCompilerOptionsOrThrow(options.compilerOptions);
39-
var result = _.clone(options);
40-
result.compilerOptions = compilerOptions;
51+
function validateAndConvertOptions(options) {
52+
if (! options) return;
4153

42-
return result;
43-
}
54+
// Validate top level options.
55+
for (var option in options) {
56+
if (options.hasOwnProperty(option)) {
57+
if (validOptions[option] === undefined) {
58+
throw new Error("Unknown option: " + option + "." +
59+
validOptionsMsg);
60+
}
4461

45-
exports.convertOptionsOrThrow = convertOptionsOrThrow;
62+
if (typeof options[option] !== validOptions[option]) {
63+
throw new Error(option + " should be of type " +
64+
validOptions[option]);
65+
}
66+
}
67+
}
68+
69+
// Validate and convert compilerOptions.
70+
if (options.compilerOptions) {
71+
options.compilerOptions = convertCompilerOptionsOrThrow(
72+
options.compilerOptions);
73+
}
74+
}
4675

4776
exports.getDefaultOptions = function getDefaultOptions() {
4877
return {

options.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
var ts = require("typescript");
44
var _ = require("underscore");
55

6-
function getCompilerOptions(customOptions) {
7-
var compilerOptions = ts.getDefaultCompilerOptions();
6+
function presetCompilerOptions(customOptions) {
7+
if (! customOptions) return;
88

9-
_.extend(compilerOptions, customOptions);
10-
11-
// Support decorators by default.
12-
compilerOptions.experimentalDecorators = true;
9+
var compilerOptions = _.clone(customOptions);
1310

1411
// Declaration files are expected to
1512
// be generated separately.
@@ -43,21 +40,23 @@ function getCompilerOptions(customOptions) {
4340
return compilerOptions;
4441
}
4542

46-
exports.getCompilerOptions = getCompilerOptions;
43+
exports.presetCompilerOptions = presetCompilerOptions;
4744

4845
// Default compiler options.
4946
function getDefaultCompilerOptions() {
5047
return {
51-
module : ts.ModuleKind.None,
52-
target: ts.ScriptTarget.ES5,
48+
module : ts.ModuleKind.CommonJS,
49+
target: ts.ScriptTarget.ES3,
5350
sourceMap: true,
5451
noResolve: false,
5552
diagnostics: true,
5653
// Custom option to turn on/off cache.
5754
useCache: true,
5855
// Always emit class metadata,
5956
// especially useful for Angular2.
60-
emitDecoratorMetadata: true
57+
emitDecoratorMetadata: true,
58+
// Support decorators by default.
59+
experimentalDecorators: true
6160
}
6261
}
6362

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
"random-js": "^1.0.3"
3232
},
3333
"devDependencies": {
34-
"mocha": "^2.2.5"
34+
"jasmine-node": "^1.14.5"
3535
}
3636
}

tests/run.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/usr/bin/env bash
22

33
cd $(dirname $0)
4-
TEST_DIR=$(pwd)
54

6-
TYPESCRIPT_CACHE_DIR=${TEST_DIR}/.cache
7-
export TYPESCRIPT_CACHE_DIR
8-
9-
node tests.js
5+
jasmine-node * --config "TYPESCRIPT_CACHE_DIR" ".cache"

tests/tests.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/ts.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var meteorTS = require("../index");
2+
3+
describe("meteor-typescript", function() {
4+
5+
var testCodeLine = "export const foo = 'foo'";
6+
7+
it("should compile with defaults", function() {
8+
var result = meteorTS.compile(testCodeLine);
9+
expect(result.code.indexOf("exports.foo")).toEqual(0);
10+
});
11+
12+
it("should throw on wrong option", function() {
13+
var test = function() {
14+
meteorTS.compile(testCodeLine, {
15+
"wrong": true
16+
});
17+
};
18+
expect(test).toThrow();
19+
});
20+
21+
it("should recognize preset options", function() {
22+
var result = meteorTS.compile(testCodeLine, {
23+
compilerOptions: {
24+
module: "system"
25+
}
26+
});
27+
expect(result.code.indexOf("System.register")).toEqual(0);
28+
});
29+
30+
it("should add module with moduleName name when moduleName is set",
31+
function() {
32+
var result = meteorTS.compile(testCodeLine, {
33+
compilerOptions: {
34+
module: "system"
35+
},
36+
moduleName: "fooModule"
37+
});
38+
expect(result.code.indexOf("System.register(\"fooModule\""))
39+
.toEqual(0);
40+
});
41+
42+
it("should throw on wrong compiler option", function() {
43+
var test = function() {
44+
meteorTS.compile(testCodeLine, {
45+
compilerOptions: {
46+
module: "wrong"
47+
}
48+
});
49+
};
50+
expect(test).toThrow();
51+
});
52+
53+
});

typescript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
var assert = require("assert");
44
var ts = require("typescript");
5-
var getCompilerOptions = require("./options").getCompilerOptions;
5+
var presetCompilerOptions = require("./options").presetCompilerOptions;
66
var _ = require("underscore");
77
var deepHash = require("./utils").deepHash;
88

99
exports.compile = function compile(fileContent, options) {
10-
var compilerOptions = getCompilerOptions(
10+
var compilerOptions = presetCompilerOptions(
1111
options.compilerOptions);
1212

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

0 commit comments

Comments
 (0)