Skip to content
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

add indexSignature to objects #59

Merged
merged 1 commit into from
Jan 21, 2018
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
3 changes: 3 additions & 0 deletions src/__tests__/__fixtures__/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export interface IButtonProps extends IActionProps {
* @default "button"
*/
type?: string;

/** Index signature for the masses. */
[x: string]: any;
}

/**
Expand Down
75 changes: 58 additions & 17 deletions src/__tests__/__snapshots__/typescript.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Object {
"documentation": undefined,
"fileName": "src/__tests__/__fixtures__/classes.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "new Animal",
"parameters": Array [
Object {
Expand All @@ -43,7 +43,7 @@ Object {
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "noise",
"type": "string",
},
Expand Down Expand Up @@ -81,6 +81,7 @@ repository.",
"isStatic": false,
},
"implements": undefined,
"indexSignature": undefined,
"kind": "class",
"methods": Array [
Object {
Expand Down Expand Up @@ -112,7 +113,7 @@ repository.",
},
"fileName": "src/__tests__/__fixtures__/classes.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "bark",
"parameters": Array [],
"returnType": "string",
Expand Down Expand Up @@ -149,7 +150,7 @@ repository.",
},
"fileName": "src/__tests__/__fixtures__/classes.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "eat",
"parameters": Array [
Object {
Expand All @@ -174,7 +175,7 @@ repository.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "food",
"type": "string",
},
Expand Down Expand Up @@ -211,7 +212,7 @@ repository.",
"documentation": undefined,
"fileName": "src/__tests__/__fixtures__/classes.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "new Dog",
"parameters": Array [],
"returnType": "Dog",
Expand All @@ -236,6 +237,7 @@ repository.",
"isStatic": false,
},
"implements": undefined,
"indexSignature": undefined,
"kind": "class",
"methods": Array [
Object {
Expand Down Expand Up @@ -267,7 +269,7 @@ repository.",
},
"fileName": "src/__tests__/__fixtures__/classes.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "bark",
"parameters": Array [],
"returnType": "string",
Expand Down Expand Up @@ -304,7 +306,7 @@ repository.",
},
"fileName": "src/__tests__/__fixtures__/classes.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "eat",
"parameters": Array [
Object {
Expand All @@ -329,7 +331,7 @@ repository.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "food",
"type": "string",
},
Expand Down Expand Up @@ -364,6 +366,7 @@ Object {
"isStatic": false,
},
"implements": undefined,
"indexSignature": undefined,
"kind": "interface",
"methods": Array [],
"name": "IActionProps",
Expand Down Expand Up @@ -523,6 +526,43 @@ Object {
"isStatic": false,
},
"implements": undefined,
"indexSignature": Object {
"documentation": Object {
"contents": Array [
"<p>Index signature for the masses.</p>
",
],
"contentsRaw": "Index signature for the masses.",
"metadata": Object {},
},
"fileName": "src/__tests__/__fixtures__/interfaces.ts",
"flags": undefined,
"kind": "signature",
"name": "__index",
"parameters": Array [
Object {
"defaultValue": undefined,
"documentation": undefined,
"fileName": undefined,
"flags": Object {
"isDeprecated": undefined,
"isExported": undefined,
"isExternal": undefined,
"isOptional": false,
"isPrivate": undefined,
"isProtected": undefined,
"isPublic": undefined,
"isRest": false,
"isStatic": undefined,
},
"kind": "parameter",
"name": "x",
"type": "string",
},
],
"returnType": "any",
"type": "{ [x: string]: any }",
},
"kind": "interface",
"methods": Array [],
"name": "IButtonProps",
Expand Down Expand Up @@ -828,6 +868,7 @@ Note that this prop has no effect on \`AnchorButton\`; it only affects \`Button\
"isStatic": false,
},
"implements": undefined,
"indexSignature": undefined,
"kind": "interface",
"methods": Array [
Object {
Expand Down Expand Up @@ -861,7 +902,7 @@ callback to extract key from each item.",
},
"fileName": "src/__tests__/__fixtures__/interfaces.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "objectify",
"parameters": Array [
Object {
Expand All @@ -879,7 +920,7 @@ callback to extract key from each item.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "array",
"type": "T[]",
},
Expand All @@ -898,7 +939,7 @@ callback to extract key from each item.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "getKey",
"type": "(item: T) => string",
},
Expand Down Expand Up @@ -948,7 +989,7 @@ is.",
},
"fileName": "src/__tests__/__fixtures__/interfaces.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "renderBlock",
"parameters": Array [
Object {
Expand All @@ -966,7 +1007,7 @@ is.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "blockContent",
"type": "string",
},
Expand All @@ -985,7 +1026,7 @@ is.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "reservedTagWords",
"type": "string[]",
},
Expand Down Expand Up @@ -1024,7 +1065,7 @@ is.",
},
"fileName": "src/__tests__/__fixtures__/interfaces.ts",
"flags": undefined,
"kind": "method signature",
"kind": "signature",
"name": "renderMarkdown",
"parameters": Array [
Object {
Expand All @@ -1042,7 +1083,7 @@ is.",
"isRest": false,
"isStatic": undefined,
},
"kind": "method parameter",
"kind": "parameter",
"name": "markdown",
"type": "string",
},
Expand Down
35 changes: 20 additions & 15 deletions src/client/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export enum Kind {
EnumMember = "enum member",
Interface = "interface",
Method = "method",
MethodParameter = "method parameter",
MethodSignature = "method signature",
Parameter = "parameter",
Signature = "signature",
Property = "property",
TypeAlias = "type alias",
}
Expand Down Expand Up @@ -61,7 +61,7 @@ export interface ITsCallable {
/** Type name from which this method was inherited. Typically takes the form `Interface.member`. */
inheritedFrom?: string;
/** A method has at least one signature, which describes the parameters and return type and contains documentation. */
signatures: ITsMethodSignature[];
signatures: ITsSignature[];
}

