Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.

Commit e352042

Browse files
committed
Merge pull request #4 from teppeis/typescript-1.5
[WIP] Support for TypeScript v1.5
2 parents ff40b29 + 3bb0ec9 commit e352042

File tree

7 files changed

+1500
-1409
lines changed

7 files changed

+1500
-1409
lines changed

dtsm.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"bundle": "typings/bundle.d.ts",
1010
"dependencies": {
1111
"node/node.d.ts": {
12-
"ref": "6425747807265d7640d73234334aa6ae56b89e87"
12+
"ref": "fbb8c672d56d1cfe677b81c300f8506a31e0404c"
1313
}
1414
}
15-
}
15+
}

index.d.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="typings/node/node.d.ts" />
21
/// <reference path="node_modules/typescript/bin/typescript.d.ts" />
32
import ts = require('typescript');
43
declare function tss(code: string, options: ts.CompilerOptions): string;
@@ -17,21 +16,24 @@ declare module tss {
1716
constructor(options?: ts.CompilerOptions, doSemanticChecks?: boolean);
1817
/**
1918
* @param {string} code TypeScript source code to compile
20-
* @param {string=} filename Only needed if you plan to use sourceMaps. Provide the complete filePath relevant to you
19+
* @param {string=} fileName Only needed if you plan to use sourceMaps.
20+
* Provide the complete filePath relevant to you
2121
* @return {string} The JavaScript with inline sourceMaps if sourceMaps were enabled
2222
* @throw {Error} A syntactic error or a semantic error (if doSemanticChecks is true)
2323
*/
24-
compile(code: string, filename?: string): string;
24+
compile(code: string, fileName?: string): string;
2525
private createService();
2626
private getTypeScriptBinDir();
27-
private getDefaultLibFilename(options);
27+
private getDefaultLibFileName(options);
2828
/**
29-
* converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}
30-
* to {"version":3,"sources":["foo/test.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]}
29+
* converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],
30+
* "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}
31+
* to {"version":3,"sources":["foo/test.ts"],"names":[],
32+
* "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]}
3133
* derived from : https://github.com/thlorenz/convert-source-map
3234
*/
33-
private getInlineSourceMap(mapText, filename);
34-
private toJavaScript(service, filename?);
35+
private getInlineSourceMap(mapText, fileName);
36+
private toJavaScript(service, fileName?);
3537
private formatDiagnostics(diagnostics);
3638
}
3739
}

index.ts

+56-49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="typings/node/node.d.ts" />
21
/// <reference path="node_modules/typescript/bin/typescript.d.ts" />
32

