Skip to content

Commit 7503d14

Browse files
committed
feat(compile) doSemanticChecks, ability to succeed despite semantic errors
1 parent 31edc92 commit 7503d14

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

index.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ var tss;
1919
/**
2020
* @param {ts.CompilerOptions=} options TypeScript compile options (some options are ignored)
2121
*/
22-
function TypeScriptSimple(options) {
22+
function TypeScriptSimple(options, doSemanticChecks) {
2323
if (options === void 0) { options = {}; }
24+
if (doSemanticChecks === void 0) { doSemanticChecks = true; }
25+
this.doSemanticChecks = doSemanticChecks;
2426
this.service = null;
2527
this.outputs = {};
2628
this.files = {};
2729
if (options.target == null) {
28-
options.target = 1 /* ES5 */;
30+
options.target = ts.ScriptTarget.ES5;
2931
}
3032
if (options.module == null) {
31-
options.module = 0 /* None */;
33+
options.module = ts.ModuleKind.None;
3234
}
3335
this.options = options;
3436
}
3537
/**
3638
* @param {string} code TypeScript source code to compile
37-
* @return {string}
39+
* @return {string} The JavaScript with inline sourceMaps if sourceMaps were enabled
3840
*/
3941
TypeScriptSimple.prototype.compile = function (code) {
4042
if (!this.service) {
@@ -77,7 +79,7 @@ var tss;
7779
return path.dirname(require.resolve('typescript'));
7880
};
7981
TypeScriptSimple.prototype.getDefaultLibFilename = function (options) {
80-
if (options.target === 2 /* ES6 */) {
82+
if (options.target === ts.ScriptTarget.ES6) {
8183
return 'lib.es6.d.ts';
8284
}
8385
else {
@@ -86,13 +88,22 @@ var tss;
8688
};
8789
TypeScriptSimple.prototype.toJavaScript = function (service) {
8890
var output = service.getEmitOutput(FILENAME_TS);
89-
if (output.emitOutputStatus === 0 /* Succeeded */) {
91+
// Meaning of succeeded is driven by whether we need to check for semantic errors or not
92+
var succeeded = output.emitOutputStatus === ts.EmitReturnStatus.Succeeded;
93+
if (!this.doSemanticChecks) {
94+
// We have an output. It implies syntactic success
95+
if (!succeeded)
96+
succeeded = !!output.outputFiles.length;
97+
}
98+
if (succeeded) {
9099
var filename = FILENAME_TS.replace(/ts$/, 'js');
91100
var file = output.outputFiles.filter(function (file) { return file.name === filename; })[0];
92101
// Fixed in v1.5 https://github.com/Microsoft/TypeScript/issues/1653
93102
return file.text.replace(/\r\n/g, os.EOL);
94103
}
95-
var allDiagnostics = service.getCompilerOptionsDiagnostics().concat(service.getSyntacticDiagnostics(FILENAME_TS)).concat(service.getSemanticDiagnostics(FILENAME_TS));
104+
var allDiagnostics = service.getCompilerOptionsDiagnostics().concat(service.getSyntacticDiagnostics(FILENAME_TS));
105+
if (this.doSemanticChecks)
106+
allDiagnostics = allDiagnostics.concat(service.getSemanticDiagnostics(FILENAME_TS));
96107
throw new Error(this.formatDiagnostics(allDiagnostics));
97108
};
98109
TypeScriptSimple.prototype.formatDiagnostics = function (diagnostics) {

index.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ module tss {
2222
private service: ts.LanguageService = null;
2323
private outputs: ts.Map<string> = {};
2424
private options: ts.CompilerOptions;
25-
private files: ts.Map<{version: number; text: string;}> = {};
25+
private files: ts.Map<{ version: number; text: string; }> = {};
2626

2727
/**
2828
* @param {ts.CompilerOptions=} options TypeScript compile options (some options are ignored)
2929
*/
30-
constructor(options: ts.CompilerOptions = {}) {
30+
constructor(options: ts.CompilerOptions = {}, private doSemanticChecks = true) {
3131
if (options.target == null) {
3232
options.target = ts.ScriptTarget.ES5;
3333
}
@@ -36,10 +36,10 @@ module tss {
3636
}
3737
this.options = options;
3838
}
39-
39+
4040
/**
4141
* @param {string} code TypeScript source code to compile
42-
* @return {string}
42+
* @return {string} The JavaScript with inline sourceMaps if sourceMaps were enabled
4343
*/
4444
compile(code: string): string {
4545
if (!this.service) {
@@ -97,16 +97,27 @@ module tss {
9797

9898
private toJavaScript(service: ts.LanguageService): string {
9999
var output = service.getEmitOutput(FILENAME_TS);
100-
if (output.emitOutputStatus === ts.EmitReturnStatus.Succeeded) {
100+
101+
// Meaning of succeeded is driven by whether we need to check for semantic errors or not
102+
var succeeded = output.emitOutputStatus === ts.EmitReturnStatus.Succeeded;
103+
if (!this.doSemanticChecks) {
104+
// We have an output. It implies syntactic success
105+
if (!succeeded) succeeded = !!output.outputFiles.length;
106+
}
107+
108+
if (succeeded) {
101109
var filename = FILENAME_TS.replace(/ts$/, 'js');
102110
var file = output.outputFiles.filter((file) => file.name === filename)[0];
103111
// Fixed in v1.5 https://github.com/Microsoft/TypeScript/issues/1653
104112
return file.text.replace(/\r\n/g, os.EOL);
105113
}
106114

107115
var allDiagnostics = service.getCompilerOptionsDiagnostics()
108-
.concat(service.getSyntacticDiagnostics(FILENAME_TS))
109-
.concat(service.getSemanticDiagnostics(FILENAME_TS));
116+
.concat(service.getSyntacticDiagnostics(FILENAME_TS));
117+
118+
if (this.doSemanticChecks)
119+
allDiagnostics = allDiagnostics.concat(service.getSemanticDiagnostics(FILENAME_TS));
120+
110121
throw new Error(this.formatDiagnostics(allDiagnostics));
111122
}
112123

test/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,24 @@ describe('typescript-update', function() {
7575
assert.equal(tss.compile(src), expected);
7676
});
7777
});
78+
79+
context('semantic vs. syntactic errors', function() {
80+
var tss;
81+
beforeEach(function() {
82+
tss = new TypeScriptSimple({target: ts.ScriptTarget.ES5}, false);
83+
});
84+
85+
it('semantic errors are ignored', function() {
86+
var src = "var x: number = 'some string';";
87+
var expected = "var x = 'some string';" + eol;
88+
assert.equal(tss.compile(src), expected);
89+
});
90+
91+
it('syntactic errors are not ignored', function() {
92+
var src = "var x = 123 123;";
93+
assert.throws(function() {
94+
tss.compile(src);
95+
}, /^Error: L1: ',' expected./);
96+
});
97+
});
7898
});

tsconfig.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "1.4.1",
3+
"compilerOptions": {
4+
"target": "es5",
5+
"module": "commonjs",
6+
"declaration": false,
7+
"noImplicitAny": false,
8+
"removeComments": false,
9+
"noLib": false
10+
},
11+
"filesGlob": [
12+
"./**/*.ts",
13+
"!./node_modules/**/*.ts"
14+
],
15+
"files": [
16+
"./index.d.ts",
17+
"./index.ts",
18+
"./typings/bundle.d.ts",
19+
"./typings/node/node.d.ts"
20+
]
21+
}

0 commit comments

Comments
 (0)