/** Re-usable interface for Typescript members that support a notion of "default value." */
Expand All @@ -76,6 +76,8 @@ export interface ITsObjectDefinition {
extends?: string[];
/** List of type names that this definition `implements`. */
implements?: string[];
/** Index signature for this object, if declared. */
indexSignature?: ITsSignature;
/** Property members of this definition. */
properties: ITsProperty[];
/** Method members of this definiton. */
Expand All @@ -95,22 +97,25 @@ export interface ITsMethod extends ITsDocBase, ITsCallable {
kind: Kind.Method;
}

/** Documentation for a single method signature, including parameters, return type, and full type string. */
export interface ITsMethodSignature extends ITsDocBase {
kind: Kind.MethodSignature;
/** Method signatures do not have flags of their own. Flags can be found on the method itself and on each parameter. */
/**
* Documentation for a single signature, including parameters, return type, and full type string.
* Signatures are used for methods and constructors on classes or interfaces, and for index signatures on objects.
*/
export interface ITsSignature extends ITsDocBase {
kind: Kind.Signature;
/** Signatures do not have flags of their own. Flags can be found on the parent and on each parameter. */
flags: undefined;
/** Method parameters, each with their own docs and data. */
parameters: ITsMethodParameter[];
/** Return type of the method. */
/** Signature parameters, each with their own docs and data. */
parameters: ITsParameter[];
/** Return type of the signature. */
returnType: string;
/** Fully qualified type string describing this method, including parameters and return type. */
type: string;
}

/** Documentation for a single parameter to a method signature. */
export interface ITsMethodParameter extends ITsDocBase, ITsDefaultValue {
kind: Kind.MethodParameter;
/** Documentation for a single parameter to a signature. */
export interface ITsParameter extends ITsDocBase, ITsDefaultValue {
kind: Kind.Parameter;
/** Fully qualified type string describing this parameter. */
type: string;
}
Expand Down Expand Up @@ -178,7 +183,7 @@ export const isTsEnum = typeguard<ITsEnum>(Kind.Enum);
export const isTsEnumMember = typeguard<ITsEnumMember>(Kind.EnumMember);
export const isTsInterface = typeguard<ITsInterface>(Kind.Interface);
export const isTsMethod = typeguard<ITsMethod>(Kind.Method);
export const isTsMethodParameter = typeguard<ITsMethodParameter>(Kind.MethodParameter);
export const isTsParameter = typeguard<ITsParameter>(Kind.Parameter);
export const isTsProperty = typeguard<ITsProperty>(Kind.Property);
export const isTsMethodSignature = typeguard<ITsMethodSignature>(Kind.MethodSignature);
export const isTsSignature = typeguard<ITsSignature>(Kind.Signature);
export const isTsTypeAlias = typeguard<ITsTypeAlias>(Kind.TypeAlias);
13 changes: 7 additions & 6 deletions src/plugins/typescript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import {
ITsFlags,
ITsInterface,
ITsMethod,
ITsMethodParameter,
ITsMethodSignature,
ITsParameter,
ITsProperty,
ITsSignature,
ITsTypeAlias,
Kind,
} from "../../client/typescript";
Expand Down Expand Up @@ -81,6 +81,7 @@ export class Visitor {
...this.makeDocEntry(def, Kind.Interface),
extends: def.extendedTypes && def.extendedTypes.map(resolveTypeString),
implements: def.implementedTypes && def.implementedTypes.map(resolveTypeString),
indexSignature: def.indexSignature && this.visitSignature(def.indexSignature),
methods: this.visitChildren(def.getChildrenByKind(ReflectionKind.Method), this.visitMethod, sortStaticFirst),
properties: this.visitChildren(
def.getChildrenByKind(ReflectionKind.Property),
Expand Down Expand Up @@ -116,16 +117,16 @@ export class Visitor {
signatures: def.signatures.map(sig => this.visitSignature(sig)),
});

private visitSignature = (sig: SignatureReflection): ITsMethodSignature => ({
...this.makeDocEntry(sig, Kind.MethodSignature),
private visitSignature = (sig: SignatureReflection): ITsSignature => ({
...this.makeDocEntry(sig, Kind.Signature),
flags: undefined,
parameters: (sig.parameters || []).map(param => this.visitParameter(param)),
returnType: resolveTypeString(sig.type),
type: resolveSignature(sig),
});

private visitParameter = (param: ParameterReflection): ITsMethodParameter => ({
...this.makeDocEntry(param, Kind.MethodParameter),
private visitParameter = (param: ParameterReflection): ITsParameter => ({
...this.makeDocEntry(param, Kind.Parameter),
defaultValue: getDefaultValue(param),
type: resolveTypeString(param.type),
});
Expand Down