Skip to content

Start using a union for FunctionLike things #16988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ namespace ts {
return declaration ? getSignatureFromDeclaration(declaration) : undefined;
},
isImplementationOfOverload: node => {
node = getParseTreeNode(node, isFunctionLike);
return node ? isImplementationOfOverload(node) : undefined;
const parsed = getParseTreeNode(node, isFunctionLike);
return parsed ? isImplementationOfOverload(parsed) : undefined;
},
getImmediateAliasedSymbol: symbol => {
Debug.assert((symbol.flags & SymbolFlags.Alias) !== 0, "Should only get Alias here.");
Expand Down Expand Up @@ -12575,7 +12575,7 @@ namespace ts {
}
}

function getContainingObjectLiteral(func: FunctionLikeDeclaration) {
function getContainingObjectLiteral(func: FunctionLike) {
return (func.kind === SyntaxKind.MethodDeclaration ||
func.kind === SyntaxKind.GetAccessor ||
func.kind === SyntaxKind.SetAccessor) && func.parent.kind === SyntaxKind.ObjectLiteralExpression ? <ObjectLiteralExpression>func.parent :
Expand All @@ -12593,7 +12593,7 @@ namespace ts {
});
}

function getContextualThisParameterType(func: FunctionLikeDeclaration): Type {
function getContextualThisParameterType(func: FunctionLike): Type {
if (func.kind === SyntaxKind.ArrowFunction) {
return undefined;
}
Expand Down Expand Up @@ -12770,7 +12770,7 @@ namespace ts {
return false;
}

function getContextualReturnType(functionDecl: FunctionLikeDeclaration): Type {
function getContextualReturnType(functionDecl: FunctionLike): Type {
// If the containing function has a return type annotation, is a constructor, or is a get accessor whose
// corresponding set accessor has a type annotation, return statements in the function are contextually typed
if (functionDecl.kind === SyntaxKind.Constructor ||
Expand Down Expand Up @@ -17866,7 +17866,7 @@ namespace ts {
error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
}
}
if (node.questionToken && isBindingPattern(node.name) && func.body) {
if (node.questionToken && isBindingPattern(node.name) && (func as FunctionLikeDeclaration).body) {
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
}
if ((<Identifier>node.name).text === "this") {
Expand Down Expand Up @@ -18624,12 +18624,12 @@ namespace ts {
let hasOverloads = false;
let bodyDeclaration: FunctionLikeDeclaration;
let lastSeenNonAmbientDeclaration: FunctionLikeDeclaration;
let previousDeclaration: FunctionLikeDeclaration;
let previousDeclaration: FunctionLike;

const declarations = symbol.declarations;
const isConstructor = (symbol.flags & SymbolFlags.Constructor) !== 0;

function reportImplementationExpectedError(node: FunctionLikeDeclaration): void {
function reportImplementationExpectedError(node: FunctionLike): void {
if (node.name && nodeIsMissing(node.name)) {
return;
}
Expand Down Expand Up @@ -18688,7 +18688,7 @@ namespace ts {
let duplicateFunctionDeclaration = false;
let multipleConstructorImplementation = false;
for (const current of declarations) {
const node = <FunctionLikeDeclaration>current;
const node = <FunctionLike>current;
const inAmbientContext = isInAmbientContext(node);
const inAmbientContextOrInterface = node.parent.kind === SyntaxKind.InterfaceDeclaration || node.parent.kind === SyntaxKind.TypeLiteral || inAmbientContext;
if (inAmbientContextOrInterface) {
Expand All @@ -18709,7 +18709,7 @@ namespace ts {
someHaveQuestionToken = someHaveQuestionToken || hasQuestionToken(node);
allHaveQuestionToken = allHaveQuestionToken && hasQuestionToken(node);

if (nodeIsPresent(node.body) && bodyDeclaration) {
if (nodeIsPresent((node as FunctionLikeDeclaration).body) && bodyDeclaration) {
if (isConstructor) {
multipleConstructorImplementation = true;
}
Expand All @@ -18721,9 +18721,9 @@ namespace ts {
reportImplementationExpectedError(previousDeclaration);
}

if (nodeIsPresent(node.body)) {
if (nodeIsPresent((node as FunctionLikeDeclaration).body)) {
if (!bodyDeclaration) {
bodyDeclaration = node;
bodyDeclaration = node as FunctionLikeDeclaration;
}
}
else {
Expand All @@ -18733,7 +18733,7 @@ namespace ts {
previousDeclaration = node;

if (!inAmbientContextOrInterface) {
lastSeenNonAmbientDeclaration = node;
lastSeenNonAmbientDeclaration = node as FunctionLikeDeclaration;
}
}
}
Expand Down Expand Up @@ -19930,7 +19930,7 @@ namespace ts {
forEach((<BindingPattern>node.name).elements, checkSourceElement);
}
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
if (node.initializer && getRootDeclaration(node).kind === SyntaxKind.Parameter && nodeIsMissing(getContainingFunction(node).body)) {
if (node.initializer && getRootDeclaration(node).kind === SyntaxKind.Parameter && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
error(node, Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation);
return;
}
Expand Down Expand Up @@ -20529,12 +20529,12 @@ namespace ts {
// TODO: Check that target label is valid
}

function isGetAccessorWithAnnotatedSetAccessor(node: FunctionLikeDeclaration) {
function isGetAccessorWithAnnotatedSetAccessor(node: FunctionLike) {
return node.kind === SyntaxKind.GetAccessor
&& getEffectiveSetAccessorTypeAnnotationNode(getDeclarationOfKind<SetAccessorDeclaration>(node.symbol, SyntaxKind.SetAccessor)) !== undefined;
}

function isUnwrappedReturnTypeVoidOrAny(func: FunctionLikeDeclaration, returnType: Type): boolean {
function isUnwrappedReturnTypeVoidOrAny(func: FunctionLike, returnType: Type): boolean {
const unwrappedReturnType = (getFunctionFlags(func) & FunctionFlags.AsyncGenerator) === FunctionFlags.Async
? getPromisedTypeOfPromise(returnType) // Async function
: returnType; // AsyncGenerator function, Generator function, or normal function
Expand Down Expand Up @@ -23054,8 +23054,8 @@ namespace ts {
return false;
}

function isImplementationOfOverload(node: FunctionLikeDeclaration) {
if (nodeIsPresent(node.body)) {
function isImplementationOfOverload(node: FunctionLike) {
if (nodeIsPresent((node as FunctionLikeDeclaration).body)) {
const symbol = getSymbolOfNode(node);
const signaturesOfSymbol = getSignaturesOfSymbol(symbol);
// If this function body corresponds to function with multiple signature, it is implementation of overload
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ namespace ts {
const valueDeclaration =
isClassLike(node)
? getFirstConstructorWithBody(node)
: isFunctionLike(node) && nodeIsPresent(node.body)
: isFunctionLike(node) && nodeIsPresent((node as FunctionLikeDeclaration).body)
? node
: undefined;

Expand All @@ -1707,7 +1707,7 @@ namespace ts {
return createArrayLiteral(expressions);
}

function getParametersOfDecoratedDeclaration(node: FunctionLikeDeclaration, container: ClassLikeDeclaration) {
function getParametersOfDecoratedDeclaration(node: FunctionLike, container: ClassLikeDeclaration) {
if (container && node.kind === SyntaxKind.GetAccessor) {
const { setAccessor } = getAllAccessorDeclarations(container.members, <AccessorDeclaration>node);
if (setAccessor) {
Expand Down
53 changes: 40 additions & 13 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,14 +705,24 @@ namespace ts {
initializer?: Expression; // Optional initializer
}

export interface PropertySignature extends TypeElement {
kind: SyntaxKind.PropertySignature | SyntaxKind.JSDocRecordMember;
export interface TSPropertySignature extends TypeElement {
kind: SyntaxKind.PropertySignature;
name: PropertyName; // Declared property name
questionToken?: QuestionToken; // Present on optional property
type?: TypeNode; // Optional type annotation
initializer?: Expression; // Optional initializer
}

export interface JSDocPropertySignature extends TypeElement {
kind: SyntaxKind.JSDocRecordMember;
name: PropertyName; // Declared property name
questionToken?: QuestionToken; // Present on optional property
type?: TypeNode; // Optional type annotation
initializer?: Expression; // Optional initializer
}

export type PropertySignature = TSPropertySignature | JSDocPropertySignature;

export interface PropertyDeclaration extends ClassElement {
kind: SyntaxKind.PropertyDeclaration;
questionToken?: QuestionToken; // Present for use with reporting a grammar error
Expand Down Expand Up @@ -796,21 +806,38 @@ namespace ts {

/**
* Several node kinds share function-like features such as a signature,
* a name, and a body. These nodes should extend FunctionLikeDeclaration.
* a name, and a body. These nodes should extend FunctionLikeDeclarationBase.
* Examples:
* - FunctionDeclaration
* - MethodDeclaration
* - AccessorDeclaration
*/
export interface FunctionLikeDeclaration extends SignatureDeclaration {
export interface FunctionLikeDeclarationBase extends SignatureDeclaration {
_functionLikeDeclarationBrand: any;

asteriskToken?: AsteriskToken;
questionToken?: QuestionToken;
body?: Block | Expression;
}

export interface FunctionDeclaration extends FunctionLikeDeclaration, DeclarationStatement {
export type FunctionLikeDeclaration =
| FunctionDeclaration
| MethodDeclaration
| ConstructorDeclaration
| GetAccessorDeclaration
| SetAccessorDeclaration
| FunctionExpression
| ArrowFunction;
export type FunctionLike =
| FunctionLikeDeclaration
| FunctionTypeNode
| ConstructorTypeNode
| IndexSignatureDeclaration
| MethodSignature
| ConstructSignatureDeclaration
| CallSignatureDeclaration;

export interface FunctionDeclaration extends FunctionLikeDeclarationBase, DeclarationStatement {
kind: SyntaxKind.FunctionDeclaration;
name?: Identifier;
body?: FunctionBody;
Expand All @@ -830,13 +857,13 @@ namespace ts {
// Because of this, it may be necessary to determine what sort of MethodDeclaration you have
// at later stages of the compiler pipeline. In that case, you can either check the parent kind
// of the method, or use helpers like isObjectLiteralMethodDeclaration
export interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement {
export interface MethodDeclaration extends FunctionLikeDeclarationBase, ClassElement, ObjectLiteralElement {
kind: SyntaxKind.MethodDeclaration;
name: PropertyName;
body?: FunctionBody;
}

export interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement {
export interface ConstructorDeclaration extends FunctionLikeDeclarationBase, ClassElement {
kind: SyntaxKind.Constructor;
parent?: ClassDeclaration | ClassExpression;
body?: FunctionBody;
Expand All @@ -850,7 +877,7 @@ namespace ts {

// See the comment on MethodDeclaration for the intuition behind GetAccessorDeclaration being a
// ClassElement and an ObjectLiteralElement.
export interface GetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement {
export interface GetAccessorDeclaration extends FunctionLikeDeclarationBase, ClassElement, ObjectLiteralElement {
kind: SyntaxKind.GetAccessor;
parent?: ClassDeclaration | ClassExpression | ObjectLiteralExpression;
name: PropertyName;
Expand All @@ -859,7 +886,7 @@ namespace ts {

// See the comment on MethodDeclaration for the intuition behind SetAccessorDeclaration being a
// ClassElement and an ObjectLiteralElement.
export interface SetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement {
export interface SetAccessorDeclaration extends FunctionLikeDeclarationBase, ClassElement, ObjectLiteralElement {
kind: SyntaxKind.SetAccessor;
parent?: ClassDeclaration | ClassExpression | ObjectLiteralExpression;
name: PropertyName;
Expand Down Expand Up @@ -1323,13 +1350,13 @@ namespace ts {
export type FunctionBody = Block;
export type ConciseBody = FunctionBody | Expression;

export interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration {
export interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclarationBase {
kind: SyntaxKind.FunctionExpression;
name?: Identifier;
body: FunctionBody; // Required, whereas the member inherited from FunctionDeclaration is optional
}

export interface ArrowFunction extends Expression, FunctionLikeDeclaration {
export interface ArrowFunction extends Expression, FunctionLikeDeclarationBase {
kind: SyntaxKind.ArrowFunction;
equalsGreaterThanToken: EqualsGreaterThanToken;
body: ConciseBody;
Expand Down Expand Up @@ -2110,7 +2137,7 @@ namespace ts {

export type JSDocTypeReferencingNode = JSDocThisType | JSDocConstructorType | JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;

export interface JSDocRecordMember extends PropertySignature {
export interface JSDocRecordMember extends JSDocPropertySignature {
kind: SyntaxKind.JSDocRecordMember;
name: Identifier | StringLiteral | NumericLiteral;
type?: JSDocType;
Expand Down Expand Up @@ -2600,7 +2627,7 @@ namespace ts {
*/
getResolvedSignature(node: CallLikeExpression, candidatesOutArray?: Signature[], argumentCount?: number): Signature | undefined;
getSignatureFromDeclaration(declaration: SignatureDeclaration): Signature | undefined;
isImplementationOfOverload(node: FunctionLikeDeclaration): boolean | undefined;
isImplementationOfOverload(node: FunctionLike): boolean | undefined;
isUndefinedSymbol(symbol: Symbol): boolean;
isArgumentsSymbol(symbol: Symbol): boolean;
isUnknownSymbol(symbol: Symbol): boolean;
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,11 @@ namespace ts {
});
}

export function getContainingFunction(node: Node): FunctionLikeDeclaration {
export function getContainingFunction(node: Node): FunctionLike {
while (true) {
node = node.parent;
if (!node || isFunctionLike(node)) {
return <FunctionLikeDeclaration>node;
return <FunctionLike>node;
}
}
}
Expand Down Expand Up @@ -1885,7 +1885,7 @@ namespace ts {
AsyncGenerator = Async | Generator, // Function is an async generator function
}

export function getFunctionFlags(node: FunctionLikeDeclaration | undefined) {
export function getFunctionFlags(node: FunctionLike | undefined) {
if (!node) {
return FunctionFlags.Invalid;
}
Expand All @@ -1906,7 +1906,7 @@ namespace ts {
break;
}

if (!node.body) {
if (!(node as FunctionLikeDeclaration).body) {
flags |= FunctionFlags.Invalid;
}

Expand Down Expand Up @@ -4810,7 +4810,7 @@ namespace ts {

// Functions

export function isFunctionLike(node: Node): node is FunctionLikeDeclaration {
export function isFunctionLike(node: Node): node is FunctionLike {
return node && isFunctionLikeKind(node.kind);
}

Expand Down
13 changes: 7 additions & 6 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,16 +1049,17 @@ namespace ts.FindAllReferences.Core {
if (isVariableLike(parent) && parent.type === containingTypeReference && parent.initializer && isImplementationExpression(parent.initializer)) {
addReference(parent.initializer);
}
else if (isFunctionLike(parent) && parent.type === containingTypeReference && parent.body) {
if (parent.body.kind === SyntaxKind.Block) {
forEachReturnStatement(<Block>parent.body, returnStatement => {
else if (isFunctionLike(parent) && parent.type === containingTypeReference && (parent as FunctionLikeDeclaration).body) {
const body = (parent as FunctionLikeDeclaration).body;
if (body.kind === SyntaxKind.Block) {
forEachReturnStatement(<Block>body, returnStatement => {
if (returnStatement.expression && isImplementationExpression(returnStatement.expression)) {
addReference(returnStatement.expression);
}
});
}
else if (isImplementationExpression(<Expression>parent.body)) {
addReference(parent.body);
else if (isImplementationExpression(<Expression>body)) {
addReference(body);
}
}
else if (isAssertionExpression(parent) && isImplementationExpression(parent.expression)) {
Expand Down Expand Up @@ -1643,7 +1644,7 @@ namespace ts.FindAllReferences.Core {
}
}
else if (isFunctionLike(node)) {
return !!node.body || hasModifier(node, ModifierFlags.Ambient);
return !!(node as FunctionLikeDeclaration).body || hasModifier(node, ModifierFlags.Ambient);
}
else {
switch (node.kind) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/symbolDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace ts.SymbolDisplay {
else if ((isNameOfFunctionDeclaration(location) && !(symbol.flags & SymbolFlags.Accessor)) || // name of function declaration
(location.kind === SyntaxKind.ConstructorKeyword && location.parent.kind === SyntaxKind.Constructor)) { // At constructor keyword of constructor declaration
// get the signature from the declaration and write it
const functionDeclaration = <FunctionLikeDeclaration>location.parent;
const functionDeclaration = <FunctionLike>location.parent;
// Use function declaration to write the signatures only if the symbol corresponding to this declaration
const locationIsSymbolDeclaration = findDeclaration(symbol, declaration =>
declaration === (location.kind === SyntaxKind.ConstructorKeyword ? functionDeclaration.parent : functionDeclaration));
Expand Down