10000 Review comments · icssjs/TypeScript@155f487 · GitHub
[go: up one dir, main page]

Skip to content

Commit 155f487

Browse files
committed
Review comments
1 parent 632519c commit 155f487

File tree

5 files changed

+284
-34
lines changed

5 files changed

+284
-34
lines changed

src/compiler/utilities.ts

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

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

367393
function getErrorSpanForArrowFunction(sourceFile: SourceFile, node: ArrowFunction): TextSpan {
368394
const pos = skipTrivia(sourceFile.text, node.pos);
369-
370395
if (node.body && node.body.kind === SyntaxKind.Block) {
371-
const {line: startLine } = getLineAndCharacterOfPosition(sourceFile, node.body.pos);
372-
const {line: endLine } = getLineAndCharacterOfPosition(sourceFile, node.body.end);
396+
const { line: startLine } = getLineAndCharacterOfPosition(sourceFile, node.body.pos);
397+
const { line: endLine } = getLineAndCharacterOfPosition(sourceFile, node.body.end);
373398
if (startLine < endLine) {
374-
// The arrow function body spans multiple lines,
375-
// make the error span be the first line.
376-
const endOfFirstLine = getLineStarts(sourceFile)[startLine + 1] - 1;
377-
return createTextSpan(pos, endOfFirstLine - pos);
399+
// The arrow function spans multiple lines,
400+
// make the error span be the first line, inclusive.
401+
return createTextSpan(pos, getEndLinePosition(startLine, sourceFile) - pos + 1);
378402
}
379403
}
380-
381404
return createTextSpanFromBounds(pos, node.end);
382405
}
383406

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+
}
1 97AE 02+
// 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 10000 () { });
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function f(a: () => number) { }
2+
3+
// oneliner
4+
f(() => { });
5+
6+
// multiline, body
7+
f(() => {
8+
9+
});
10+
11+
// multiline 2, body
12+
f(() => {
13+
14+
});
15+
16+
// multiline 3, arrow on a new line
17+
f(()
18+
=> { });
19+
20+
// multiline 4, arguments
21+
f((a,
22+
b,
23+
c,
24+
d) => { });
25+
26+
// single line with a comment
27+
f(/*
28+
*/() => { });
29+
30+
// multi line with a comment
31+
f(/*
32+
*/() => { });
33+
34+
// multi line with a comment 2
35+
f(/*
36+
*/() => {
37+
38+
});
39+
40+
// multi line with a comment 3
41+
f( // comment 1
42+
// comment 2
43+
() =>
44+
// comment 3
45+
{
46+
// comment 4
47+
}
48+
// comment 5
49+
);
50+
51+
// body is not a block
52+
f(_ => 1 +
53+
2);

0 commit comments

Comments
 (0)
0