Skip to content

Commit cafe821

Browse files
committed
Merge branch 'master' of https://github.com/Microsoft/TypeScript
2 parents 523529c + 8b203ec commit cafe821

17 files changed

Lines changed: 373 additions & 48 deletions

src/compiler/declarationEmitter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,10 @@ namespace ts {
530530
else {
531531
// Expression
532532
const tempVarName = getExportDefaultTempVariableName();
533-
write("declare var ");
533+
if (!noDeclare) {
534+
write("declare ");
535+
}
536+
write("var ");
534537
write(tempVarName);
535538
write(": ");
536539
writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic;

src/compiler/utilities.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,33 @@ namespace ts {
179179
return node.pos;
180180
}
181181

182+
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
183+
Debug.assert(line >= 0);
184+
const lineStarts = getLineStarts(sourceFile);
185+
186+
const lineIndex = line;
187+
const sourceText = sourceFile.text;
188+
if (lineIndex + 1 === lineStarts.length) {
189+
// last line - return EOF
190+
return sourceText.length - 1;
191+
}
192+
else {
193+
// current line start
194+
const start = lineStarts[lineIndex];
195+
// take the start position of the next line - 1 = it should be some line break
196+
let pos = lineStarts[lineIndex + 1] - 1;
197+
Debug.assert(isLineBreak(sourceText.charCodeAt(pos)));
198+
// walk backwards skipping line breaks, stop the the beginning of current line.
199+
// i.e:
200+
// <some text>
201+
// $ <- end of line for this position should match the start position
202+
while (start <= pos && isLineBreak(sourceText.charCodeAt(pos))) {
203+
pos--;
204+
}
205+
return pos;
206+
}
207+
}
208+
182209
// Returns true if this node is missing from the actual source code. A 'missing' node is different
183210
// from 'undefined/defined'. When a node is undefined (which can happen for optional nodes
184211
// in the tree), it is definitely missing. However, a node may be defined, but still be
@@ -364,6 +391,20 @@ namespace ts {
364391
return createTextSpanFromBounds(start, scanner.getTextPos());
365392
}
366393

394+
function getErrorSpanForArrowFunction(sourceFile: SourceFile, node: ArrowFunction): TextSpan {
395+
const pos = skipTrivia(sourceFile.text, node.pos);
396+
if (node.body && node.body.kind === SyntaxKind.Block) {
397+
const { line: startLine } = getLineAndCharacterOfPosition(sourceFile, node.body.pos);
398+
const { line: endLine } = getLineAndCharacterOfPosition(sourceFile, node.body.end);
399+
if (startLine < endLine) {
400+
// The arrow function spans multiple lines,
401+
// make the error span be the first line, inclusive.
402+
return createTextSpan(pos, getEndLinePosition(startLine, sourceFile) - pos + 1);
403+
}
404+
}
405+
return createTextSpanFromBounds(pos, node.end);
406+
}
407+
367408
export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan {
368409
let errorNode = node;
369410
switch (node.kind) {
@@ -392,6 +433,8 @@ namespace ts {
392433
case SyntaxKind.TypeAliasDeclaration:
393434
errorNode = (<Declaration>node).name;
394435
break;
436+
case SyntaxKind.ArrowFunction:
437+
return getErrorSpanForArrowFunction(sourceFile, <ArrowFunction>node);
395438
}
396439

397440
if (errorNode === undefined) {

src/services/utilities.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,6 @@ namespace ts {
66
list: Node;
77
}
88

9-
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
10-
Debug.assert(line >= 0);
11-
let lineStarts = sourceFile.getLineStarts();
12-
13-
let lineIndex = line;
14-
if (lineIndex + 1 === lineStarts.length) {
15-
// last line - return EOF
16-
return sourceFile.text.length - 1;
17-
}
18-
else {
19-
// current line start
20-
let start = lineStarts[lineIndex];
21-
// take the start position of the next line -1 = it should be some line break
22-
let pos = lineStarts[lineIndex + 1] - 1;
23-
Debug.assert(isLineBreak(sourceFile.text.charCodeAt(pos)));
24-
// walk backwards skipping line breaks, stop the the beginning of current line.
25-
// i.e:
26-
// <some text>
27-
// $ <- end of line for this position should match the start position
28-
while (start <= pos && isLineBreak(sourceFile.text.charCodeAt(pos))) {
29-
pos--;
30-
}
31-
return pos;
32-
}
33-
}
34-
359
export function getLineStartPositionForPosition(position: number, sourceFile: SourceFile): number {
3610
let lineStarts = sourceFile.getLineStarts();
3711
let line = sourceFile.getLineAndCharacterOfPosition(position).line;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
tests/cases/compiler/arrowFunctionErrorSpan.ts(4,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
2+
Type 'void' is not assignable to type 'number'.
3+
tests/cases/compiler/arrowFunctionErrorSpan.ts(7,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
4+
Type 'void' is not assignable to type 'number'.
5+
tests/cases/compiler/arrowFunctionErrorSpan.ts(12,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
6+
Type 'void' is not assignable to type 'number'.
7+
tests/cases/compiler/arrowFunctionErrorSpan.ts(17,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
8+
Type 'void' is not assignable to type 'number'.
9+
tests/cases/compiler/arrowFunctionErrorSpan.ts(18,5): error TS1200: Line terminator not permitted before arrow.
10+
tests/cases/compiler/arrowFunctionErrorSpan.ts(21,3): error TS2345: Argument of type '(a: any, b: any, c: any, d: any) => void' is not assignable to parameter of type '() => number'.
11+
tests/cases/compiler/arrowFunctionErrorSpan.ts(28,7): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
12+
Type 'void' is not assignable to type 'number'.
13+
tests/cases/compiler/arrowFunctionErrorSpan.ts(32,7): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
14+
Type 'void' is not assignable to type 'number'.
15+
tests/cases/compiler/arrowFunctionErrorSpan.ts(36,7): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
16+
Type 'void' is not assignable to type 'number'.
17+
tests/cases/compiler/arrowFunctionErrorSpan.ts(43,5): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
18+
Type 'void' is not assignable to type 'number'.
19+
tests/cases/compiler/arrowFunctionErrorSpan.ts(52,3): error TS2345: Argument of type '(_: any) => number' is not assignable to parameter of type '() => number'.
20+
21+
22+
==== tests/cases/compiler/arrowFunctionErrorSpan.ts (11 errors) ====
23+
function f(a: () => number) { }
24+
25+
// oneliner
26+
f(() => { });
27+
~~~~~~~~~
28+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
29+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
30+
31+
// multiline, body
32+
f(() => {
33+
~~~~~~~
34+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
35+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
36+
37+
});
38+
39+
// multiline 2, body
40+
f(() => {
41+
~~~~~~~
42+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
43+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
44+
45+
});
46+
47+
// multiline 3, arrow on a new line
48+
f(()
49+
~~
50+
=> { });
51+
~~~~~~~~~~
52+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
53+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
54+
~~
55+
!!! error TS1200: Line terminator not permitted before arrow.
56+
57+
// multiline 4, arguments
58+
f((a,
59+
~~~
60+
b,
61+
~~~~~~
62+
c,
63+
~~~~~~
64+
d) => { });
65+
~~~~~~~~~~~~~
66+
!!! error TS2345: Argument of type '(a: any, b: any, c: any, d: any) => void' is not assignable to parameter of type '() => number'.
67+
68+
// single line with a comment
69+
f(/*
70+
*/() => { });
71+
~~~~~~~~~
72+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
73+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
74+
75+
// multi line with a comment
76+
f(/*
77+
*/() => { });
78+
~~~~~~~~~
79+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
80+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
81+
82+
// multi line with a comment 2
83+
f(/*
84+
*/() => {
85+
~~~~~~~~
86+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
87+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
88+
89+
});
90+
91+
// multi line with a comment 3
92+
f( // comment 1
93+
// comment 2
94+
() =>
95+
~~~~~
96+
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
97+
!!! error TS2345: Type 'void' is not assignable to type 'number'.
98+
// comment 3
99+
{
100+
// comment 4
101+
}
102+
// comment 5
103+
);
104+
105+
// body is not a block
106+
f(_ => 1 +
107+
~~~~~~~~
108+
2);
109+
~~~~~
110+
!!! error TS2345: Argument of type '(_: any) => number' is not assignable to parameter of type '() => number'.
111+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//// [arrowFunctionErrorSpan.ts]
2+
function f(a: () => number) { }
3+
4+
// oneliner
5+
f(() => { });
6+
7+
// multiline, body
8+
f(() => {
9+
10+
});
11+
12+
// multiline 2, body
13+
f(() => {
14+
15+
});
16+
17+
// multiline 3, arrow on a new line
18+
f(()
19+
=> { });
20+
21+
// multiline 4, arguments
22+
f((a,
23+
b,
24+
c,
25+
d) => { });
26+
27+
// single line with a comment
28+
f(/*
29+
*/() => { });
30+
31+
// multi line with a comment
32+
f(/*
33+
*/() => { });
34+
35+
// multi line with a comment 2
36+
f(/*
37+
*/() => {
38+
39+
});
40+
41+
// multi line with a comment 3
42+
f( // comment 1
43+
// comment 2
44+
() =>
45+
// comment 3
46+
{
47+
// comment 4
48+
}
49+
// comment 5
50+
);
51+
52+
// body is not a block
53+
f(_ => 1 +
54+
2);
55+
56+
57+
//// [arrowFunctionErrorSpan.js]
58+
function f(a) { }
59+
// oneliner
60+
f(function () { });
61+
// multiline, body
62+
f(function () {
63+
});
64+
// multiline 2, body
65+
f(function () {
66+
});
67+
// multiline 3, arrow on a new line
68+
f(function () { });
69+
// multiline 4, arguments
70+
f(function (a, b, c, d) { });
71+
// single line with a comment
72+
f(/*
73+
*/ function () { });
74+
// multi line with a comment
75+
f(/*
76+
*/ function () { });
77+
// multi line with a comment 2
78+
f(/*
79+
*/ function () {
80+
});
81+
// multi line with a comment 3
82+
f(// comment 1
83+
// comment 2
84+
function () {
85+
// comment 4
86+
});
87+
// body is not a block
88+
f(function (_) { return 1 +
89+
2; });
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [pi.ts]
2+
export default 3.14159;
3+
4+
//// [pi.js]
5+
System.register([], function(exports_1, context_1) {
6+
"use strict";
7+
var __moduleName = context_1 && context_1.id;
8+
return {
9+
setters:[],
10+
execute: function() {
11+
exports_1("default",3.14159);
12+
}
13+
}
14+
});
15+
16+
17+
//// [pi.d.ts]
18+
declare var _default: number;
19+
export default _default;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
=== tests/cases/compiler/pi.ts ===
2+
export default 3.14159;
3+
No type information for this code.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
=== tests/cases/compiler/pi.ts ===
2+
export default 3.14159;
3+
No type information for this code.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [pi.ts]
2+
3+
export default 3.14159;
4+
5+
//// [app.js]
6+
System.register("pi", [], function(exports_1, context_1) {
7+
"use strict";
8+
var __moduleName = context_1 && context_1.id;
9+
return {
10+
setters:[],
11+
execute: function() {
12+
exports_1("default",3.14159);
13+
}
14+
}
15+
});
16+
17+
18+
//// [app.d.ts]
19+
declare module "pi" {
20+
var _default: number;
21+
export default _default;
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/pi.ts ===
2+
3+
No type information for this code.export default 3.14159;
4+
No type information for this code.

0 commit comments

Comments
 (0)