Skip to content

Commit e6be42a

Browse files
committed
remove all ES6 syntax
This part of the effort to create an official TypeScript compiler: Urigo/angular2-meteor#89 Urigo/angular2-meteor#90
1 parent 3949d13 commit e6be42a

File tree

8 files changed

+233
-153
lines changed

8 files changed

+233
-153
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
tests/.cache

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## meteor-typescript
2+
3+
TypeScript wrapped for Meteor.

cache.js

Lines changed: 89 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
const path = require("path");
4-
const fs = require("fs");
5-
const assert = require("assert");
6-
const LRU = require("lru-cache");
7-
const utils = require("./utils");
8-
const pkgVersion = require("./package.json").version;
9-
const random = require("random-js")();
3+
var path = require("path");
4+
var fs = require("fs");
5+
var assert = require("assert");
6+
var LRU = require("lru-cache");
7+
var utils = require("./utils");
8+
var pkgVersion = require("./package.json").version;
9+
var random = require("random-js")();
1010

1111
function ensureCacheDir(cacheDir) {
1212
cacheDir = path.resolve(
@@ -29,113 +29,111 @@ function ensureCacheDir(cacheDir) {
2929
return cacheDir;
3030
}
3131

32-
class Cache {
32+
function Cache(compileFn, cacheDir) {
33+
assert.ok(this instanceof Cache);
34+
assert.strictEqual(typeof compileFn, "function");
3335

34-
constructor(compileFn, cacheDir) {
35-
assert.strictEqual(typeof compileFn, "function");
36+
this.compileFn = compileFn;
37+
this.cacheDir = ensureCacheDir(cacheDir);
3638

37-
this.compileFn = compileFn;
38-
this.cacheDir = ensureCacheDir(cacheDir);
39+
var maxSize = process.env.TYPESCRIPT_CACHE_SIZE;
40+
this._cache = new LRU({
41+
max: maxSize || 1024 * 10 * 10
42+
});
43+
};
3944

40-
const maxSize = process.env.TYPESCRIPT_CACHE_SIZE;
41-
this._cache = new LRU({
42-
max: maxSize || 1024 * 10 * 10
43-
});
44-
}
45+
exports.Cache = Cache;
4546

46-
get(source, options) {
47-
let cacheKey = utils.deepHash(pkgVersion, source, options);
48-
let compileResult = this._cache.get(cacheKey);
47+
var Cp = Cache.prototype;
4948

50-
if (! compileResult) {
51-
compileResult = this._readCache(cacheKey);
52-
}
49+
Cp.get = function(source, options) {
50+
var cacheKey = utils.deepHash(pkgVersion, source, options);
51+
var compileResult = this._cache.get(cacheKey);
5352

54-
if (! compileResult) {
55-
compileResult = this.compileFn(source, options);
56-
this._cache.set(cacheKey, compileResult);
57-
this._writeCacheAsync(cacheKey, compileResult);
58-
}
59-
60-
return compileResult;
53+
if (! compileResult) {
54+
compileResult = this._readCache(cacheKey);
6155
}
6256

63-
_cacheFilename(cacheKey) {
64-
// We want cacheKeys to be hex so that they work on any FS
65-
// and never end in .cache.
66-
if (!/^[a-f0-9]+$/.test(cacheKey)) {
67-
throw Error('bad cacheKey: ' + cacheKey);
68-
}
69-
70-
return path.join(this.cacheDir, cacheKey + '.cache');
57+
if (! compileResult) {
58+
compileResult = this.compileFn(source, options);
59+
this._cache.set(cacheKey, compileResult);
60+
this._writeCacheAsync(cacheKey, compileResult);
7161
}
7262

73-
_readFileOrNull(filename) {
74-
try {
75-
return fs.readFileSync(filename, 'utf8');
76-
} catch (e) {
77-
if (e && e.code === 'ENOENT')
78-
return null;
79-
throw e;
80-
}
81-
}
63+
return compileResult;
64+
}
8265

83-
_parseJSONOrNull(json) {
84-
try {
85-
return JSON.parse(json);
86-
} catch (e) {
87-
if (e instanceof SyntaxError)
88-
return null;
89-
throw e;
90-
}
66+
Cp._cacheFilename = function(cacheKey) {
67+
// We want cacheKeys to be hex so that they work on any FS
68+
// and never end in .cache.
69+
if (!/^[a-f0-9]+$/.test(cacheKey)) {
70+
throw Error('bad cacheKey: ' + cacheKey);
9171
}
9272

93-
// Returns null if the file does not exist or can't be parsed; otherwise
94-
// returns the parsed compileResult in the file.
95-
_readAndParseCompileResultOrNull(filename) {
96-
var content = this._readFileOrNull(filename);
97-
return this._parseJSONOrNull(content);
98-
}
73+
return path.join(this.cacheDir, cacheKey + '.cache');
74+
}
9975

100-
_readCache(cacheKey) {
101-
if (! this.cacheDir) {
76+
Cp._readFileOrNull = function(filename) {
77+
try {
78+
return fs.readFileSync(filename, 'utf8');
79+
} catch (e) {
80+
if (e && e.code === 'ENOENT')
10281
return null;
103-
}
82+
throw e;
83+
}
84+
}
10485

105-
var cacheFilename = this._cacheFilename(cacheKey);
106-
var compileResult = this._readAndParseCompileResultOrNull(cacheFilename);
107-
if (! compileResult) {
86+
Cp._parseJSONOrNull = function(json) {
87+
try {
88+
return JSON.parse(json);
89+
} catch (e) {
90+
if (e instanceof SyntaxError)
10891
return null;
109-
}
110-
this._cache.set(cacheKey, compileResult);
111-
112-
return compileResult;
92+
throw e;
11393
}
94+
}
11495

115-
// We want to write the file atomically.
116-
// But we also don't want to block processing on the file write.
117-
_writeFileAsync(filename, contents) {
118-
var tempFilename = filename + '.tmp.' + random.uuid4();
119-
fs.writeFile(tempFilename, contents, (err) => {
120-
// ignore errors, it's just a cache
121-
if (err) {
122-
return;
123-
}
124-
fs.rename(tempFilename, filename, (err) => {
125-
// ignore this error too.
126-
});
127-
});
128-
}
96+
// Returns null if the file does not exist or can't be parsed; otherwise
97+
// returns the parsed compileResult in the file.
98+
Cp._readAndParseCompileResultOrNull = function(filename) {
99+
var content = this._readFileOrNull(filename);
100+
return this._parseJSONOrNull(content);
101+
}
129102

130-
_writeCacheAsync(cacheKey, compileResult) {
131-
if (! this.cacheDir) return;
103+
Cp._readCache = function(cacheKey) {
104+
if (! this.cacheDir) {
105+
return null;
106+
}
132107

133-
var cacheFilename = this._cacheFilename(cacheKey);
134-
var cacheContents = JSON.stringify(compileResult);
135-
this._writeFileAsync(cacheFilename, cacheContents);
108+
var cacheFilename = this._cacheFilename(cacheKey);
109+
var compileResult = this._readAndParseCompileResultOrNull(cacheFilename);
110+
if (! compileResult) {
111+
return null;
136112
}
113+
this._cache.set(cacheKey, compileResult);
137114

115+
return compileResult;
138116
}
139117

140-
exports.Cache = Cache;
118+
// We want to write the file atomically.
119+
// But we also don't want to block processing on the file write.
120+
Cp._writeFileAsync = function(filename, contents) {
121+
var tempFilename = filename + '.tmp.' + random.uuid4();
122+
fs.writeFile(tempFilename, contents, function(err) {
123+
// ignore errors, it's just a cache
124+
if (err) {
125+
return;
126+
}
127+
fs.rename(tempFilename, filename, function(err) {
128+
// ignore this error too.
129+
});
130+
});
131+
}
141132

133+
Cp._writeCacheAsync = function(cacheKey, compileResult) {
134+
if (! this.cacheDir) return;
135+
136+
var cacheFilename = this._cacheFilename(cacheKey);
137+
var cacheContents = JSON.stringify(compileResult);
138+
this._writeFileAsync(cacheFilename, cacheContents);
139+
}

index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
'use strict';
22

3-
const getDefaultOptions = require("./options").getDefaultOptions;
4-
const tsCompile = require("./typescript").compile;
5-
const Cache = require("./cache").Cache;
3+
var getDefaultCompilerOptions = require("./options").getDefaultCompilerOptions;
4+
var convertCompilerOptionsOrThrow = require("./options").convertCompilerOptionsOrThrow;
5+
var tsCompile = require("./typescript").compile;
6+
var Cache = require("./cache").Cache;
7+
var _ = require("underscore");
68

7-
function setCacheDir(cacheDir) {
9+
exports.setCacheDir = function setCacheDir(cacheDir) {
810
if (compileCache && compileCache.cacheDir === cacheDir) {
911
return;
1012
}
1113

1214
compileCache = new Cache(function(source, options) {
1315
return tsCompile(source, options);
1416
}, cacheDir);
15-
}
16-
17-
exports.setCacheDir = setCacheDir;
17+
};
1818

19-
let compileCache;
19+
var compileCache;
2020
exports.compile = function compile(source, options) {
21-
options = options || {compilerOptions: getDefaultOptions()};
21+
options = options ? convertOptionsOrThrow(options) :
22+
{compilerOptions: getDefaultCompilerOptions()};
2223

2324
if (! options.useCache) {
2425
return tsCompile(source, options);
@@ -30,3 +31,21 @@ exports.compile = function compile(source, options) {
3031

3132
return compileCache.get(source, options);
3233
};
34+
35+
function convertOptionsOrThrow(options) {
36+
if (! options.compilerOptions) return null;
37+
38+
var compilerOptions = convertCompilerOptionsOrThrow(options.compilerOptions);
39+
var result = _.clone(options);
40+
result.compilerOptions = compilerOptions;
41+
42+
return result;
43+
}
44+
45+
exports.convertOptionsOrThrow = convertOptionsOrThrow;
46+
47+
exports.getDefaultOptions = function getDefaultOptions() {
48+
return {
49+
compilerOptions: getDefaultCompilerOptions()
50+
}
51+
}

options.js

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const ts = require("typescript");
4-
const _ = require("underscore");
3+
var ts = require("typescript");
4+
var _ = require("underscore");
55

66
function getCompilerOptions(customOptions) {
7-
let compilerOptions = ts.getDefaultCompilerOptions();
7+
var compilerOptions = ts.getDefaultCompilerOptions();
88

99
_.extend(compilerOptions, customOptions);
1010

@@ -46,7 +46,7 @@ function getCompilerOptions(customOptions) {
4646
exports.getCompilerOptions = getCompilerOptions;
4747

4848
// Default compiler options.
49-
function getDefaultOptions() {
49+
function getDefaultCompilerOptions() {
5050
return {
5151
module : ts.ModuleKind.None,
5252
target: ts.ScriptTarget.ES5,
@@ -61,4 +61,54 @@ function getDefaultOptions() {
6161
}
6262
}
6363

64-
exports.getDefaultOptions = getDefaultOptions;
64+
exports.getDefaultCompilerOptions = getDefaultCompilerOptions;
65+
66+
var customOptions = ['useCache'];
67+
function isCustomOption(option) {
68+
return customOptions.indexOf(option) !== -1;
69+
}
70+
71+
function validateCustomOptions(options) {
72+
if ('useCache' in options) {
73+
if (typeof options['useCache'] !== 'boolean') {
74+
throw new Error('useCache should be boolean');
75+
}
76+
}
77+
}
78+
79+
// Validate compiler options and convert them from
80+
// user-friendly format to enum values used by TypeScript:
81+
// 'system' string converted to ts.ModuleKind.System value.
82+
function convertCompilerOptionsOrThrow(options) {
83+
if (! options) return null;
84+
85+
var compilerOptions = _.clone(options);
86+
var customOptions = {};
87+
if (compilerOptions) {
88+
for (var option in compilerOptions) {
89+
if (isCustomOption(option)) {
90+
customOptions[option] = compilerOptions[option];
91+
delete compilerOptions[option];
92+
}
93+
}
94+
}
95+
96+
var testOptions = {};
97+
testOptions.compilerOptions = compilerOptions;
98+
testOptions.files = [];
99+
var result = ts.parseJsonConfigFileContent(testOptions);
100+
101+
if (result.errors && result.errors.length) {
102+
throw new Error(result.errors[0].messageText);
103+
}
104+
105+
validateCustomOptions(customOptions);
106+
107+
// Add converted compiler options plus custom options back.
108+
compilerOptions = _.extend(
109+
result.options, customOptions);
110+
111+
return compilerOptions;
112+
}
113+
114+
exports.convertCompilerOptionsOrThrow = convertCompilerOptionsOrThrow;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "meteor-typescript",
33
"author": "@barbatus",
4-
"version": "0.1.0",
4+
"version": "0.5.2",
55
"license": "MIT",
66
"description": "TypeScript wrapper package for use with Meteor",
77
"keywords": [

tests/tests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ var assert = require("assert");
55
var result = meteorTS.compile("export const foo = 600;");
66
assert.equal(result.code.indexOf("exports.foo = 600;"), 0);
77

8+
var converted = meteorTS.convertOptionsOrThrow({
9+
compilerOptions: {
10+
module: 'system'
11+
}
12+
});

0 commit comments

Comments
 (0)