Skip to content

Commit e8b0767

Browse files
authored
Infer function expressions in matching contexts (#514)
* legalizes omitting types on function expressions within function type contexts * legalizes omitting any number of arguments
1 parent 2945af6 commit e8b0767

29 files changed

+1198
-550
lines changed

src/ast.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ export abstract class Node {
864864
body: Statement | null,
865865
decorators: DecoratorNode[] | null,
866866
flags: CommonFlags,
867+
arrowKind: ArrowKind,
867868
range: Range
868869
): FunctionDeclaration {
869870
var stmt = new FunctionDeclaration();
@@ -874,6 +875,7 @@ export abstract class Node {
874875
stmt.signature = signature;
875876
stmt.body = body;
876877
stmt.decorators = decorators;
878+
stmt.arrowKind = arrowKind;
877879
return stmt;
878880
}
879881

@@ -1773,6 +1775,16 @@ export class ForStatement extends Statement {
17731775
statement: Statement;
17741776
}
17751777

1778+
/** Indicates the kind of an array function. */
1779+
export const enum ArrowKind {
1780+
/** Not an arrow function. */
1781+
NONE,
1782+
/** Parenthesized parameter list. */
1783+
ARROW_PARENTHESIZED,
1784+
/** Single parameter without parenthesis. */
1785+
ARROW_SINGLE
1786+
}
1787+
17761788
/** Represents a `function` declaration. */
17771789
export class FunctionDeclaration extends DeclarationStatement {
17781790
kind = NodeKind.FUNCTIONDECLARATION;
@@ -1783,6 +1795,8 @@ export class FunctionDeclaration extends DeclarationStatement {
17831795
signature: SignatureNode;
17841796
/** Body statement. Usually a block. */
17851797
body: Statement | null;
1798+
/** Arrow function kind, if applicable. */
1799+
arrowKind: ArrowKind;
17861800

17871801
get isGeneric(): bool {
17881802
var typeParameters = this.typeParameters;
@@ -1792,7 +1806,14 @@ export class FunctionDeclaration extends DeclarationStatement {
17921806
/** Clones this function declaration. */
17931807
clone(): FunctionDeclaration {
17941808
return Node.createFunctionDeclaration(
1795-
this.name, this.typeParameters, this.signature, this.body, this.decorators, this.flags, this.range
1809+
this.name,
1810+
this.typeParameters,
1811+
this.signature,
1812+
this.body,
1813+
this.decorators,
1814+
this.flags,
1815+
this.arrowKind,
1816+
this.range
17961817
);
17971818
}
17981819
}
@@ -1960,3 +1981,12 @@ export function mangleInternalPath(path: string): string {
19601981
if (path.endsWith(".ts")) path = path.substring(0, path.length - 3);
19611982
return path;
19621983
}
1984+
1985+
/** Tests if the specified type node represents an omitted type. */
1986+
export function isTypeOmitted(type: CommonTypeNode): bool {
1987+
if (type.kind == NodeKind.TYPE) {
1988+
let name = (<TypeNode>type).name;
1989+
return !(name.next || name.identifier.text.length);
1990+
}
1991+
return false;
1992+
}

src/common.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,31 @@ export enum CommonFlags {
5151
INSTANCE = 1 << 17,
5252
/** Is a constructor. */
5353
CONSTRUCTOR = 1 << 18,
54-
/** Is an arrow function. */
55-
ARROW = 1 << 19,
5654
/** Is a module export. */
57-
MODULE_EXPORT = 1 << 20,
55+
MODULE_EXPORT = 1 << 19,
5856
/** Is a module import. */
59-
MODULE_IMPORT = 1 << 21,
57+
MODULE_IMPORT = 1 << 20,
6058

6159
// Compilation states
6260

6361
/** Is resolved. */
64-
RESOLVED = 1 << 22,
62+
RESOLVED = 1 << 21,
6563
/** Is compiled. */
66-
COMPILED = 1 << 23,
64+
COMPILED = 1 << 22,
6765
/** Has a constant value and is therefore inlined. */
68-
INLINED = 1 << 24,
66+
INLINED = 1 << 23,
6967
/** Is scoped. */
70-
SCOPED = 1 << 25,
68+
SCOPED = 1 << 24,
7169
/** Is a trampoline. */
72-
TRAMPOLINE = 1 << 26,
70+
TRAMPOLINE = 1 << 25,
7371
/** Is a virtual method. */
74-
VIRTUAL = 1 << 27,
72+
VIRTUAL = 1 << 26,
7573
/** Is the main function. */
76-
MAIN = 1 << 28,
74+
MAIN = 1 << 27,
7775

7876
// Other
7977

80-
QUOTED = 1 << 29
78+
QUOTED = 1 << 28
8179
}
8280

8381
/** Path delimiter inserted between file system levels. */

0 commit comments

Comments
 (0)