-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Allow resolving of ES5 default values in functions #25048
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,9 +147,9 @@ export interface ClassMember { | |
| } | ||
|
|
||
| /** | ||
| * A parameter to a function or constructor. | ||
| * A parameter to a constructor. | ||
| */ | ||
| export interface Parameter { | ||
| export interface CtorParameter { | ||
| /** | ||
| * Name of the parameter, if available. | ||
| * | ||
|
|
@@ -180,6 +180,53 @@ export interface Parameter { | |
| decorators: Decorator[]|null; | ||
| } | ||
|
|
||
| /** | ||
| * Definition of a function or method, including its body if present and any parameters. | ||
| * | ||
| * In TypeScript code this metadata will be a simple reflection of the declarations in the node | ||
| * itself. In ES5 code this can be more complicated, as the default values for parameters may | ||
| * be extracted from certain body statements. | ||
| */ | ||
| export interface FunctionDefinition<T extends ts.MethodDeclaration|ts.FunctionDeclaration|ts.FunctionExpression> { | ||
| /** | ||
| * A reference to the node which declares the function. | ||
| */ | ||
| node: T; | ||
|
|
||
| /** | ||
| * Statements of the function body, if a body is present, or null if no body is present. | ||
| * | ||
| * This list may be filtered to exclude statements which perform parameter default value | ||
| * initialization. | ||
| */ | ||
| body: ts.Statement[] | null; | ||
|
|
||
| /** | ||
| * Metadata regarding the function's parameters, including possible default value expressions. | ||
| */ | ||
| parameters: Parameter[]; | ||
| } | ||
|
|
||
| /** | ||
| * A parameter to a function or method. | ||
| */ | ||
| export interface Parameter { | ||
| /** | ||
| * Name of the parameter, if available. | ||
| */ | ||
| name: string|null; | ||
|
|
||
| /** | ||
| * Declaration which created this parameter. | ||
| */ | ||
| node: ts.ParameterDeclaration; | ||
|
|
||
| /** | ||
| * Expression which represents the default value of the parameter, if any. | ||
| */ | ||
| initializer: ts.Expression|null; | ||
| } | ||
|
|
||
| /** | ||
| * The source of an imported symbol, including the original symbol name and the module from which it | ||
| * was imported. | ||
|
|
@@ -273,7 +320,29 @@ export interface ReflectionHost { | |
| * a constructor exists. If the constructor exists and has 0 parameters, this array will be empty. | ||
| * If the class has no constructor, this method returns `null`. | ||
| */ | ||
| getConstructorParameters(declaration: ts.Declaration): Parameter[]|null; | ||
| getConstructorParameters(declaration: ts.Declaration): CtorParameter[]|null; | ||
|
|
||
| /** | ||
| * Reflect over a function and return metadata about its parameters and body. | ||
| * | ||
| * Functions in TypeScript and ES5 code have different AST representations, in particular around | ||
| * default values for parameters. A TypeScript function has its default value as the initializer | ||
| * on the parameter declaration, whereas an ES5 function has its default value set in a statement | ||
| * of the form: | ||
| * | ||
| * if (param === void 0) { param = 3; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps stick some triple back ticks around this. |
||
| * | ||
| * This method abstracts over these details, and interprets the function declaration and body to | ||
| * extract parameter default values and the "real" body. | ||
| * | ||
| * A current limitation is that this metadata has no representation for shorthand assignment of | ||
| * parameter objects in the function signature. | ||
| * | ||
| * @param node a TypeScript `ts.Declaration` node representing the function over which to reflect. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The param's name is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this in my PR |
||
| * | ||
| * @returns a `FunctionDefinition` giving metadata about the function definition. | ||
| */ | ||
| getDefinitionOfFunction<T extends ts.MethodDeclaration|ts.FunctionDeclaration|ts.FunctionExpression>(fn: T): FunctionDefinition<T>; | ||
|
|
||
| /** | ||
| * Determine if an identifier was imported from another module and return `Import` metadata | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
|
|
||
| import * as ts from 'typescript'; | ||
|
|
||
| import {ClassMember, ClassMemberKind, Declaration, Decorator, Import, Parameter, ReflectionHost} from '../../host'; | ||
| import {ClassMember, ClassMemberKind, CtorParameter, Declaration, Decorator, Import, Parameter, ReflectionHost, FunctionDefinition} from '../../host'; | ||
|
|
||
| /** | ||
| * reflector.ts implements static reflection of declarations using the TypeScript `ts.TypeChecker`. | ||
|
|
@@ -31,7 +31,7 @@ export class TypeScriptReflectionHost implements ReflectionHost { | |
| .filter((member): member is ClassMember => member !== null); | ||
| } | ||
|
|
||
| getConstructorParameters(declaration: ts.Declaration): Parameter[]|null { | ||
| getConstructorParameters(declaration: ts.Declaration): CtorParameter[]|null { | ||
| const clazz = castDeclarationToClassOrDie(declaration); | ||
|
|
||
| // First, find the constructor. | ||
|
|
@@ -141,6 +141,18 @@ export class TypeScriptReflectionHost implements ReflectionHost { | |
| return this._getDeclarationOfSymbol(symbol); | ||
| } | ||
|
|
||
| getDefinitionOfFunction<T extends ts.FunctionDeclaration|ts.MethodDeclaration|ts.FunctionExpression>(node: T): FunctionDefinition<T> { | ||
| return { | ||
| node, | ||
| body: node.body !== undefined ? Array.from(node.body.statements) : null, | ||
| parameters: node.parameters.map(node => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming the second
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this in my PR |
||
| const name = parameterName(node.name); | ||
| const initializer = node.initializer || null; | ||
| return {name, node, initializer}; | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a `ts.Symbol` to its declaration, keeping track of the `viaModule` along the way. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be reworded to
This list may have been filtered (...)to indicate theReflectionHostis expected to do that. Currently it reads to me as if consumers ofReflectionHostare responsible for doing so, in which it doesn't make much sense to tell here wat consumers can do with an array :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this in my PR