43
import fs = require('fs');
@@ -41,11 +40,12 @@ module tss {
4140

4241
/**
4342
* @param {string} code TypeScript source code to compile
44-
* @param {string=} filename Only needed if you plan to use sourceMaps. Provide the complete filePath relevant to you
43+
* @param {string=} fileName Only needed if you plan to use sourceMaps.
44+
* Provide the complete filePath relevant to you
4545
* @return {string} The JavaScript with inline sourceMaps if sourceMaps were enabled
4646
* @throw {Error} A syntactic error or a semantic error (if doSemanticChecks is true)
4747
*/
48-
compile(code: string, filename = FILENAME_TS): string {
48+
compile(code: string, fileName = FILENAME_TS): string {
4949
if (!this.service) {
5050
this.service = this.createService();
5151
}
@@ -54,44 +54,57 @@ module tss {
5454
file.text = code;
5555
file.version++;
5656

57-
return this.toJavaScript(this.service, filename);
57+
return this.toJavaScript(this.service, fileName);
5858
}
5959

6060
private createService(): ts.LanguageService {
61-
var defaultLib = this.getDefaultLibFilename(this.options);
61+
var defaultLib = this.getDefaultLibFileName(this.options);
6262
var defaultLibPath = path.join(this.getTypeScriptBinDir(), defaultLib);
6363
this.files[defaultLib] = { version: 0, text: fs.readFileSync(defaultLibPath).toString() };
6464
this.files[FILENAME_TS] = { version: 0, text: '' };
6565

66-
var servicesHost: ts.LanguageServiceHost = {
67-
getScriptFileNames: () => [this.getDefaultLibFilename(this.options), FILENAME_TS],
68-
getScriptVersion: (filename) => this.files[filename] && this.files[filename].version.toString(),
69-
getScriptSnapshot: (filename) => {
70-
var file = this.files[filename];
71-
return {
72-
getText: (start, end) => file.text.substring(start, end),
73-
getLength: () => file.text.length,
74-
getLineStartPositions: () => [],
75-
getChangeRange: (oldSnapshot) => undefined
76-
};
66+
var serviceHost: ts.LanguageServiceHost = {
67+
getScriptFileNames: () => [this.getDefaultLibFileName(this.options), FILENAME_TS],
68+
getScriptVersion: (fileName) => this.files[fileName] && this.files[fileName].version.toString(),
69+
getScriptSnapshot: (fileName) => {
70+
var file = this.files[fileName];
71+
if (file) {
72+
return {
73+
getText: (start, end) => file.text.substring(start, end),
74+
getLength: () => file.text.length,
75+
getLineStartPositions: (): number[]=> [],
76+
getChangeRange: (oldSnapshot) => undefined
77+
};
78+
}
79+
else { // This is some reference import
80+
return {
81+
getText: (start, end) => '',
82+
getLength: () => 0,
83+
getLineStartPositions: (): number[]=> [],
84+
getChangeRange: (oldSnapshot) => undefined
85+
};
86+
}
7787
},
7888
getCurrentDirectory: () => process.cwd(),
7989
getScriptIsOpen: () => true,
8090
getCompilationSettings: () => this.options,
81-
getDefaultLibFilename: (options: ts.CompilerOptions) => {
82-
return this.getDefaultLibFilename(options);
91+
getDefaultLibFileName: (options: ts.CompilerOptions) => {
92+
return this.getDefaultLibFileName(options);
8393
},
84-
log: (message) => console.log(message)
94+
getNewLine: () => os.EOL,
95+
log: (message: string) => console.log(message),
96+
trace: (message: string) => console.debug(message),
97+
error: (message: string) => console.error(message)
8598
};
8699

87-
return ts.createLanguageService(servicesHost, ts.createDocumentRegistry())
100+
return ts.createLanguageService(serviceHost, ts.createDocumentRegistry())
88101
}
89102

90103
private getTypeScriptBinDir(): string {
91104
return path.dirname(require.resolve('typescript'));
92105
}
93106

94-
private getDefaultLibFilename(options: ts.CompilerOptions): string {
107+
private getDefaultLibFileName(options: ts.CompilerOptions): string {
95108
if (options.target === ts.ScriptTarget.ES6) {
96109
return 'lib.es6.d.ts';
97110
} else {
@@ -100,56 +113,50 @@ module tss {
100113
}
101114

102115
/**
103-
* converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}
104-
* to {"version":3,"sources":["foo/test.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]}
116+
* converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[],
117+
* "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}
118+
* to {"version":3,"sources":["foo/test.ts"],"names":[],
119+
* "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]}
105120
* derived from : https://github.com/thlorenz/convert-source-map
106121
*/
107-
private getInlineSourceMap(mapText: string, filename: string): string {
122+
private getInlineSourceMap(mapText: string, fileName: string): string {
108123
var sourceMap = JSON.parse(mapText);
109-
sourceMap.file = filename;
110-
sourceMap.sources = [filename];
124+
sourceMap.file = fileName;
125+
sourceMap.sources = [fileName];
111126
sourceMap.sourcesContent = [this.files[FILENAME_TS].text];
112127
delete sourceMap.sourceRoot;
113128
return JSON.stringify(sourceMap);
114129
}
115130

116-
private toJavaScript(service: ts.LanguageService, filename = FILENAME_TS): string {
131+
private toJavaScript(service: ts.LanguageService, fileName = FILENAME_TS): string {
117132
var output = service.getEmitOutput(FILENAME_TS);
118133

119-
// Meaning of succeeded is driven by whether we need to check for semantic errors or not
120-
var succeeded = output.emitOutputStatus === ts.EmitReturnStatus.Succeeded;
121-
if (!this.doSemanticChecks && !succeeded) {
122-
// We have an output. It implies syntactic success
123-
succeeded = !!output.outputFiles.length;
124-
}
125-
126-
if (!succeeded) {
127-
var allDiagnostics = service.getCompilerOptionsDiagnostics()
128-
.concat(service.getSyntacticDiagnostics(FILENAME_TS));
134+
var allDiagnostics = service.getCompilerOptionsDiagnostics()
135+
.concat(service.getSyntacticDiagnostics(FILENAME_TS));
129136

130-
if (this.doSemanticChecks) {
131-
allDiagnostics = allDiagnostics.concat(service.getSemanticDiagnostics(FILENAME_TS));
132-
}
137+
if (this.doSemanticChecks) {
138+
allDiagnostics = allDiagnostics.concat(service.getSemanticDiagnostics(FILENAME_TS));
139+
}
133140

141+
if (allDiagnostics.length) {
134142
throw new Error(this.formatDiagnostics(allDiagnostics));
135143
}
136144

137-
var outputFilename = FILENAME_TS.replace(/ts$/, 'js');
138-
var file = output.outputFiles.filter((file) => file.name === outputFilename)[0];
139-
// TODO: Fixed in v1.5 https://github.com/Microsoft/TypeScript/issues/1653
140-
var text = file.text.replace(/\r\n/g, os.EOL);
145+
var outputFileName = FILENAME_TS.replace(/ts$/, 'js');
146+
var file = output.outputFiles.filter((file) => file.name === outputFileName)[0];
147+
var text = file.text;
141148

142149
// If we have sourceMaps convert them to inline sourceMaps
143150
if (this.options.sourceMap) {
144-
var sourceMapFilename = FILENAME_TS.replace(/ts$/, 'js.map');
145-
var sourceMapFile = output.outputFiles.filter((file) => file.name === sourceMapFilename)[0];
151+
var sourceMapFileName = FILENAME_TS.replace(/ts$/, 'js.map');
152+
var sourceMapFile = output.outputFiles.filter((file) => file.name === sourceMapFileName)[0];
146153

147154
// Transform sourcemap
148155
var sourceMapText = sourceMapFile.text;
149-
sourceMapText = this.getInlineSourceMap(sourceMapText, filename);
156+
sourceMapText = this.getInlineSourceMap(sourceMapText, fileName);
150157
var base64SourceMapText = new Buffer(sourceMapText).toString('base64');
151158
var sourceMapComment = '//# sourceMappingURL=data:application/json;base64,' + base64SourceMapText;
152-
text = text.replace('//# sourceMappingURL=' + sourceMapFilename, sourceMapComment);
159+
text = text.replace('//# sourceMappingURL=' + sourceMapFileName, sourceMapComment);
153160
}
154161

155162
return text;
@@ -158,7 +165,7 @@ module tss {
158165
private formatDiagnostics(diagnostics: ts.Diagnostic[]): string {
159166
return diagnostics.map((d) => {
160167
if (d.file) {
161-
return 'L' + d.file.getLineAndCharacterFromPosition(d.start).line + ': ' + d.messageText;
168+
return 'L' + d.file.getLineAndCharacterOfPosition(d.start).line + ': ' + d.messageText;
162169
} else {
163170
return d.messageText;
164171
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "typescript-simple",
3-
"version": "1.0.3",
3+
"version": "2.0.0-beta.1",
44
"description": "Simple API to compile TypeScript code string to JavaScript. That's all!",
55
"main": "index.js",
66
"scripts": {
77
"test": "npm run build && mocha",
8-
"build": "tsc -d --target ES5 --module commonjs --noImplicitAny index.ts",
8+
"build": "tsc",
99
"prepublish": "npm test"
1010
},
1111
"repository": {
@@ -22,7 +22,7 @@
2222
},
2323
"homepage": "https://github.com/teppeis/typescript-simple",
2424
"dependencies": {
25-
"typescript": "~1.4.1"
25+
"typescript": "~1.5.0-beta"
2626
},
2727
"devDependencies": {
2828
"mocha": "^2.1.0"

test/test.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,20 @@ describe('typescript-update', function() {
3939
var src = "var x: number = 'str';";
4040
assert.throws(function() {
4141
tss(src);
42-
}, /^Error: L1: Type 'string' is not assignable to type 'number'./);
42+
}, /^Error: L0: Type 'string' is not assignable to type 'number'./);
4343
});
4444

45-
it('throws an error for ES6 "let"', function() {
46-
var src = "let x: number = 1;";
47-
assert.throws(function() {
48-
tss(src);
49-
}, /^Error: L1: 'let' declarations are only available when targeting ECMAScript 6 and higher./);
45+
it('compiles ES6 "let" to "var"', function() {
46+
var src = 'let x: number = 1;';
47+
var expected = 'var x = 1;' + eol;
48+
assert.equal(tss(src), expected);
5049
});
5150

5251
it('throws an error for ES6 Promise', function() {
5352
var src = "var x = new Promise(function (resolve, reject) {\n});";
5453
assert.throws(function() {
5554
tss(src);
56-
}, /^Error: L1: Cannot find name 'Promise'./);
55+
}, /^Error: L0: Cannot find name 'Promise'./);
5756
});
5857
});
5958

@@ -63,13 +62,13 @@ describe('typescript-update', function() {
6362
tss = new TypeScriptSimple({target: ts.ScriptTarget.ES6});
6463
});
6564

66-
it('compiles ES6 "let"', function() {
65+
it('compiles ES6 "let" to "let"', function() {
6766
var src = "let x: number = 1;";
6867
var expected = 'let x = 1;' + eol;
6968
assert.equal(tss.compile(src), expected);
7069
});
7170

72-
it('compiles ES6 Promise', function() {
71+
it('does not throw for ES6 Promise', function() {
7372
var src = "var x = new Promise(function (resolve, reject) {" + eol + "});";
7473
var expected = src + eol;
7574
assert.equal(tss.compile(src), expected);
@@ -87,12 +86,20 @@ describe('typescript-update', function() {
8786
var expected = "var x = 'some string';" + eol;
8887
assert.equal(tss.compile(src), expected);
8988
});
89+
90+
it('reference imports are ignored', function() {
91+
var src = "/// <reference path='./typings/tsd'/>" + eol
92+
+ "var x: number = 'some string';";
93+
var expected = "/// <reference path='./typings/tsd'/>" + eol
94+
+ "var x = 'some string';" + eol;
95+
assert.equal(tss.compile(src), expected);
96+
});
9097

9198
it('syntactic errors are not ignored', function() {
9299
var src = "var x = 123 123;";
93100
assert.throws(function() {
94101
tss.compile(src);
95-
}, /^Error: L1: ',' expected./);
102+
}, /^Error: L0: ',' expected./);
96103
});
97104
});
98105

tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.4.1",
2+
"version": "1.5.0",
33
"compilerOptions": {
44
"target": "es5",
55
"module": "commonjs",
@@ -10,12 +10,12 @@
1010
},
1111
"filesGlob": [
1212
"./**/*.ts",
13+
"!./index.d.ts",
1314
"!./node_modules/**/*.ts"
1415
],
1516
"files": [
16-
"./index.d.ts",
1717
"./index.ts",
1818
"./typings/bundle.d.ts",
1919
"./typings/node/node.d.ts"
2020
]
21-
}
21+
}

0 commit comments

Comments
 (0)