mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-15 08:20:53 -06:00
Merge branch 'master' of https://github.com/Microsoft/TypeScript
This commit is contained in:
commit
cafe821e0c
@ -530,7 +530,10 @@ namespace ts {
|
||||
else {
|
||||
// Expression
|
||||
const tempVarName = getExportDefaultTempVariableName();
|
||||
write("declare var ");
|
||||
if (!noDeclare) {
|
||||
write("declare ");
|
||||
}
|
||||
write("var ");
|
||||
write(tempVarName);
|
||||
write(": ");
|
||||
writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic;
|
||||
|
||||
@ -179,6 +179,33 @@ namespace ts {
|
||||
return node.pos;
|
||||
}
|
||||
|
||||
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
|
||||
Debug.assert(line >= 0);
|
||||
const lineStarts = getLineStarts(sourceFile);
|
||||
|
||||
const lineIndex = line;
|
||||
const sourceText = sourceFile.text;
|
||||
if (lineIndex + 1 === lineStarts.length) {
|
||||
// last line - return EOF
|
||||
return sourceText.length - 1;
|
||||
}
|
||||
else {
|
||||
// current line start
|
||||
const start = lineStarts[lineIndex];
|
||||
// take the start position of the next line - 1 = it should be some line break
|
||||
let pos = lineStarts[lineIndex + 1] - 1;
|
||||
Debug.assert(isLineBreak(sourceText.charCodeAt(pos)));
|
||||
// walk backwards skipping line breaks, stop the the beginning of current line.
|
||||
// i.e:
|
||||
// <some text>
|
||||
// $ <- end of line for this position should match the start position
|
||||
while (start <= pos && isLineBreak(sourceText.charCodeAt(pos))) {
|
||||
pos--;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if this node is missing from the actual source code. A 'missing' node is different
|
||||
// from 'undefined/defined'. When a node is undefined (which can happen for optional nodes
|
||||
// in the tree), it is definitely missing. However, a node may be defined, but still be
|
||||
@ -364,6 +391,20 @@ namespace ts {
|
||||
return createTextSpanFromBounds(start, scanner.getTextPos());
|
||||
}
|
||||
|
||||
function getErrorSpanForArrowFunction(sourceFile: SourceFile, node: ArrowFunction): TextSpan {
|
||||
const pos = skipTrivia(sourceFile.text, node.pos);
|
||||
if (node.body && node.body.kind === SyntaxKind.Block) {
|
||||
const { line: startLine } = getLineAndCharacterOfPosition(sourceFile, node.body.pos);
|
||||
const { line: endLine } = getLineAndCharacterOfPosition(sourceFile, node.body.end);
|
||||
if (startLine < endLine) {
|
||||
// The arrow function spans multiple lines,
|
||||
// make the error span be the first line, inclusive.
|
||||
return createTextSpan(pos, getEndLinePosition(startLine, sourceFile) - pos + 1);
|
||||
}
|
||||
}
|
||||
return createTextSpanFromBounds(pos, node.end);
|
||||
}
|
||||
|
||||
export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan {
|
||||
let errorNode = node;
|
||||
switch (node.kind) {
|
||||
@ -392,6 +433,8 @@ namespace ts {
|
||||
case SyntaxKind.TypeAliasDeclaration:
|
||||
errorNode = (<Declaration>node).name;
|
||||
break;
|
||||
case SyntaxKind.ArrowFunction:
|
||||
return getErrorSpanForArrowFunction(sourceFile, <ArrowFunction>node);
|
||||
}
|
||||
|
||||
if (errorNode === undefined) {
|
||||
|
||||
@ -6,32 +6,6 @@ namespace ts {
|
||||
list: Node;
|
||||
}
|
||||
|
||||
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
|
||||
Debug.assert(line >= 0);
|
||||
let lineStarts = sourceFile.getLineStarts();
|
||||
|
||||
let lineIndex = line;
|
||||
if (lineIndex + 1 === lineStarts.length) {
|
||||
// last line - return EOF
|
||||
return sourceFile.text.length - 1;
|
||||
}
|
||||
else {
|
||||
// current line start
|
||||
let start = lineStarts[lineIndex];
|
||||
// take the start position of the next line -1 = it should be some line break
|
||||
let pos = lineStarts[lineIndex + 1] - 1;
|
||||
Debug.assert(isLineBreak(sourceFile.text.charCodeAt(pos)));
|
||||
// walk backwards skipping line breaks, stop the the beginning of current line.
|
||||
// i.e:
|
||||
// <some text>
|
||||
// $ <- end of line for this position should match the start position
|
||||
while (start <= pos && isLineBreak(sourceFile.text.charCodeAt(pos))) {
|
||||
pos--;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
export function getLineStartPositionForPosition(position: number, sourceFile: SourceFile): number {
|
||||
let lineStarts = sourceFile.getLineStarts();
|
||||
let line = sourceFile.getLineAndCharacterOfPosition(position).line;
|
||||
|
||||
111
tests/baselines/reference/arrowFunctionErrorSpan.errors.txt
Normal file
111
tests/baselines/reference/arrowFunctionErrorSpan.errors.txt
Normal file
@ -0,0 +1,111 @@
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(4,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(7,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(12,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(17,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(18,5): error TS1200: Line terminator not permitted before arrow.
|
||||
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'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(28,7): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(32,7): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(36,7): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(43,5): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
Type 'void' is not assignable to type 'number'.
|
||||
tests/cases/compiler/arrowFunctionErrorSpan.ts(52,3): error TS2345: Argument of type '(_: any) => number' is not assignable to parameter of type '() => number'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/arrowFunctionErrorSpan.ts (11 errors) ====
|
||||
function f(a: () => number) { }
|
||||
|
||||
// oneliner
|
||||
f(() => { });
|
||||
~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
|
||||
// multiline, body
|
||||
f(() => {
|
||||
~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
|
||||
});
|
||||
|
||||
// multiline 2, body
|
||||
f(() => {
|
||||
~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
|
||||
});
|
||||
|
||||
// multiline 3, arrow on a new line
|
||||
f(()
|
||||
~~
|
||||
=> { });
|
||||
~~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
~~
|
||||
!!! error TS1200: Line terminator not permitted before arrow.
|
||||
|
||||
// multiline 4, arguments
|
||||
f((a,
|
||||
~~~
|
||||
b,
|
||||
~~~~~~
|
||||
c,
|
||||
~~~~~~
|
||||
d) => { });
|
||||
~~~~~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '(a: any, b: any, c: any, d: any) => void' is not assignable to parameter of type '() => number'.
|
||||
|
||||
// single line with a comment
|
||||
f(/*
|
||||
*/() => { });
|
||||
~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
|
||||
// multi line with a comment
|
||||
f(/*
|
||||
*/() => { });
|
||||
~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
|
||||
// multi line with a comment 2
|
||||
f(/*
|
||||
*/() => {
|
||||
~~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
|
||||
});
|
||||
|
||||
// multi line with a comment 3
|
||||
f( // comment 1
|
||||
// comment 2
|
||||
() =>
|
||||
~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => number'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'number'.
|
||||
// comment 3
|
||||
{
|
||||
// comment 4
|
||||
}
|
||||
// comment 5
|
||||
);
|
||||
|
||||
// body is not a block
|
||||
f(_ => 1 +
|
||||
~~~~~~~~
|
||||
2);
|
||||
~~~~~
|
||||
!!! error TS2345: Argument of type '(_: any) => number' is not assignable to parameter of type '() => number'.
|
||||
|
||||
89
tests/baselines/reference/arrowFunctionErrorSpan.js
Normal file
89
tests/baselines/reference/arrowFunctionErrorSpan.js
Normal file
@ -0,0 +1,89 @@
|
||||
//// [arrowFunctionErrorSpan.ts]
|
||||
function f(a: () => number) { }
|
||||
|
||||
// oneliner
|
||||
f(() => { });
|
||||
|
||||
// multiline, body
|
||||
f(() => {
|
||||
|
||||
});
|
||||
|
||||
// multiline 2, body
|
||||
f(() => {
|
||||
|
||||
});
|
||||
|
||||
// multiline 3, arrow on a new line
|
||||
f(()
|
||||
=> { });
|
||||
|
||||
// multiline 4, arguments
|
||||
f((a,
|
||||
b,
|
||||
c,
|
||||
d) => { });
|
||||
|
||||
// single line with a comment
|
||||
f(/*
|
||||
*/() => { });
|
||||
|
||||
// multi line with a comment
|
||||
f(/*
|
||||
*/() => { });
|
||||
|
||||
// multi line with a comment 2
|
||||
f(/*
|
||||
*/() => {
|
||||
|
||||
});
|
||||
|
||||
// multi line with a comment 3
|
||||
f( // comment 1
|
||||
// comment 2
|
||||
() =>
|
||||
// comment 3
|
||||
{
|
||||
// comment 4
|
||||
}
|
||||
// comment 5
|
||||
);
|
||||
|
||||
// body is not a block
|
||||
f(_ => 1 +
|
||||
2);
|
||||
|
||||
|
||||
//// [arrowFunctionErrorSpan.js]
|
||||
function f(a) { }
|
||||
// oneliner
|
||||
f(function () { });
|
||||
// multiline, body
|
||||
f(function () {
|
||||
});
|
||||
// multiline 2, body
|
||||
f(function () {
|
||||
});
|
||||
// multiline 3, arrow on a new line
|
||||
f(function () { });
|
||||
// multiline 4, arguments
|
||||
f(function (a, b, c, d) { });
|
||||
// single line with a comment
|
||||
f(/*
|
||||
*/ function () { });
|
||||
// multi line with a comment
|
||||
f(/*
|
||||
*/ function () { });
|
||||
// multi line with a comment 2
|
||||
f(/*
|
||||
*/ function () {
|
||||
});
|
||||
// multi line with a comment 3
|
||||
f(// comment 1
|
||||
// comment 2
|
||||
function () {
|
||||
// comment 4
|
||||
});
|
||||
// body is not a block
|
||||
f(function (_) { return 1 +
|
||||
2; });
|
||||
@ -0,0 +1,19 @@
|
||||
//// [pi.ts]
|
||||
export default 3.14159;
|
||||
|
||||
//// [pi.js]
|
||||
System.register([], function(exports_1, context_1) {
|
||||
"use strict";
|
||||
var __moduleName = context_1 && context_1.id;
|
||||
return {
|
||||
setters:[],
|
||||
execute: function() {
|
||||
exports_1("default",3.14159);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//// [pi.d.ts]
|
||||
declare var _default: number;
|
||||
export default _default;
|
||||
@ -0,0 +1,3 @@
|
||||
=== tests/cases/compiler/pi.ts ===
|
||||
export default 3.14159;
|
||||
No type information for this code.
|
||||
@ -0,0 +1,3 @@
|
||||
=== tests/cases/compiler/pi.ts ===
|
||||
export default 3.14159;
|
||||
No type information for this code.
|
||||
@ -0,0 +1,22 @@
|
||||
//// [pi.ts]
|
||||
|
||||
export default 3.14159;
|
||||
|
||||
//// [app.js]
|
||||
System.register("pi", [], function(exports_1, context_1) {
|
||||
"use strict";
|
||||
var __moduleName = context_1 && context_1.id;
|
||||
return {
|
||||
setters:[],
|
||||
execute: function() {
|
||||
exports_1("default",3.14159);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//// [app.d.ts]
|
||||
declare module "pi" {
|
||||
var _default: number;
|
||||
export default _default;
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
=== tests/cases/compiler/pi.ts ===
|
||||
|
||||
No type information for this code.export default 3.14159;
|
||||
No type information for this code.
|
||||
@ -0,0 +1,4 @@
|
||||
=== tests/cases/compiler/pi.ts ===
|
||||
|
||||
No type information for this code.export default 3.14159;
|
||||
No type information for this code.
|
||||
@ -30,13 +30,10 @@ tests/cases/conformance/functions/functionImplementationErrors.ts(70,11): error
|
||||
};
|
||||
var f3 = () => {
|
||||
~~~~~~~
|
||||
return '';
|
||||
~~~~~~~~~~~~~~
|
||||
return 3;
|
||||
~~~~~~~~~~~~~
|
||||
};
|
||||
~
|
||||
!!! error TS2354: No best common type exists among return expressions.
|
||||
return '';
|
||||
return 3;
|
||||
};
|
||||
|
||||
// FunctionExpression with no return type annotation with return branch of number[] and other of string[]
|
||||
var f4 = function () {
|
||||
@ -94,13 +91,10 @@ tests/cases/conformance/functions/functionImplementationErrors.ts(70,11): error
|
||||
};
|
||||
var f10 = () => {
|
||||
~~~~~~~
|
||||
return new Derived1();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
return new Derived2();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
};
|
||||
~
|
||||
!!! error TS2354: No best common type exists among return expressions.
|
||||
return new Derived1();
|
||||
return new Derived2();
|
||||
};
|
||||
function f11() {
|
||||
~~~
|
||||
!!! error TS2354: No best common type exists among return expressions.
|
||||
@ -115,11 +109,8 @@ tests/cases/conformance/functions/functionImplementationErrors.ts(70,11): error
|
||||
};
|
||||
var f13 = () => {
|
||||
~~~~~~~
|
||||
return new Base();
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
return new AnotherClass();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
};
|
||||
~
|
||||
!!! error TS2354: No best common type exists among return expressions.
|
||||
return new Base();
|
||||
return new AnotherClass();
|
||||
};
|
||||
|
||||
@ -39,16 +39,13 @@ tests/cases/compiler/overloadresolutionWithConstraintCheckingDeferred.ts(19,14):
|
||||
|
||||
var result3: string = foo(x => { // x has type D
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
var y: G<typeof x>; // error that D does not satisfy constraint, y is of type G<D>, entire call to foo is an error
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
~~~~~~~~
|
||||
!!! error TS2344: Type 'D' does not satisfy the constraint 'A'.
|
||||
return y;
|
||||
~~~~~~~~~~~~~
|
||||
});
|
||||
~
|
||||
!!! error TS2345: Argument of type '(x: D) => G<D>' is not assignable to parameter of type '(x: B) => any'.
|
||||
!!! error TS2345: Types of parameters 'x' and 'x' are incompatible.
|
||||
!!! error TS2345: Type 'B' is not assignable to type 'D'.
|
||||
!!! error TS2345: Property 'q' is missing in type 'B'.
|
||||
var y: G<typeof x>; // error that D does not satisfy constraint, y is of type G<D>, entire call to foo is an error
|
||||
~~~~~~~~
|
||||
!!! error TS2344: Type 'D' does not satisfy the constraint 'A'.
|
||||
return y;
|
||||
});
|
||||
|
||||
@ -16,10 +16,9 @@ tests/cases/compiler/undeclaredModuleError.ts(11,41): error TS2304: Cannot find
|
||||
fs.readFile(originalFilePath, () => {
|
||||
readdir(covFileDir, () => {
|
||||
~~~~~~~
|
||||
} , (error: Error, files: {}[]) => {
|
||||
~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '(stat: any, name: string) => boolean'.
|
||||
!!! error TS2345: Type 'void' is not assignable to type 'boolean'.
|
||||
} , (error: Error, files: {}[]) => {
|
||||
files.forEach((file) => {
|
||||
var fullPath = join(IDoNotExist);
|
||||
~~~~~~~~~~~
|
||||
|
||||
53
tests/cases/compiler/arrowFunctionErrorSpan.ts
Normal file
53
tests/cases/compiler/arrowFunctionErrorSpan.ts
Normal file
@ -0,0 +1,53 @@
|
||||
function f(a: () => number) { }
|
||||
|
||||
// oneliner
|
||||
f(() => { });
|
||||
|
||||
// multiline, body
|
||||
f(() => {
|
||||
|
||||
});
|
||||
|
||||
// multiline 2, body
|
||||
f(() => {
|
||||
|
||||
});
|
||||
|
||||
// multiline 3, arrow on a new line
|
||||
f(()
|
||||
=> { });
|
||||
|
||||
// multiline 4, arguments
|
||||
f((a,
|
||||
b,
|
||||
c,
|
||||
d) => { });
|
||||
|
||||
// single line with a comment
|
||||
f(/*
|
||||
*/() => { });
|
||||
|
||||
// multi line with a comment
|
||||
f(/*
|
||||
*/() => { });
|
||||
|
||||
// multi line with a comment 2
|
||||
f(/*
|
||||
*/() => {
|
||||
|
||||
});
|
||||
|
||||
// multi line with a comment 3
|
||||
f( // comment 1
|
||||
// comment 2
|
||||
() =>
|
||||
// comment 3
|
||||
{
|
||||
// comment 4
|
||||
}
|
||||
// comment 5
|
||||
);
|
||||
|
||||
// body is not a block
|
||||
f(_ => 1 +
|
||||
2);
|
||||
@ -0,0 +1,4 @@
|
||||
// @declaration: true
|
||||
// @module: system
|
||||
// @Filename: pi.ts
|
||||
export default 3.14159;
|
||||
@ -0,0 +1,6 @@
|
||||
// @declaration: true
|
||||
// @module: system
|
||||
// @outFile: app.js
|
||||
|
||||
// @Filename: pi.ts
|
||||
export default 3.14159;
|
||||
Loading…
x
Reference in New Issue
Block a user